Fix inheritance issues, poison cache if attributes change

This commit is contained in:
Sam 2013-03-15 14:13:02 -07:00
parent 46378add65
commit ad1d34b2c4
2 changed files with 36 additions and 0 deletions

View File

@ -105,6 +105,14 @@ module ActiveModel
define_include_method attr define_include_method attr
# protect inheritance chains and open classes
# if a serializer inherits from another OR
# attributes are added later in a classes lifecycle
# poison the cache
define_method :_fast_attributes do
raise NameError
end
end end
def associate(klass, attrs) #:nodoc: def associate(klass, attrs) #:nodoc:

View File

@ -1311,4 +1311,32 @@ class SerializerTest < ActiveModel::TestCase
] ]
}, actual) }, actual)
end end
def test_inheritance_does_not_used_cached_attributes
parent = Class.new(ActiveModel::Serializer) do
attributes :title
end
child = Class.new(parent) do
attributes :body
end
data_class = Class.new do
attr_accessor :title, :body
end
item = data_class.new
item.title = "title"
item.body = "body"
2.times do
assert_equal({:title => "title"},
parent.new(item).attributes)
assert_equal({:body => "body", :title => "title"},
child.new(item).attributes)
end
end
end end