Skip to content

Commit daaa21b

Browse files
committed
several enhancements to humanize [closes #12288]
* Strips leading underscores. * Changes some unnecessary gsub!s to sub!s. * Replaces some anchors ^, $ with \A, \z. * Documents that human inflection rules are applied. * Documents that words are downcased except acronyms. * Adds an example with an acronym. * Rewords docs.
1 parent d975819 commit daaa21b

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

activesupport/CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* `humanize` strips leading underscores, if any.
2+
3+
Before:
4+
5+
'_id'.humanize # => ""
6+
7+
After:
8+
9+
'_id'.humanize # => "Id"
10+
11+
*Xavier Noria*
12+
113
* Fixed backward compatibility isues introduced in 326e652.
214

315
Empty Hash or Array should not present in serialization result.

activesupport/lib/active_support/inflector/methods.rb

+29-9
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,46 @@ def underscore(camel_cased_word)
9999
word
100100
end
101101

102-
# Capitalizes the first word, turns underscores into spaces, and strips a
103-
# trailing '_id' if present.
104-
# Like +titleize+, this is meant for creating pretty output.
102+
# Tweaks an attribute name for display to end users.
103+
#
104+
# Specifically, +humanize+ performs these transformations:
105+
#
106+
# * Applies human inflection rules to the argument.
107+
# * Deletes leading underscores, if any.
108+
# * Removes a "_id" suffix if present.
109+
# * Replaces underscores with spaces, if any.
110+
# * Downcases all words except acronyms.
111+
# * Capitalizes the first word.
105112
#
106113
# The capitalization of the first word can be turned off by setting the
107-
# optional parameter +capitalize+ to false.
108-
# By default, this parameter is true.
114+
# +:capitalize+ option to false (default is true).
109115
#
110116
# humanize('employee_salary') # => "Employee salary"
111117
# humanize('author_id') # => "Author"
112118
# humanize('author_id', capitalize: false) # => "author"
119+
# humanize('_id') # => "Id"
120+
#
121+
# If "SSL" was defined to be an acronym:
122+
#
123+
# humanize('ssl_error') # => "SSL error"
124+
#
113125
def humanize(lower_case_and_underscored_word, options = {})
114126
result = lower_case_and_underscored_word.to_s.dup
127+
115128
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
116-
result.gsub!(/_id$/, "")
129+
130+
result.sub!(/\A_+/, '')
131+
result.sub!(/_id\z/, '')
117132
result.tr!('_', ' ')
118-
result.gsub!(/([a-z\d]*)/i) { |match|
133+
134+
result.gsub!(/([a-z\d]*)/i) do |match|
119135
"#{inflections.acronyms[match] || match.downcase}"
120-
}
121-
result.gsub!(/^\w/) { |match| match.upcase } if options.fetch(:capitalize, true)
136+
end
137+
138+
if options.fetch(:capitalize, true)
139+
result.sub!(/\A\w/) { |match| match.upcase }
140+
end
141+
122142
result
123143
end
124144

activesupport/test/inflector_test_cases.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ module InflectorTestCases
208208
}
209209

210210
UnderscoreToHuman = {
211-
"employee_salary" => "Employee salary",
212-
"employee_id" => "Employee",
213-
"underground" => "Underground"
211+
'employee_salary' => 'Employee salary',
212+
'employee_id' => 'Employee',
213+
'underground' => 'Underground',
214+
'_id' => 'Id',
215+
'_external_id' => 'External'
214216
}
215217

216218
UnderscoreToHumanWithoutCapitalize = {

guides/source/active_support_core_extensions.md

+22-7
Original file line numberDiff line numberDiff line change
@@ -1768,21 +1768,36 @@ NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
17681768

17691769
#### `humanize`
17701770

1771-
The method `humanize` gives you a sensible name for display out of an attribute name. To do so it replaces underscores with spaces, removes any "_id" suffix, and capitalizes the first word:
1771+
The method `humanize` tqweaks an attribute name for display to end users.
1772+
1773+
Specifically performs these transformations:
1774+
1775+
* Applies human inflection rules to the argument.
1776+
* Deletes leading underscores, if any.
1777+
* Removes a "_id" suffix if present.
1778+
* Replaces underscores with spaces, if any.
1779+
* Downcases all words except acronyms.
1780+
* Capitalizes the first word.
1781+
1782+
The capitalization of the first word can be turned off by setting the
1783+
+:capitalize+ option to false (default is true).
17721784

17731785
```ruby
1774-
"name".humanize # => "Name"
1775-
"author_id".humanize # => "Author"
1776-
"comments_count".humanize # => "Comments count"
1786+
"name".humanize # => "Name"
1787+
"author_id".humanize # => "Author"
1788+
"author_id".humanize(capitalize: false) # => "author"
1789+
"comments_count".humanize # => "Comments count"
1790+
"_id".humanize # => "Id"
17771791
```
17781792

1779-
The capitalization of the first word can be turned off by setting the optional parameter `capitalize` to false:
1793+
If "SSL" was defined to be an acronym:
17801794

17811795
```ruby
1782-
"author_id".humanize(capitalize: false) # => "author"
1796+
'ssl_error'.humanize # => "SSL error"
17831797
```
17841798

1785-
The helper method `full_messages` uses `humanize` as a fallback to include attribute names:
1799+
The helper method `full_messages` uses `humanize` as a fallback to include
1800+
attribute names:
17861801

17871802
```ruby
17881803
def full_messages

0 commit comments

Comments
 (0)