提交 f8e5022c 编写于 作者: X Xavier Noria

Merge pull request #12789 from claudiob/humanize-without-capitalizing

Add +capitalize+ option to Inflector.humanize
* 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 * Fixed Object#as_json and Struct#as_json not working properly with options. They now take
the same options as Hash#as_json: the same options as Hash#as_json:
......
...@@ -190,13 +190,19 @@ def classify ...@@ -190,13 +190,19 @@ def classify
ActiveSupport::Inflector.classify(self) ActiveSupport::Inflector.classify(self)
end 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. # Like +titleize+, this is meant for creating pretty output.
# #
# 'employee_salary'.humanize # => "Employee salary" # The capitalization of the first word can be turned off by setting the
# 'author_id'.humanize # => "Author" # optional parameter +capitalize+ to false.
def humanize # By default, this parameter is true.
ActiveSupport::Inflector.humanize(self) #
# 'employee_salary'.humanize # => "Employee salary"
# 'author_id'.humanize # => "Author"
# 'author_id'.humanize(capitalize: false) # => "author"
def humanize(options = {})
ActiveSupport::Inflector.humanize(self, options)
end end
# Creates a foreign key name from a class name. # Creates a foreign key name from a class name.
......
...@@ -98,20 +98,26 @@ def underscore(camel_cased_word) ...@@ -98,20 +98,26 @@ def underscore(camel_cased_word)
word word
end end
# Capitalizes the first word and turns underscores into spaces and strips a # Capitalizes the first word, turns underscores into spaces, and strips a
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty # trailing '_id' if present.
# output. # Like +titleize+, this is meant for creating pretty output.
# #
# 'employee_salary'.humanize # => "Employee salary" # The capitalization of the first word can be turned off by setting the
# 'author_id'.humanize # => "Author" # optional parameter +capitalize+ to false.
def humanize(lower_case_and_underscored_word) # 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 result = lower_case_and_underscored_word.to_s.dup
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) } inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
result.gsub!(/_id$/, "") result.gsub!(/_id$/, "")
result.tr!('_', ' ') result.tr!('_', ' ')
result.gsub(/([a-z\d]*)/i) { |match| result.gsub!(/([a-z\d]*)/i) { |match|
"#{inflections.acronyms[match] || match.downcase}" "#{inflections.acronyms[match] || match.downcase}"
}.gsub(/^\w/) { $&.upcase } }
options.fetch(:capitalize, true) ? result.gsub(/^\w/) { $&.upcase } : result
end end
# Capitalizes all the words and replaces some characters in the string to # Capitalizes all the words and replaces some characters in the string to
......
...@@ -155,6 +155,12 @@ def test_humanize ...@@ -155,6 +155,12 @@ def test_humanize
end end
end end
def test_humanize_without_capitalize
UnderscoreToHumanWithoutCapitalize.each do |underscore, human|
assert_equal(human, underscore.humanize(capitalize: false))
end
end
def test_ord def test_ord
assert_equal 97, 'a'.ord assert_equal 97, 'a'.ord
assert_equal 97, 'abc'.ord assert_equal 97, 'abc'.ord
...@@ -270,7 +276,7 @@ def test_truncate_multibyte ...@@ -270,7 +276,7 @@ def test_truncate_multibyte
def test_truncate_should_not_be_html_safe def test_truncate_should_not_be_html_safe
assert !"Hello World!".truncate(12).html_safe? assert !"Hello World!".truncate(12).html_safe?
end end
def test_remove def test_remove
assert_equal "Summer", "Fast Summer".remove(/Fast /) assert_equal "Summer", "Fast Summer".remove(/Fast /)
assert_equal "Summer", "Fast Summer".remove!(/Fast /) assert_equal "Summer", "Fast Summer".remove!(/Fast /)
......
...@@ -287,6 +287,12 @@ def test_humanize ...@@ -287,6 +287,12 @@ def test_humanize
end end
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 def test_humanize_by_rule
ActiveSupport::Inflector.inflections do |inflect| ActiveSupport::Inflector.inflections do |inflect|
inflect.human(/_cnt$/i, '\1_count') inflect.human(/_cnt$/i, '\1_count')
......
...@@ -212,6 +212,12 @@ module InflectorTestCases ...@@ -212,6 +212,12 @@ module InflectorTestCases
"underground" => "Underground" "underground" => "Underground"
} }
UnderscoreToHumanWithoutCapitalize = {
"employee_salary" => "employee salary",
"employee_id" => "employee",
"underground" => "underground"
}
MixtureToTitleCase = { MixtureToTitleCase = {
'active_record' => 'Active Record', 'active_record' => 'Active Record',
'ActiveRecord' => 'Active Record', 'ActiveRecord' => 'Active Record',
......
...@@ -1772,6 +1772,12 @@ The method `humanize` gives you a sensible name for display out of an attribute ...@@ -1772,6 +1772,12 @@ The method `humanize` gives you a sensible name for display out of an attribute
"comments_count".humanize # => "Comments count" "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: The helper method `full_messages` uses `humanize` as a fallback to include attribute names:
```ruby ```ruby
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册