Skip to content

Commit a76344f

Browse files
committed
Merge PR rails#42475
2 parents 7e738a5 + a8ae85a commit a76344f

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

activesupport/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* `ActiveSupport::Inflector::Inflections#clear(:acronyms)` is now supported,
2+
and `inflector.clear` / `inflector.clear(:all)` also clears acronyms.
3+
4+
*Alex Ghiculescu*, *Oliver Peate*
5+
6+
17
## Rails 7.0.0.alpha2 (September 15, 2021) ##
28

39
* No changes.

activesupport/lib/active_support/inflector/inflections.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,24 @@ def human(rule, replacement)
222222
# Clears the loaded inflections within a given scope (default is
223223
# <tt>:all</tt>). Give the scope as a symbol of the inflection type, the
224224
# options are: <tt>:plurals</tt>, <tt>:singulars</tt>, <tt>:uncountables</tt>,
225-
# <tt>:humans</tt>.
225+
# <tt>:humans</tt>, <tt>:acronyms</tt>.
226226
#
227227
# clear :all
228228
# clear :plurals
229229
def clear(scope = :all)
230230
case scope
231231
when :all
232-
@plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, []
233-
else
232+
clear(:acronyms)
233+
clear(:plurals)
234+
clear(:singulars)
235+
clear(:uncountables)
236+
clear(:humans)
237+
when :acronyms
238+
@acronyms = {}
239+
define_acronym_regex_patterns
240+
when :uncountables
241+
@uncountables = Uncountables.new
242+
when :plurals, :singulars, :humans
234243
instance_variable_set "@#{scope}", []
235244
end
236245
end

activesupport/test/inflector_test.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,18 @@ def test_clear_#{inflection_type}
469469
RUBY
470470
end
471471

472+
def test_clear_acronyms_resets_to_reusable_state
473+
ActiveSupport::Inflector.inflections.clear(:acronyms)
474+
475+
assert_empty ActiveSupport::Inflector.inflections.acronyms
476+
477+
ActiveSupport::Inflector.inflections do |inflect|
478+
inflect.acronym "HTML"
479+
end
480+
481+
assert_equal "HTML", "html".titleize
482+
end
483+
472484
def test_inflector_locality
473485
ActiveSupport::Inflector.inflections(:es) do |inflect|
474486
inflect.plural(/$/, "s")
@@ -512,13 +524,15 @@ def test_clear_all
512524
inflect.singular(/(database)s$/i, '\1')
513525
inflect.uncountable("series")
514526
inflect.human("col_rpted_bugs", "Reported bugs")
527+
inflect.acronym("HTML")
515528

516529
inflect.clear :all
517530

518531
assert_empty inflect.plurals
519532
assert_empty inflect.singulars
520533
assert_empty inflect.uncountables
521534
assert_empty inflect.humans
535+
assert_empty inflect.acronyms
522536
end
523537
end
524538

@@ -529,13 +543,30 @@ def test_clear_with_default
529543
inflect.singular(/(database)s$/i, '\1')
530544
inflect.uncountable("series")
531545
inflect.human("col_rpted_bugs", "Reported bugs")
546+
inflect.acronym("HTML")
532547

533548
inflect.clear
534549

535550
assert_empty inflect.plurals
536551
assert_empty inflect.singulars
537552
assert_empty inflect.uncountables
538553
assert_empty inflect.humans
554+
assert_empty inflect.acronyms
555+
end
556+
end
557+
558+
def test_clear_all_resets_camelize_and_underscore_regexes
559+
ActiveSupport::Inflector.inflections do |inflect|
560+
# ensure any data is present
561+
inflect.acronym("HTTP")
562+
assert_equal "http_s", "HTTPS".underscore
563+
assert_equal "Https", "https".camelize
564+
565+
inflect.clear :all
566+
567+
assert_empty inflect.acronyms
568+
assert_equal "https", "HTTPS".underscore
569+
assert_equal "Https", "https".camelize
539570
end
540571
end
541572

@@ -592,7 +623,7 @@ def test_clear_with_default
592623
end
593624
end
594625

595-
%w(plurals singulars uncountables humans acronyms).each do |scope|
626+
%i(plurals singulars uncountables humans).each do |scope|
596627
define_method("test_clear_inflections_with_#{scope}") do
597628
# clear the inflections
598629
ActiveSupport::Inflector.inflections do |inflect|
@@ -601,4 +632,11 @@ def test_clear_with_default
601632
end
602633
end
603634
end
635+
636+
def test_clear_inflections_with_acronyms
637+
ActiveSupport::Inflector.inflections do |inflect|
638+
inflect.clear(:acronyms)
639+
assert_equal({}, inflect.acronyms)
640+
end
641+
end
604642
end

0 commit comments

Comments
 (0)