aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector.rb
diff options
context:
space:
mode:
authorAdam Cigánek <adam.ciganek@gmail.com>2008-09-12 14:45:11 +0200
committerMichael Koziarski <michael@koziarski.com>2008-09-23 08:08:21 +0200
commita4f2ba8fb3c46ef9f7e31725849efdcb1a22c72d (patch)
tree7213d54c6b58397392c697e3f9e15d53a889ee77 /activesupport/lib/active_support/inflector.rb
parent961e2b861096c67573f3ddd2c9e55bb0658d6d88 (diff)
downloadrails-a4f2ba8fb3c46ef9f7e31725849efdcb1a22c72d.tar.gz
rails-a4f2ba8fb3c46ef9f7e31725849efdcb1a22c72d.tar.bz2
rails-a4f2ba8fb3c46ef9f7e31725849efdcb1a22c72d.zip
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]
Diffstat (limited to 'activesupport/lib/active_support/inflector.rb')
-rw-r--r--activesupport/lib/active_support/inflector.rb16
1 files changed, 11 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index b2046f26de..89a93f4a5f 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,20 @@ module ActiveSupport
# "#{id}-#{name.parameterize}"
# end
# end
- #
+ #
# @person = Person.find(1)
# # => #<Person id: 1, name: "Donald E. Knuth">
- #
+ #
# <%= link_to(@person.name, person_path %>
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
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
# Create the name of a table like Rails does for models to table names. This method