Skip to content
This repository
Browse code

Allow String#parameterize to accept a separator [#2157 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 20d3892f46c553e8ca3d4e8d223a3bd92789556d 1 parent 7fb7b48
Sam Granieri authored March 07, 2009 lifo committed March 07, 2009
4  activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -102,8 +102,8 @@ def demodulize
102 102
         # 
103 103
         #   <%= link_to(@person.name, person_path %>
104 104
         #   # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
105  
-        def parameterize
106  
-          Inflector.parameterize(self)
  105
+        def parameterize(sep = '-')
  106
+          Inflector.parameterize(self, sep)
107 107
         end
108 108
 
109 109
         # Creates the name of a table like Rails does for models to table names. This method
18  activesupport/test/core_ext/string_ext_test.rb
@@ -77,6 +77,24 @@ def test_classify
77 77
     end
78 78
   end
79 79
 
  80
+  def test_string_parameterized_normal
  81
+    StringToParameterized.each do |normal, slugged|
  82
+      assert_equal(normal.parameterize, slugged)
  83
+    end
  84
+  end
  85
+
  86
+  def test_string_parameterized_no_separator
  87
+    StringToParameterizeWithNoSeparator.each do |normal, slugged|
  88
+      assert_equal(normal.parameterize(''), slugged)
  89
+    end
  90
+  end
  91
+
  92
+  def test_string_parameterized_underscore
  93
+    StringToParameterizeWithUnderscore.each do |normal, slugged|
  94
+      assert_equal(normal.parameterize('_'), slugged)
  95
+    end
  96
+  end
  97
+
80 98
   def test_humanize
81 99
     UnderscoreToHuman.each do |underscore, human|
82 100
       assert_equal(human, underscore.humanize)
16  activesupport/test/inflector_test_cases.rb
@@ -154,6 +154,22 @@ module InflectorTestCases
154 154
     "Squeeze   separators"                => "squeeze-separators"
155 155
   }
156 156
 
  157
+  StringToParameterizeWithNoSeparator = {
  158
+    "Donald E. Knuth"                     => "donaldeknuth",
  159
+    "Random text with *(bad)* characters" => "randomtextwithbadcharacters",
  160
+    "Trailing bad characters!@#"          => "trailingbadcharacters",
  161
+    "!@#Leading bad characters"           => "leadingbadcharacters",
  162
+    "Squeeze   separators"                => "squeezeseparators"
  163
+  }
  164
+
  165
+  StringToParameterizeWithUnderscore = {
  166
+    "Donald E. Knuth"                     => "donald_e_knuth",
  167
+    "Random text with *(bad)* characters" => "random_text_with_bad_characters",
  168
+    "Trailing bad characters!@#"          => "trailing_bad_characters",
  169
+    "!@#Leading bad characters"           => "leading_bad_characters",
  170
+    "Squeeze   separators"                => "squeeze_separators"
  171
+  }
  172
+
157 173
   # Ruby 1.9 doesn't do Unicode normalization yet.
158 174
   if RUBY_VERSION >= '1.9'
159 175
     StringToParameterizedAndNormalized = {

0 notes on commit 20d3892

Please sign in to comment.
Something went wrong with that request. Please try again.