Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/friendly_id/slug_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ def generate(candidates)

def purely_numeric_slug?(slug)
return false unless slug
begin
Integer(slug, 10).to_s == slug.to_s
rescue ArgumentError, TypeError
false
end

slug.to_s.match?(/\A[0-9]+\z/)
end
end
end
10 changes: 10 additions & 0 deletions test/numeric_slug_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

class Article < ActiveRecord::Base
extend FriendlyId

friendly_id :name, use: :slugged
end

class ArticleWithNumericPrevention < ActiveRecord::Base
self.table_name = "articles"
extend FriendlyId

friendly_id :name, use: :slugged
friendly_id_config.treat_numeric_as_conflict = true
end
Expand Down Expand Up @@ -71,6 +73,14 @@ def model_class
end
end

test "should handle numbers with leading zeroes as numeric when treat_numeric_as_conflict is enabled" do
transaction do
record = ArticleWithNumericPrevention.create! name: "00123"
refute_equal "00123", record.slug
assert_match(/\A00123-[0-9a-f-]{36}\z/, record.slug)
end
end

test "should handle large numbers as numeric when treat_numeric_as_conflict is enabled" do
transaction do
record = ArticleWithNumericPrevention.create! name: "999999999"
Expand Down
Loading