Skip to content

Commit

Permalink
Fix acronym support in humanize
Browse files Browse the repository at this point in the history
Acronym inflections are stored with lowercase keys in the hash but
the match wasn't being lowercased before being looked up in the hash.
This shouldn't have any performance impact because before it would
fail to find the acronym and perform the `downcase` operation anyway.

Fixes #31052.
  • Loading branch information
pixeltrix committed Nov 6, 2017
1 parent 7d1d6c4 commit 0ddde0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,14 @@
* Fix acronym support in `humanize`

Acronym inflections are stored with lowercase keys in the hash but
the match wasn't being lowercased before being looked up in the hash.
This shouldn't have any performance impact because before it would
fail to find the acronym and perform the `downcase` operation anyway.

Fixes #31052.

*Andrew White*

* Add same method signature for `Time#prev_year` and `Time#next_year` * Add same method signature for `Time#prev_year` and `Time#next_year`
in accordance with `Date#prev_year`, `Date#next_year`. in accordance with `Date#prev_year`, `Date#next_year`.


Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/inflector/methods.rb
Expand Up @@ -138,7 +138,7 @@ def humanize(lower_case_and_underscored_word, capitalize: true, keep_id_suffix:
result.tr!("_".freeze, " ".freeze) result.tr!("_".freeze, " ".freeze)


result.gsub!(/([a-z\d]*)/i) do |match| result.gsub!(/([a-z\d]*)/i) do |match|
"#{inflections.acronyms[match] || match.downcase}" "#{inflections.acronyms[match.downcase] || match.downcase}"
end end


if capitalize if capitalize
Expand Down
13 changes: 13 additions & 0 deletions activesupport/test/inflector_test.rb
Expand Up @@ -356,6 +356,19 @@ def test_humanize_by_string
assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs")) assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
end end


def test_humanize_with_acronyms
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "LAX"
inflect.acronym "SFO"
end
assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("LAX ROUNDTRIP TO SFO"))
assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("LAX ROUNDTRIP TO SFO", capitalize: false))
assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("lax roundtrip to sfo"))
assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("lax roundtrip to sfo", capitalize: false))
assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("Lax Roundtrip To Sfo"))
assert_equal("LAX roundtrip to SFO", ActiveSupport::Inflector.humanize("Lax Roundtrip To Sfo", capitalize: false))
end

def test_constantize def test_constantize
run_constantize_tests_on do |string| run_constantize_tests_on do |string|
ActiveSupport::Inflector.constantize(string) ActiveSupport::Inflector.constantize(string)
Expand Down

0 comments on commit 0ddde0a

Please sign in to comment.