Skip to content

Commit 90366a1

Browse files
author
David Heinemeier Hansson
committed
Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 [Matt Darby]
1 parent b141624 commit 90366a1

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

activesupport/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*Edge*
22

3+
* Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 [Matt Darby]
4+
35
* Changed cache benchmarking to be reported in milliseconds [DHH]
46

57
* 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]

activesupport/lib/active_support/core_ext/string/inflections.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ def demodulize
8787
Inflector.demodulize(self)
8888
end
8989

90+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
91+
#
92+
# ==== Examples
93+
#
94+
# class Person
95+
# def to_param
96+
# "#{id}-#{name.parameterize}"
97+
# end
98+
# end
99+
#
100+
# @person = Person.find(1)
101+
# # => #<Person id: 1, name: "Donald E. Knuth">
102+
#
103+
# <%= link_to(@person.name, person_path %>
104+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
105+
def parameterize
106+
Inflector.parameterize(self)
107+
end
108+
90109
# Creates the name of a table like Rails does for models to table names. This method
91110
# uses the +pluralize+ method on the last word in the string.
92111
#

activesupport/lib/active_support/inflector.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ def humanize(lower_case_and_underscored_word)
240240
def demodulize(class_name_in_module)
241241
class_name_in_module.to_s.gsub(/^.*::/, '')
242242
end
243+
244+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
245+
#
246+
# ==== Examples
247+
#
248+
# class Person
249+
# def to_param
250+
# "#{id}-#{name.parameterize}"
251+
# end
252+
# end
253+
#
254+
# @person = Person.find(1)
255+
# # => #<Person id: 1, name: "Donald E. Knuth">
256+
#
257+
# <%= link_to(@person.name, person_path %>
258+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
259+
def parameterize(string, sep = '-')
260+
string.gsub(/[^a-z0-9]+/i, sep).downcase
261+
end
243262

244263
# Create the name of a table like Rails does for models to table names. This method
245264
# uses the +pluralize+ method on the last word in the string.

activesupport/test/inflector_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def test_tableize
9898
end
9999
end
100100

101+
def test_parameterize
102+
StringToParameterized.each do |some_string, parameterized_string|
103+
assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string))
104+
end
105+
end
106+
101107
def test_classify
102108
ClassNameToTableName.each do |class_name, table_name|
103109
assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))

activesupport/test/inflector_test_cases.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ module InflectorTestCases
142142
"NodeChild" => "node_children"
143143
}
144144

145+
StringToParameterized = {
146+
"Donald E. Knuth" => "donald-e-knuth",
147+
"Random text with *(bad)* characters" => "random-text-with-bad-characters"
148+
}
149+
145150
UnderscoreToHuman = {
146151
"employee_salary" => "Employee salary",
147152
"employee_id" => "Employee",

0 commit comments

Comments
 (0)