From dab4955ee75ee972e2e173b9d7b69d25f34ebeb8 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Adam=20Cig=C3=A1nek?= Date: Fri, 12 Sep 2008 14:45:11 +0200 Subject: [PATCH] Change ActiveSupport::Inflector#parameterize to strip leading/trailing dashes --- activesupport/lib/active_support/inflector.rb | 10 +++++----- activesupport/test/inflector_test_cases.rb | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index b2046f2..6784824 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -240,9 +240,9 @@ module ActiveSupport def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end - + # Replaces special characters in a string so that it may be used as part of a 'pretty' URL. - # + # # ==== Examples # # class Person @@ -250,14 +250,14 @@ module ActiveSupport # "#{id}-#{name.parameterize}" # end # end - # + # # @person = Person.find(1) # # => # - # + # # <%= link_to(@person.name, person_path %> # # => Donald E. Knuth def parameterize(string, sep = '-') - string.mb_chars.normalize(:kd).to_s.gsub(/[^\x00-\x7F]+/, '').gsub(/[^a-z0-9_\-]+/i, sep).downcase + string.mb_chars.normalize(:kd).to_s.gsub(/[^\x00-\x7F]+/, '').gsub(/[^a-z0-9_\-]+/i, sep).gsub(/^\-+|\-+$/, '').downcase end # Create the name of a table like Rails does for models to table names. This method diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index 8057809..88dd9c6 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -147,7 +147,9 @@ module InflectorTestCases "Random text with *(bad)* characters" => "random-text-with-bad-characters", "Malmö" => "malmo", "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" } UnderscoreToHuman = { -- 1.6.0.2 From 135b7e2f923e1e818701df3aea4f53358693ef1e Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Adam=20Cig=C3=A1nek?= Date: Fri, 12 Sep 2008 18:31:50 +0200 Subject: [PATCH] Modified ActiveSupport::Inflector#parameterize with code from slugalizer (http://github.com/henrik/slugalizer) --- activesupport/lib/active_support/inflector.rb | 9 ++++++++- activesupport/test/inflector_test.rb | 6 ++++++ activesupport/test/inflector_test_cases.rb | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 6784824..84fab35 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -257,7 +257,14 @@ module ActiveSupport # <%= link_to(@person.name, person_path %> # # => Donald E. Knuth def parameterize(string, sep = '-') - string.mb_chars.normalize(:kd).to_s.gsub(/[^\x00-\x7F]+/, '').gsub(/[^a-z0-9_\-]+/i, sep).gsub(/^\-+|\-+$/, '').downcase + # This code is taken from slugalizer (http://github.com/henrik/slugalizer) + 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 # Create the name of a table like Rails does for models to table names. This method diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index f304844..d30852c 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -104,6 +104,12 @@ class InflectorTest < Test::Unit::TestCase 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 ClassNameToTableName.each do |class_name, table_name| assert_equal(class_name, ActiveSupport::Inflector.classify(table_name)) diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index 88dd9c6..fc7a35f 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -149,7 +149,8 @@ module InflectorTestCases "Garçons" => "garcons", "Allow_Under_Scores" => "allow_under_scores", "Trailing bad characters!@#" => "trailing-bad-characters", - "!@#Leading bad characters" => "leading-bad-characters" + "!@#Leading bad characters" => "leading-bad-characters", + "Squeeze separators" => "squeeze-separators" } UnderscoreToHuman = { -- 1.6.0.2