mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1276 from kieran/0-8-stable
[FIX] serializers were never including associations on cache hits
This commit is contained in:
commit
e54d129091
@ -353,13 +353,16 @@ module ActiveModel
|
|||||||
# Returns a hash representation of the serializable
|
# Returns a hash representation of the serializable
|
||||||
# object without the root.
|
# object without the root.
|
||||||
def serializable_hash
|
def serializable_hash
|
||||||
if perform_caching?
|
@node = if perform_caching?
|
||||||
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do
|
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do
|
||||||
_serializable_hash
|
_serializable_hash
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
_serializable_hash
|
_serializable_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
include_associations! if @object.present? && _embed
|
||||||
|
@node
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_associations!
|
def include_associations!
|
||||||
@ -476,9 +479,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def _serializable_hash
|
def _serializable_hash
|
||||||
return nil if @object.nil?
|
return nil if @object.nil?
|
||||||
@node = attributes
|
attributes
|
||||||
include_associations! if _embed
|
|
||||||
@node
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_caching?
|
def perform_caching?
|
||||||
|
|||||||
@ -35,6 +35,42 @@ class CachingTest < ActiveModel::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Parent
|
||||||
|
def id
|
||||||
|
'parent1'
|
||||||
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
'Kieran'
|
||||||
|
end
|
||||||
|
|
||||||
|
def children
|
||||||
|
[ Child.new ]
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
send name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Child
|
||||||
|
def id
|
||||||
|
'child1'
|
||||||
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
'Joshua'
|
||||||
|
end
|
||||||
|
|
||||||
|
def parent
|
||||||
|
Parent.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
send name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_serializers_have_a_cache_store
|
def test_serializers_have_a_cache_store
|
||||||
ActiveModel::Serializer.cache = NullStore.new
|
ActiveModel::Serializer.cache = NullStore.new
|
||||||
|
|
||||||
@ -93,4 +129,49 @@ class CachingTest < ActiveModel::TestCase
|
|||||||
assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serializable-array')
|
assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serializable-array')
|
||||||
assert_equal instance.to_json, serializer.cache.read('array_serializer/cache-key/to-json')
|
assert_equal instance.to_json, serializer.cache.read('array_serializer/cache-key/to-json')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_cached_serializers_return_associations
|
||||||
|
|
||||||
|
child_serializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
cached true
|
||||||
|
attributes :name
|
||||||
|
|
||||||
|
def self.to_s
|
||||||
|
'child_serializer'
|
||||||
|
end
|
||||||
|
|
||||||
|
def cache_key
|
||||||
|
object.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
parent_serializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
cached true
|
||||||
|
attributes :name
|
||||||
|
|
||||||
|
has_many :children, serializer: child_serializer, embed: :ids, include: true
|
||||||
|
|
||||||
|
def self.to_s
|
||||||
|
'parent_serializer'
|
||||||
|
end
|
||||||
|
|
||||||
|
def cache_key
|
||||||
|
object.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
parent_serializer.cache = NullStore.new
|
||||||
|
child_serializer.cache = NullStore.new
|
||||||
|
|
||||||
|
instance = parent_serializer.new Parent.new, root: :parent
|
||||||
|
|
||||||
|
initial_keys = instance.as_json.keys
|
||||||
|
|
||||||
|
assert_equal(initial_keys, [:children, :parent])
|
||||||
|
|
||||||
|
cached_keys = instance.as_json.keys
|
||||||
|
|
||||||
|
assert_equal(cached_keys, [:children, :parent])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user