Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

FriendlyId::SlugGenerator#wildcard must escape underscores. #244

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion lib/friendly_id/slug_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def separator
end

def wildcard
"#{normalized}#{separator}%"
# Underscores (matching a single character) and percent signs (matching
# any number of characters) need to be escaped
# (While this seems like an excessive number of backslashes, it is correct)
"#{normalized}#{separator}".gsub(/[_%]/, '\\\\\&') + '%'
end
end
end
17 changes: 17 additions & 0 deletions test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ class Article < ActiveRecord::Base
friendly_id :name, :use => :slugged
end

class Novelist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :slugged, :sequence_separator => '_'

def normalize_friendly_id(string)
super.gsub("-", "_")
end
end

class SluggedTest < MiniTest::Unit::TestCase

include FriendlyId::Test
Expand Down Expand Up @@ -112,6 +121,14 @@ def model_class
end
end

test "should correctly sequence slugs with underscores" do
transaction do
record1 = Novelist.create! :name => 'wordsfail, buildings tumble'
record2 = Novelist.create! :name => 'word fail'
assert_equal 'word_fail', record2.slug
end
end

end

class SlugSeparatorTest < MiniTest::Unit::TestCase
Expand Down