1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Modified ActiveSupport::Inflector#parameterize with code from slugalizer (http://github.com/henrik/slugalizer)

Handles trailing and leading slashes, and squashes repeated separators into a single character.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1034 state:committed]
This commit is contained in:
Adam Cigánek 2008-09-12 14:45:11 +02:00 committed by Michael Koziarski
parent 961e2b8610
commit a4f2ba8fb3
3 changed files with 21 additions and 6 deletions

View file

@ -240,9 +240,9 @@ module ActiveSupport
def demodulize(class_name_in_module) def demodulize(class_name_in_module)
class_name_in_module.to_s.gsub(/^.*::/, '') class_name_in_module.to_s.gsub(/^.*::/, '')
end end
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL. # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
# #
# ==== Examples # ==== Examples
# #
# class Person # class Person
@ -250,14 +250,20 @@ module ActiveSupport
# "#{id}-#{name.parameterize}" # "#{id}-#{name.parameterize}"
# end # end
# end # end
# #
# @person = Person.find(1) # @person = Person.find(1)
# # => #<Person id: 1, name: "Donald E. Knuth"> # # => #<Person id: 1, name: "Donald E. Knuth">
# #
# <%= link_to(@person.name, person_path %> # <%= link_to(@person.name, person_path %>
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a> # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
def parameterize(string, sep = '-') def parameterize(string, sep = '-')
string.mb_chars.normalize(:kd).to_s.gsub(/[^\x00-\x7F]+/, '').gsub(/[^a-z0-9_\-]+/i, sep).downcase re_sep = Regexp.escape(sep)
string.mb_chars.normalize(:kd). # Decompose accented characters
gsub(/[^\x00-\x7F]+/, ''). # Remove anything non-ASCII entirely (e.g. diacritics).
gsub(/[^a-z0-9\-_\+]+/i, sep). # Turn unwanted chars into the separator.
squeeze(sep). # No more than one of the separator in a row.
gsub(/^#{re_sep}|#{re_sep}$/i, ''). # Remove leading/trailing separator.
downcase
end end
# Create the name of a table like Rails does for models to table names. This method # Create the name of a table like Rails does for models to table names. This method

View file

@ -104,6 +104,12 @@ class InflectorTest < Test::Unit::TestCase
end end
end end
def test_parameterize_with_custom_separator
StringToParameterized.each do |some_string, parameterized_string|
assert_equal(parameterized_string.gsub('-', '_'), ActiveSupport::Inflector.parameterize(some_string, '_'))
end
end
def test_classify def test_classify
ClassNameToTableName.each do |class_name, table_name| ClassNameToTableName.each do |class_name, table_name|
assert_equal(class_name, ActiveSupport::Inflector.classify(table_name)) assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))

View file

@ -147,7 +147,10 @@ module InflectorTestCases
"Random text with *(bad)* characters" => "random-text-with-bad-characters", "Random text with *(bad)* characters" => "random-text-with-bad-characters",
"Malmö" => "malmo", "Malmö" => "malmo",
"Garçons" => "garcons", "Garçons" => "garcons",
"Allow_Under_Scores" => "allow_under_scores" "Allow_Under_Scores" => "allow_under_scores",
"Trailing bad characters!@#" => "trailing-bad-characters",
"!@#Leading bad characters" => "leading-bad-characters",
"Squeeze separators" => "squeeze-separators"
} }
UnderscoreToHuman = { UnderscoreToHuman = {