Skip to content

Commit

Permalink
Allow String#parameterize to accept a separator [#2157 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
samgranieri authored and lifo committed Mar 7, 2009
1 parent 7fb7b48 commit 20d3892
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Expand Up @@ -102,8 +102,8 @@ def demodulize
#
# <%= link_to(@person.name, person_path %>
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
def parameterize
Inflector.parameterize(self)
def parameterize(sep = '-')
Inflector.parameterize(self, sep)
end

# Creates the name of a table like Rails does for models to table names. This method
Expand Down
18 changes: 18 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -77,6 +77,24 @@ def test_classify
end
end

def test_string_parameterized_normal
StringToParameterized.each do |normal, slugged|
assert_equal(normal.parameterize, slugged)
end
end

def test_string_parameterized_no_separator
StringToParameterizeWithNoSeparator.each do |normal, slugged|
assert_equal(normal.parameterize(''), slugged)
end
end

def test_string_parameterized_underscore
StringToParameterizeWithUnderscore.each do |normal, slugged|
assert_equal(normal.parameterize('_'), slugged)
end
end

def test_humanize
UnderscoreToHuman.each do |underscore, human|
assert_equal(human, underscore.humanize)
Expand Down
16 changes: 16 additions & 0 deletions activesupport/test/inflector_test_cases.rb
Expand Up @@ -154,6 +154,22 @@ module InflectorTestCases
"Squeeze separators" => "squeeze-separators"
}

StringToParameterizeWithNoSeparator = {
"Donald E. Knuth" => "donaldeknuth",
"Random text with *(bad)* characters" => "randomtextwithbadcharacters",
"Trailing bad characters!@#" => "trailingbadcharacters",
"!@#Leading bad characters" => "leadingbadcharacters",
"Squeeze separators" => "squeezeseparators"
}

StringToParameterizeWithUnderscore = {
"Donald E. Knuth" => "donald_e_knuth",
"Random text with *(bad)* characters" => "random_text_with_bad_characters",
"Trailing bad characters!@#" => "trailing_bad_characters",
"!@#Leading bad characters" => "leading_bad_characters",
"Squeeze separators" => "squeeze_separators"
}

# Ruby 1.9 doesn't do Unicode normalization yet.
if RUBY_VERSION >= '1.9'
StringToParameterizedAndNormalized = {
Expand Down

0 comments on commit 20d3892

Please sign in to comment.