Skip to content

Commit c3d43a8

Browse files
committed
Inflections cleanup
1. Don’t use weird instance variable names 2. Don’t reach into the internals from the test (accessing ivars) 3. Properly reset the inflection rules after each test (1) and (2) is to establish a clear boundary to make future refactors easier. (3) is to fix a bug where we modify the inflection rules and don’t reset them properly after each test. We only attempt to do that for the `:en` locale, but we have tests that modifies other locales that doesn’t get cleaned up properly. For better or worse, the current API for loading the default rules is to load `inflections.rb`, so let’s just do that and reset the rules after each test.
1 parent 5f9d6f5 commit c3d43a8

File tree

3 files changed

+21
-39
lines changed

3 files changed

+21
-39
lines changed

actionpack/test/controller/params_wrapper_test.rb

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,27 +339,20 @@ def parse
339339

340340
tests ParamswrappernewsController
341341

342-
def test_uses_model_attribute_names_with_irregular_inflection
343-
with_dup do
344-
ActiveSupport::Inflector.inflections do |inflect|
345-
inflect.irregular "paramswrappernews_item", "paramswrappernews"
346-
end
347-
348-
with_default_wrapper_options do
349-
@request.env["CONTENT_TYPE"] = "application/json"
350-
post :parse, params: { "username" => "sikachu", "test_attr" => "test_value" }
351-
assert_parameters("username" => "sikachu", "test_attr" => "test_value", "paramswrappernews_item" => { "test_attr" => "test_value" })
352-
end
353-
end
342+
def teardown
343+
ActiveSupport::Inflector::Inflections.clear!
344+
load "active_support/inflections.rb"
354345
end
355346

356-
private
347+
def test_uses_model_attribute_names_with_irregular_inflection
348+
ActiveSupport::Inflector.inflections do |inflect|
349+
inflect.irregular "paramswrappernews_item", "paramswrappernews"
350+
end
357351

358-
def with_dup
359-
original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
360-
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original.dup)
361-
yield
362-
ensure
363-
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original)
352+
with_default_wrapper_options do
353+
@request.env["CONTENT_TYPE"] = "application/json"
354+
post :parse, params: { "username" => "sikachu", "test_attr" => "test_value" }
355+
assert_parameters("username" => "sikachu", "test_attr" => "test_value", "paramswrappernews_item" => { "test_attr" => "test_value" })
364356
end
357+
end
365358
end

activesupport/lib/active_support/inflector/inflections.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Inflector
2424
# singularization rules that is runs. This guarantees that your rules run
2525
# before any of the rules that may already have been loaded.
2626
class Inflections
27-
@__instance__ = {}
27+
@instances = {}
2828

2929
class Uncountables < Array
3030
def initialize
@@ -58,7 +58,11 @@ def to_regex(string)
5858
end
5959

6060
def self.instance(locale = :en)
61-
@__instance__[locale] ||= new
61+
@instances[locale] ||= new
62+
end
63+
64+
def self.clear! # :nodoc:
65+
@instances = {}
6266
end
6367

6468
attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex
@@ -67,13 +71,6 @@ def initialize
6771
@plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], Uncountables.new, [], {}, /(?=a)b/
6872
end
6973

70-
# Private, for the test suite.
71-
def initialize_dup(orig) # :nodoc:
72-
%w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope|
73-
instance_variable_set("@#{scope}", orig.send(scope).dup)
74-
end
75-
end
76-
7774
# Specifies a new acronym. An acronym must be specified as it will appear
7875
# in a camelized string. An underscore string that contains the acronym
7976
# will retain the acronym when passed to +camelize+, +humanize+, or

activesupport/test/inflector_test.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@ class InflectorTest < ActiveSupport::TestCase
88
include InflectorTestCases
99
include ConstantizeTestCases
1010

11-
def setup
12-
# Dups the singleton before each test, restoring the original inflections later.
13-
#
14-
# This helper is implemented by setting @__instance__ because in some tests
15-
# there are module functions that access ActiveSupport::Inflector.inflections,
16-
# so we need to replace the singleton itself.
17-
@original_inflections = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
18-
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections.dup)
19-
end
20-
2111
def teardown
22-
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: @original_inflections)
12+
# Restore the default rules
13+
ActiveSupport::Inflector::Inflections.clear!
14+
load "active_support/inflections.rb"
2315
end
2416

2517
def test_pluralize_plurals

0 commit comments

Comments
 (0)