mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Move attributes cache method out of concern
This commit is contained in:
parent
6b1a487e00
commit
c2dccbac5f
@ -379,8 +379,7 @@ module ActiveModel
|
|||||||
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
|
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
|
||||||
adapter_options ||= {}
|
adapter_options ||= {}
|
||||||
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
||||||
cached_attributes = adapter_options[:cached_attributes] ||= {}
|
resource = attributes_hash(adapter_options, options, adapter_instance)
|
||||||
resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
|
|
||||||
relationships = resource_relationships(adapter_options, options, adapter_instance)
|
relationships = resource_relationships(adapter_options, options, adapter_instance)
|
||||||
resource.merge(relationships)
|
resource.merge(relationships)
|
||||||
end
|
end
|
||||||
@ -412,6 +411,17 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
def attributes_hash(_adapter_options, options, adapter_instance)
|
||||||
|
if self.class.cache_enabled?
|
||||||
|
fetch_attributes(options[:fields], options[:cached_attributes] || {}, adapter_instance)
|
||||||
|
elsif self.class.fragment_cache_enabled?
|
||||||
|
fetch_attributes_fragment(adapter_instance, options[:cached_attributes] || {})
|
||||||
|
else
|
||||||
|
attributes(options[:fields], true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
def resource_relationships(adapter_options, options, adapter_instance)
|
def resource_relationships(adapter_options, options, adapter_instance)
|
||||||
relationships = {}
|
relationships = {}
|
||||||
|
|||||||
@ -170,6 +170,7 @@ module ActiveModel
|
|||||||
|
|
||||||
# Read cache from cache_store
|
# Read cache from cache_store
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
|
# Used in CollectionSerializer to set :cached_attributes
|
||||||
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
|
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
|
||||||
return {} if ActiveModelSerializers.config.cache_store.blank?
|
return {} if ActiveModelSerializers.config.cache_store.blank?
|
||||||
|
|
||||||
@ -215,23 +216,17 @@ module ActiveModel
|
|||||||
|
|
||||||
### INSTANCE METHODS
|
### INSTANCE METHODS
|
||||||
def fetch_attributes(fields, cached_attributes, adapter_instance)
|
def fetch_attributes(fields, cached_attributes, adapter_instance)
|
||||||
if serializer_class.cache_enabled?
|
|
||||||
key = cache_key(adapter_instance)
|
key = cache_key(adapter_instance)
|
||||||
cached_attributes.fetch(key) do
|
cached_attributes.fetch(key) do
|
||||||
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
fetch(adapter_instance, serializer_class._cache_options, key) do
|
||||||
attributes(fields, true)
|
attributes(fields, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elsif serializer_class.fragment_cache_enabled?
|
|
||||||
fetch_attributes_fragment(adapter_instance, cached_attributes)
|
|
||||||
else
|
|
||||||
attributes(fields, true)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch(adapter_instance, cache_options = serializer_class._cache_options)
|
def fetch(adapter_instance, cache_options = serializer_class._cache_options, key = cache_key(adapter_instance))
|
||||||
if serializer_class.cache_store
|
if serializer_class.cache_store
|
||||||
serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
|
serializer_class.cache_store.fetch(key, cache_options) do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@ -242,7 +237,6 @@ module ActiveModel
|
|||||||
# 1. Determine cached fields from serializer class options
|
# 1. Determine cached fields from serializer class options
|
||||||
# 2. Get non_cached_fields and fetch cache_fields
|
# 2. Get non_cached_fields and fetch cache_fields
|
||||||
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
||||||
# rubocop:disable Metrics/AbcSize
|
|
||||||
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
|
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
|
||||||
serializer_class._cache_options ||= {}
|
serializer_class._cache_options ||= {}
|
||||||
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
|
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
|
||||||
@ -257,7 +251,7 @@ module ActiveModel
|
|||||||
key = cache_key(adapter_instance)
|
key = cache_key(adapter_instance)
|
||||||
cached_hash =
|
cached_hash =
|
||||||
cached_attributes.fetch(key) do
|
cached_attributes.fetch(key) do
|
||||||
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
fetch(adapter_instance, serializer_class._cache_options, key) do
|
||||||
hash = attributes(cached_fields, true)
|
hash = attributes(cached_fields, true)
|
||||||
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
||||||
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
||||||
@ -266,7 +260,6 @@ module ActiveModel
|
|||||||
# Merge both results
|
# Merge both results
|
||||||
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
|
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
|
||||||
end
|
end
|
||||||
# rubocop:enable Metrics/AbcSize
|
|
||||||
|
|
||||||
def cache_key(adapter_instance)
|
def cache_key(adapter_instance)
|
||||||
return @cache_key if defined?(@cache_key)
|
return @cache_key if defined?(@cache_key)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user