diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index a2c872cb89578..ba48358879e11 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,9 @@ +* `ActiveSupport::Inflector::Inflections#clear(:acronyms)` is now supported, + and `inflector.clear` / `inflector.clear(:all)` also clears acronyms. + + *Alex Ghiculescu*, *Oliver Peate* + + ## Rails 7.0.0.alpha2 (September 15, 2021) ## * No changes. diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index 85b00ee90b9d2..a9943a8838a41 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -222,15 +222,24 @@ def human(rule, replacement) # Clears the loaded inflections within a given scope (default is # :all). Give the scope as a symbol of the inflection type, the # options are: :plurals, :singulars, :uncountables, - # :humans. + # :humans, :acronyms. # # clear :all # clear :plurals def clear(scope = :all) case scope when :all - @plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, [] - else + clear(:acronyms) + clear(:plurals) + clear(:singulars) + clear(:uncountables) + clear(:humans) + when :acronyms + @acronyms = {} + define_acronym_regex_patterns + when :uncountables + @uncountables = Uncountables.new + when :plurals, :singulars, :humans instance_variable_set "@#{scope}", [] end end diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 5aac45a9481f9..7814326e0026f 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -469,6 +469,18 @@ def test_clear_#{inflection_type} RUBY end + def test_clear_acronyms_resets_to_reusable_state + ActiveSupport::Inflector.inflections.clear(:acronyms) + + assert_empty ActiveSupport::Inflector.inflections.acronyms + + ActiveSupport::Inflector.inflections do |inflect| + inflect.acronym "HTML" + end + + assert_equal "HTML", "html".titleize + end + def test_inflector_locality ActiveSupport::Inflector.inflections(:es) do |inflect| inflect.plural(/$/, "s") @@ -512,6 +524,7 @@ def test_clear_all inflect.singular(/(database)s$/i, '\1') inflect.uncountable("series") inflect.human("col_rpted_bugs", "Reported bugs") + inflect.acronym("HTML") inflect.clear :all @@ -519,6 +532,7 @@ def test_clear_all assert_empty inflect.singulars assert_empty inflect.uncountables assert_empty inflect.humans + assert_empty inflect.acronyms end end @@ -529,6 +543,7 @@ def test_clear_with_default inflect.singular(/(database)s$/i, '\1') inflect.uncountable("series") inflect.human("col_rpted_bugs", "Reported bugs") + inflect.acronym("HTML") inflect.clear @@ -536,6 +551,22 @@ def test_clear_with_default assert_empty inflect.singulars assert_empty inflect.uncountables assert_empty inflect.humans + assert_empty inflect.acronyms + end + end + + def test_clear_all_resets_camelize_and_underscore_regexes + ActiveSupport::Inflector.inflections do |inflect| + # ensure any data is present + inflect.acronym("HTTP") + assert_equal "http_s", "HTTPS".underscore + assert_equal "Https", "https".camelize + + inflect.clear :all + + assert_empty inflect.acronyms + assert_equal "https", "HTTPS".underscore + assert_equal "Https", "https".camelize end end @@ -592,7 +623,7 @@ def test_clear_with_default end end - %w(plurals singulars uncountables humans acronyms).each do |scope| + %i(plurals singulars uncountables humans).each do |scope| define_method("test_clear_inflections_with_#{scope}") do # clear the inflections ActiveSupport::Inflector.inflections do |inflect| @@ -601,4 +632,11 @@ def test_clear_with_default end end end + + def test_clear_inflections_with_acronyms + ActiveSupport::Inflector.inflections do |inflect| + inflect.clear(:acronyms) + assert_equal({}, inflect.acronyms) + end + end end