mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Begin simplifying fragment cache
This commit is contained in:
parent
1e10c20ac0
commit
06636b25b2
@ -216,9 +216,9 @@ module ActiveModel
|
|||||||
# 1. Create a CachedSerializer and NonCachedSerializer from the serializer class
|
# 1. Create a CachedSerializer and NonCachedSerializer from the serializer class
|
||||||
# 2. Serialize the above two with the given adapter
|
# 2. Serialize the above two with the given adapter
|
||||||
# 3. Pass their serializations to the adapter +::fragment_cache+
|
# 3. Pass their serializations to the adapter +::fragment_cache+
|
||||||
def fetch_fragment_cache(adapter_instance)
|
#
|
||||||
# It will split the serializer into two, one that will be cached and one that will not
|
# It will split the serializer into two, one that will be cached and one that will not
|
||||||
|
#
|
||||||
# Given a resource name
|
# Given a resource name
|
||||||
# 1. Dynamically creates a CachedSerializer and NonCachedSerializer
|
# 1. Dynamically creates a CachedSerializer and NonCachedSerializer
|
||||||
# for a given class 'name'
|
# for a given class 'name'
|
||||||
@ -236,45 +236,42 @@ module ActiveModel
|
|||||||
# CachedUser_AdminSerializer
|
# CachedUser_AdminSerializer
|
||||||
# NonCachedUser_AdminSerializer
|
# NonCachedUser_AdminSerializer
|
||||||
#
|
#
|
||||||
serializer_class_name = self.class.name.gsub('::'.freeze, '_'.freeze)
|
|
||||||
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
|
|
||||||
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
|
|
||||||
|
|
||||||
self.class._cache_options ||= {}
|
|
||||||
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
|
|
||||||
cached_serializer.cache(self.class._cache_options)
|
|
||||||
|
|
||||||
cached_serializer.type(self.class._type)
|
|
||||||
non_cached_serializer.type(self.class._type)
|
|
||||||
|
|
||||||
non_cached_serializer.fragmented(self)
|
|
||||||
cached_serializer.fragmented(self)
|
|
||||||
|
|
||||||
# Given a hash of its cached and non-cached serializers
|
# Given a hash of its cached and non-cached serializers
|
||||||
# 1. Determine cached attributes from serializer class options
|
# 1. Determine cached attributes from serializer class options
|
||||||
# 2. Add cached attributes to cached Serializer
|
# 2. Add cached attributes to cached Serializer
|
||||||
# 3. Add non-cached attributes to non-cached Serializer
|
# 3. Add non-cached attributes to non-cached Serializer
|
||||||
|
def fetch_fragment_cache(adapter_instance)
|
||||||
|
serializer_class_name = self.class.name.gsub('::'.freeze, '_'.freeze)
|
||||||
|
self.class._cache_options ||= {}
|
||||||
|
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
|
||||||
|
|
||||||
attributes = self.class._attributes
|
attributes = self.class._attributes
|
||||||
cache_only = self.class._cache_only
|
cache_only = self.class._cache_only
|
||||||
cached_attributes = cache_only ? cache_only : attributes - self.class._cache_except
|
cached_attributes = cache_only ? cache_only : attributes - self.class._cache_except
|
||||||
non_cached_attributes = attributes - cached_attributes
|
non_cached_attributes = attributes - cached_attributes
|
||||||
attributes_keys = self.class._attributes_keys
|
attributes_keys = self.class._attributes_keys
|
||||||
|
|
||||||
|
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
|
||||||
|
cached_serializer.cache(self.class._cache_options)
|
||||||
|
cached_serializer.type(self.class._type)
|
||||||
|
cached_serializer.fragmented(self)
|
||||||
cached_attributes.each do |attribute|
|
cached_attributes.each do |attribute|
|
||||||
options = attributes_keys[attribute] || {}
|
options = attributes_keys[attribute] || {}
|
||||||
cached_serializer.attribute(attribute, options)
|
cached_serializer.attribute(attribute, options)
|
||||||
end
|
end
|
||||||
non_cached_attributes.each do |attribute|
|
|
||||||
options = attributes_keys[attribute] || {}
|
|
||||||
non_cached_serializer.attribute(attribute, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Get serializable hash from both
|
|
||||||
cached_hash = ActiveModelSerializers::SerializableResource.new(
|
cached_hash = ActiveModelSerializers::SerializableResource.new(
|
||||||
object,
|
object,
|
||||||
serializer: cached_serializer,
|
serializer: cached_serializer,
|
||||||
adapter: adapter_instance.class
|
adapter: adapter_instance.class
|
||||||
).serializable_hash
|
).serializable_hash
|
||||||
|
|
||||||
|
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
|
||||||
|
non_cached_serializer.type(self.class._type)
|
||||||
|
non_cached_serializer.fragmented(self)
|
||||||
|
non_cached_attributes.each do |attribute|
|
||||||
|
options = attributes_keys[attribute] || {}
|
||||||
|
non_cached_serializer.attribute(attribute, options)
|
||||||
|
end
|
||||||
non_cached_hash = ActiveModelSerializers::SerializableResource.new(
|
non_cached_hash = ActiveModelSerializers::SerializableResource.new(
|
||||||
object,
|
object,
|
||||||
serializer: non_cached_serializer,
|
serializer: non_cached_serializer,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user