Skip to content

Commit

Permalink
* Fixed regex to support multiple digit sequences
Browse files Browse the repository at this point in the history
The parse_friendly_id method was only grabbing the first number in a
squence. Modified regex to support multiple digit numbers
  • Loading branch information
Nash Kabbara committed Mar 10, 2010
1 parent 13e1573 commit c41bc99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/friendly_id.rb
Expand Up @@ -61,7 +61,7 @@ def uses_friendly_id?
class String
def parse_friendly_id(separator = nil)
separator ||= FriendlyId::Configuration::DEFAULTS[:sequence_separator]
name, sequence = split(/#{Regexp.escape(separator)}(\d)*\z/)
name, sequence = split(/#{Regexp.escape(separator)}(\d+)*\z/)
return name, sequence ||= "1"
end
end
21 changes: 21 additions & 0 deletions test/friendly_id_test.rb
Expand Up @@ -10,6 +10,18 @@ class FriendlyIdTest < ::Test::Unit::TestCase
assert_equal ["test", "2"], "test--2".parse_friendly_id
end

test "should parse a friendly_id name and 10 as a sequence" do
assert_equal ["test", "10"], "test--10".parse_friendly_id
end

test "should parse a friendly_id name and 11 as a sequence" do
assert_equal ["test", "11"], "test--11".parse_friendly_id
end

test "should parse a friendly_id name and 29 as a sequence" do
assert_equal ["test", "29"], "test--29".parse_friendly_id
end

test "should parse with a default sequence of 1" do
assert_equal ["test", "1"], "test".parse_friendly_id
end
Expand All @@ -18,6 +30,10 @@ class FriendlyIdTest < ::Test::Unit::TestCase
assert_equal ["test", "2"], "test:2".parse_friendly_id(":")
end

test "should be parseable with a custom separator and a double digit sequence" do
assert_equal ["test", "12"], "test:12".parse_friendly_id(":")
end

test "should parse when default sequence separator occurs in friendly_id name" do
assert_equal ["test--test", "2"], "test--test--2".parse_friendly_id
end
Expand All @@ -34,6 +50,11 @@ class FriendlyIdTest < ::Test::Unit::TestCase
assert_equal ["test--2--test", "2"], "test--2--test--2".parse_friendly_id
end

test "should parse when double digit sequence separator, number and sequence occur in friendly_id name" do
assert_equal ["test--2--test", "12"], "test--2--test--12".parse_friendly_id
end


end
end
end

0 comments on commit c41bc99

Please sign in to comment.