From 90366a1521659d07a3b75936b3231adeb376f1a4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 10 Sep 2008 00:26:50 -0500 Subject: Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 [Matt Darby] --- activesupport/lib/active_support/inflector.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'activesupport/lib/active_support/inflector.rb') diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 7ae9e0c6ab..e5f0063285 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -240,6 +240,25 @@ 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 + # def to_param + # "#{id}-#{name.parameterize}" + # end + # end + # + # @person = Person.find(1) + # # => # + # + # <%= link_to(@person.name, person_path %> + # # => Donald E. Knuth + def parameterize(string, sep = '-') + string.gsub(/[^a-z0-9]+/i, sep).downcase + end # Create the name of a table like Rails does for models to table names. This method # uses the +pluralize+ method on the last word in the string. -- cgit v1.2.3