From c61544c7818f109c132fcad9db73d43216417535 Mon Sep 17 00:00:00 2001 From: claudiob Date: Wed, 6 Nov 2013 12:57:21 -0800 Subject: [PATCH] Add +capitalize+ option to Inflector.humanize So strings can be humanized without being capitalized: 'employee_salary'.humanize # => "Employee salary" 'employee_salary'.humanize(capitalize: false) # => "employee salary" --- activesupport/CHANGELOG.md | 7 ++++++ .../core_ext/string/inflections.rb | 16 +++++++++---- .../lib/active_support/inflector/methods.rb | 24 ++++++++++++------- .../test/core_ext/string_ext_test.rb | 8 ++++++- activesupport/test/inflector_test.rb | 6 +++++ activesupport/test/inflector_test_cases.rb | 6 +++++ .../source/active_support_core_extensions.md | 6 +++++ 7 files changed, 58 insertions(+), 15 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index b1544f6eac..9e63a2bb17 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* Add `capitalize` option to Inflector.humanize, so strings can be humanized without being capitalized: + + 'employee_salary'.humanize # => "Employee salary" + 'employee_salary'.humanize(capitalize: false) # => "employee salary" + + *claudiob* + * Fixed Object#as_json and Struct#as_json not working properly with options. They now take the same options as Hash#as_json: diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 56e8a5f98d..b7b750c77b 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -190,13 +190,19 @@ def classify ActiveSupport::Inflector.classify(self) end - # Capitalizes the first word, turns underscores into spaces, and strips '_id'. + # Capitalizes the first word, turns underscores into spaces, and strips a + # trailing '_id' if present. # Like +titleize+, this is meant for creating pretty output. # - # 'employee_salary'.humanize # => "Employee salary" - # 'author_id'.humanize # => "Author" - def humanize - ActiveSupport::Inflector.humanize(self) + # The capitalization of the first word can be turned off by setting the + # optional parameter +capitalize+ to false. + # By default, this parameter is true. + # + # 'employee_salary'.humanize # => "Employee salary" + # 'author_id'.humanize # => "Author" + # 'author_id'.humanize(capitalize: false) # => "author" + def humanize(options = {}) + ActiveSupport::Inflector.humanize(self, options) end # Creates a foreign key name from a class name. diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index ffdb7b53c4..0f7ae98a8a 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -98,20 +98,26 @@ def underscore(camel_cased_word) word end - # Capitalizes the first word and turns underscores into spaces and strips a - # trailing "_id", if any. Like +titleize+, this is meant for creating pretty - # output. - # - # 'employee_salary'.humanize # => "Employee salary" - # 'author_id'.humanize # => "Author" - def humanize(lower_case_and_underscored_word) + # Capitalizes the first word, turns underscores into spaces, and strips a + # trailing '_id' if present. + # Like +titleize+, this is meant for creating pretty output. + # + # The capitalization of the first word can be turned off by setting the + # optional parameter +capitalize+ to false. + # By default, this parameter is true. + # + # humanize('employee_salary') # => "Employee salary" + # humanize('author_id') # => "Author" + # humanize('author_id', capitalize: false) # => "author" + def humanize(lower_case_and_underscored_word, options = {}) result = lower_case_and_underscored_word.to_s.dup inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) } result.gsub!(/_id$/, "") result.tr!('_', ' ') - result.gsub(/([a-z\d]*)/i) { |match| + result.gsub!(/([a-z\d]*)/i) { |match| "#{inflections.acronyms[match] || match.downcase}" - }.gsub(/^\w/) { $&.upcase } + } + options.fetch(:capitalize, true) ? result.gsub(/^\w/) { $&.upcase } : result end # Capitalizes all the words and replaces some characters in the string to diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 3cb66d4eec..20e3d4802e 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -155,6 +155,12 @@ def test_humanize end end + def test_humanize_without_capitalize + UnderscoreToHumanWithoutCapitalize.each do |underscore, human| + assert_equal(human, underscore.humanize(capitalize: false)) + end + end + def test_ord assert_equal 97, 'a'.ord assert_equal 97, 'abc'.ord @@ -270,7 +276,7 @@ def test_truncate_multibyte def test_truncate_should_not_be_html_safe assert !"Hello World!".truncate(12).html_safe? end - + def test_remove assert_equal "Summer", "Fast Summer".remove(/Fast /) assert_equal "Summer", "Fast Summer".remove!(/Fast /) diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 32739d45a9..6184df481f 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -287,6 +287,12 @@ def test_humanize end end + def test_humanize_without_capitalize + UnderscoreToHumanWithoutCapitalize.each do |underscore, human| + assert_equal(human, ActiveSupport::Inflector.humanize(underscore, capitalize: false)) + end + end + def test_humanize_by_rule ActiveSupport::Inflector.inflections do |inflect| inflect.human(/_cnt$/i, '\1_count') diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index cc36d62138..b34a946baf 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -212,6 +212,12 @@ module InflectorTestCases "underground" => "Underground" } + UnderscoreToHumanWithoutCapitalize = { + "employee_salary" => "employee salary", + "employee_id" => "employee", + "underground" => "underground" + } + MixtureToTitleCase = { 'active_record' => 'Active Record', 'ActiveRecord' => 'Active Record', diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 0370e40012..b72ebd63ee 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -1772,6 +1772,12 @@ The method `humanize` gives you a sensible name for display out of an attribute "comments_count".humanize # => "Comments count" ``` +The capitalization of the first word can be turned off by setting the optional parameter `capitalize` to false: + +```ruby +"author_id".humanize(capitalize: false) # => "author" +``` + The helper method `full_messages` uses `humanize` as a fallback to include attribute names: ```ruby -- GitLab