Skip to content

Commit

Permalink
Fix bug in sequencing slugs starting with numbers
Browse files Browse the repository at this point in the history
Fixes a bug where a slug conflicting with a slug that starts with a number
would get incorrectly sequenced based on the number at the start of the slug,
rather than as a fresh sequence.
  • Loading branch information
tekin committed May 15, 2015
1 parent a9c04aa commit 43caf4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/friendly_id/sequentially_slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ def next_slug
private

def next_sequence_number
if last_sequence_number == 0
2
else
last_sequence_number + 1
end
last_sequence_number ? last_sequence_number + 1 : 2
end

def last_sequence_number
slug_conflicts.last.split("#{slug}#{sequence_separator}").last.to_i
if match = /#{slug}#{sequence_separator}(\d+)/.match(slug_conflicts.last)
match[1].to_i
end
end

def slug_conflicts
Expand Down
9 changes: 9 additions & 0 deletions test/sequentially_slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ def model_class
end
end

test "should correctly sequence slugs that begin with a number" do
transaction do
record1 = model_class.create! :name => "2010 to 2015 Records"
assert_equal "2010-to-2015-records", record1.slug
record2 = model_class.create! :name => "2010 to 2015 Records"
assert_equal "2010-to-2015-records-2", record2.slug
end
end

test "should sequence with a custom sequence separator" do
model_class = Class.new(ActiveRecord::Base) do
self.table_name = "novelists"
Expand Down

0 comments on commit 43caf4b

Please # to comment.