From fa97886c7aab5b8bb7b58c5a830b8af31b079b3b Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 25 Sep 2008 00:02:38 -0700 Subject: [PATCH] change ActiveSupport::Inflector#parameterize to strip special characters inside words --- activesupport/lib/active_support/inflector.rb | 15 ++++++++------- activesupport/test/inflector_test_cases.rb | 7 ++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 89a93f4..7e6fdf1 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -254,16 +254,17 @@ module ActiveSupport # @person = Person.find(1) # # => # # - # <%= link_to(@person.name, person_path %> + # <%= link_to(@person.name, person_path) %> # # => Donald E. Knuth def parameterize(string, sep = '-') 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 + result = string.mb_chars.normalize(:kd) # Decompose accented characters + result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). + result.gsub!(/[^\w_ #{re_sep}]+/i, '') # Remove unwanted chars. + result.gsub!(/[ #{re_sep}]+/i, sep) # No more than one of the separator in a row. + result.gsub!(/^#{re_sep}|#{re_sep}$/i, '') # Remove leading/trailing separator. + result.downcase! + result 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 fc7a35f..5741888 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -150,7 +150,12 @@ module InflectorTestCases "Allow_Under_Scores" => "allow_under_scores", "Trailing bad characters!@#" => "trailing-bad-characters", "!@#Leading bad characters" => "leading-bad-characters", - "Squeeze separators" => "squeeze-separators" + "Trailing separators--" => "trailing-separators", + "--Leading separators" => "leading-separators", + "Trailing spaces " => "trailing-spaces", + " Leading spaces" => "leading-spaces", + "Squeeze separators" => "squeeze-separators", + "Fool's Errand" => "fools-errand" } UnderscoreToHuman = { -- 1.6.0.2