Skip to content

Commit

Permalink
Added Inflector#parameterize for easy slug generation ("Donald E. Knu…
Browse files Browse the repository at this point in the history
…th".parameterize => "donald-e-knuth") #713 [Matt Darby]
  • Loading branch information
dhh committed Sep 10, 2008
1 parent b141624 commit 90366a1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 [Matt Darby]

* Changed cache benchmarking to be reported in milliseconds [DHH]

* Fix Ruby's Time marshaling bug in pre-1.9 versions of Ruby: utc instances are now correctly unmarshaled with a utc zone instead of the system local zone [#900 state:resolved] [Luca Guidi, Geoff Buesing]
Expand Down
19 changes: 19 additions & 0 deletions activesupport/lib/active_support/core_ext/string/inflections.rb
Expand Up @@ -87,6 +87,25 @@ def demodulize
Inflector.demodulize(self)
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)
# # => #<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
Inflector.parameterize(self)
end

# Creates 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.
#
Expand Down
19 changes: 19 additions & 0 deletions activesupport/lib/active_support/inflector.rb
Expand Up @@ -240,6 +240,25 @@ def humanize(lower_case_and_underscored_word)
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)
# # => #<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.gsub(/[^a-z0-9]+/i, sep).downcase

This comment has been minimized.

Copy link
@joshsusser

joshsusser Sep 10, 2008

Contributor

It would be nice to see underscore (_) as an allowed character. I use those in slugs all the time, and they work fine in URLs. Use /\W+/ for the regexp, easy!

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.
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/inflector_test.rb
Expand Up @@ -98,6 +98,12 @@ def test_tableize
end
end

def test_parameterize
StringToParameterized.each do |some_string, parameterized_string|
assert_equal(parameterized_string, 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))
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/inflector_test_cases.rb
Expand Up @@ -142,6 +142,11 @@ module InflectorTestCases
"NodeChild" => "node_children"
}

StringToParameterized = {
"Donald E. Knuth" => "donald-e-knuth",
"Random text with *(bad)* characters" => "random-text-with-bad-characters"
}

UnderscoreToHuman = {
"employee_salary" => "Employee salary",
"employee_id" => "Employee",
Expand Down

3 comments on commit 90366a1

@bumi
Copy link

@bumi bumi commented on 90366a1 Sep 10, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, underscore would be great.

@kpumuk
Copy link
Contributor

@kpumuk kpumuk commented on 90366a1 Sep 10, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about stripping out separators from the beginning and end of string?
“!!!Donald E. Knuth!!!”.parameterize => “donald-e-knuth

@henrik
Copy link
Contributor

@henrik henrik commented on 90366a1 Sep 11, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slugalizer handles underscore and separators in beginning/end of string, and some other edge cases:

http://github.com/henrik/slugalizer

Commented about that previously in http://github.com/rails/rails/commit/b8e8be83e952163e225f9b38bd7251cba9c44f38#comments

Please sign in to comment.