diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index aa35a2726e53c..26ee9c43151e9 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -1,3 +1,6 @@ +require 'active_support/core_ext/hash/keys' +require 'active_support/core_ext/class/inheritable_attributes' + module ActiveModel class MissingAttributeError < NoMethodError end @@ -219,7 +222,7 @@ def method_missing_target end def attribute_method_matchers #:nodoc: - @@attribute_method_matchers ||= [] + read_inheritable_attribute(:attribute_method_matchers) || write_inheritable_attribute(:attribute_method_matchers, []) end end diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb new file mode 100644 index 0000000000000..614c4a3645ca3 --- /dev/null +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -0,0 +1,20 @@ +require 'cases/helper' + +class ModelWithAttributes + include ActiveModel::AttributeMethods + + attribute_method_suffix '' +end + +class ModelWithAttributes2 + include ActiveModel::AttributeMethods + + attribute_method_suffix '_test' +end + +class AttributeMethodsTest < ActiveModel::TestCase + test 'unrelated classes should not share attribute method matchers' do + assert_not_equal ModelWithAttributes.send(:attribute_method_matchers), + ModelWithAttributes2.send(:attribute_method_matchers) + end +end