From a8ae85a33191a0f22669a3c13fbfaa12877af084 Mon Sep 17 00:00:00 2001 From: Oli Peate Date: Mon, 14 Jun 2021 11:28:52 +0100 Subject: [PATCH] Support clearing acronyms in ActiveSupport::Inflector::Inflections Previously calling ActiveSupport::Inflector::Inflections.clear(:acronyms) reset the instance variable to an empty Array, which is not the correct default. The next time an acronym is added a TypeError is thrown. --- activesupport/CHANGELOG.md | 5 +++ .../active_support/inflector/inflections.rb | 15 +++++-- activesupport/test/inflector_test.rb | 40 ++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 489842d44b0d9..6c0e344fc4343 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* `ActiveSupport::Inflector::Inflections#clear(:acronyms)` is now supported, + and `inflector.clear` / `inflector.clear(:all)` also clears acronyms. + + *Alex Ghiculescu*, *Oliver Peate* + * Raise `ActiveSupport::EncryptedFile::MissingKeyError` when the `RAILS_MASTER_KEY` environment variable is blank (e.g. `""`). diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index f1151e8c94d3e..4d909bb220cca 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