diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 3bbad7d..ca15865 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -87,6 +87,40 @@ module ActiveSupport #:nodoc: Inflector.demodulize(self) end + # Transforms a string in titleize() form to a string suitable for a action name + # + # "Expense Report".actionize # => "expense_report" + # "My Totally Cool Action".actionize # => "my_totally_cool_action" + # + # @reports = ['Expense Report', 'Employee Hours'] + # @reports.each do |report_name| + # link_to(report_name, "/reports/#{report_name.actionize}") + # end + # # => "Expense Report + # Employee Hours" + def actionize + Inflector.actionize(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 + # "#{self.id}-#{self.name.parameterize}" + # end + # end + # + # @person = Person.find(1) + # # => # + # + # <%= link_to(@person.name, person_path %> + # # => Login + 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. # diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 6651569..04a92ea 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -234,6 +234,40 @@ module ActiveSupport def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end + + # Transforms a string in titleize() form to a string suitable for a action name + # + # "Expense Report".actionize # => "expense_report" + # "My Totally Cool Action".actionize # => "my_totally_cool_action" + # + # @reports = ['Expense Report', 'Employee Hours'] + # @reports.each do |report_name| + # link_to(report_name, "/reports/#{report_name.actionize}") + # end + # # => "Expense Report + # Employee Hours" + def actionize(titleized_string) + titleized_string.downcase.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 + # "#{self.id}-#{self.name.parameterize}" + # end + # end + # + # @person = Person.find(1) + # # => # + # + # <%= link_to(@person.name, person_path %> + # # => Login + def parameterize(string, sep = '_') + string.gsub(/[^a-z0-9]+/i, sep) + 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. diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 6c0c14e..da5ef53 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -87,6 +87,18 @@ class InflectorTest < Test::Unit::TestCase end end + def test_actionize + TitleizedToActionized.each do |titleized_string, actionized_string| + assert_equal(actionized_string, ActiveSupport::Inflector.actionize(titleized_string)) + 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)) diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index a9dd83a..0ca3ae6 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -142,6 +142,16 @@ module InflectorTestCases "NodeChild" => "node_children" } + TitleizedToActionized = { + "Expense Report" => "expense_report", + "Employee Hours By Month" => "employee_hours_by_month" + } + + 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",