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.
This commit is contained in:
Andrew White 2017-11-06 14:46:42 +00:00
parent 7d1d6c4c0b
commit 0ddde0a8fc
3 changed files with 25 additions and 1 deletions

View File

@ -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`
in accordance with `Date#prev_year`, `Date#next_year`.

View File

@ -138,7 +138,7 @@ module ActiveSupport
result.tr!("_".freeze, " ".freeze)
result.gsub!(/([a-z\d]*)/i) do |match|
"#{inflections.acronyms[match] || match.downcase}"
"#{inflections.acronyms[match.downcase] || match.downcase}"
end
if capitalize

View File

@ -356,6 +356,19 @@ class InflectorTest < ActiveSupport::TestCase
assert_equal("Col rpted bugs", ActiveSupport::Inflector.humanize("COL_rpted_bugs"))
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
run_constantize_tests_on do |string|
ActiveSupport::Inflector.constantize(string)