Skip to content

Commit

Permalink
Merge PR #42475
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Sep 20, 2021
2 parents 7e738a5 + a8ae85a commit a76344f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
6 changes: 6 additions & 0 deletions 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.
Expand Down
15 changes: 12 additions & 3 deletions activesupport/lib/active_support/inflector/inflections.rb
Expand Up @@ -222,15 +222,24 @@ def human(rule, replacement)
# 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
Expand Down
40 changes: 39 additions & 1 deletion activesupport/test/inflector_test.rb
Expand Up @@ -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")
Expand Down Expand Up @@ -512,13 +524,15 @@ 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

assert_empty inflect.plurals
assert_empty inflect.singulars
assert_empty inflect.uncountables
assert_empty inflect.humans
assert_empty inflect.acronyms
end
end

Expand All @@ -529,13 +543,30 @@ 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

assert_empty inflect.plurals
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

Expand Down Expand Up @@ -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|
Expand All @@ -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

0 comments on commit a76344f

Please sign in to comment.