mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Fix inheritance issues, poison cache if attributes change
This commit is contained in:
parent
46378add65
commit
ad1d34b2c4
@ -105,6 +105,14 @@ module ActiveModel
|
||||
|
||||
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
|
||||
|
||||
def associate(klass, attrs) #:nodoc:
|
||||
|
||||
@ -1311,4 +1311,32 @@ class SerializerTest < ActiveModel::TestCase
|
||||
]
|
||||
}, actual)
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user