Permalink
Browse files

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.
  • Loading branch information...
pixeltrix committed Nov 6, 2017
1 parent 7d1d6c4 commit 0ddde0a8fca6a0ca3158e3329713959acd65605d
@@ -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`.
@@ -138,7 +138,7 @@ def humanize(lower_case_and_underscored_word, capitalize: true, keep_id_suffix:
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
@@ -356,6 +356,19 @@ def test_humanize_by_string
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)

0 comments on commit 0ddde0a

Please sign in to comment.