1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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.
This commit is contained in:
Oli Peate 2021-06-14 11:28:52 +01:00
parent 051b0fcd29
commit a8ae85a331
3 changed files with 56 additions and 4 deletions

View file

@ -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. `""`).

View file

@ -222,15 +222,24 @@ module ActiveSupport
# Clears the loaded inflections within a given scope (default is
# <tt>:all</tt>). Give the scope as a symbol of the inflection type, the
# options are: <tt>:plurals</tt>, <tt>:singulars</tt>, <tt>:uncountables</tt>,
# <tt>:humans</tt>.
# <tt>:humans</tt>, <tt>:acronyms</tt>.
#
# 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

View file

@ -469,6 +469,18 @@ class InflectorTest < ActiveSupport::TestCase
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 @@ class InflectorTest < ActiveSupport::TestCase
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 @@ class InflectorTest < ActiveSupport::TestCase
assert_empty inflect.singulars
assert_empty inflect.uncountables
assert_empty inflect.humans
assert_empty inflect.acronyms
end
end
@ -529,6 +543,7 @@ class InflectorTest < ActiveSupport::TestCase
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 @@ class InflectorTest < ActiveSupport::TestCase
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 @@ class InflectorTest < ActiveSupport::TestCase
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 @@ class InflectorTest < ActiveSupport::TestCase
end
end
end
def test_clear_inflections_with_acronyms
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear(:acronyms)
assert_equal({}, inflect.acronyms)
end
end
end