diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index ea24ce52..a9ecb1e3 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -379,8 +379,7 @@ module ActiveModel def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance) adapter_options ||= {} options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options) - cached_attributes = adapter_options[:cached_attributes] ||= {} - resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance) + resource = attributes_hash(adapter_options, options, adapter_instance) relationships = resource_relationships(adapter_options, options, adapter_instance) resource.merge(relationships) end @@ -412,6 +411,17 @@ module ActiveModel 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 def resource_relationships(adapter_options, options, adapter_instance) relationships = {} diff --git a/lib/active_model/serializer/concerns/caching.rb b/lib/active_model/serializer/concerns/caching.rb index 3238adc8..69900001 100644 --- a/lib/active_model/serializer/concerns/caching.rb +++ b/lib/active_model/serializer/concerns/caching.rb @@ -170,6 +170,7 @@ module ActiveModel # Read cache from cache_store # @return [Hash] + # Used in CollectionSerializer to set :cached_attributes def cache_read_multi(collection_serializer, adapter_instance, include_directive) return {} if ActiveModelSerializers.config.cache_store.blank? @@ -215,23 +216,17 @@ module ActiveModel ### INSTANCE METHODS def fetch_attributes(fields, cached_attributes, adapter_instance) - if serializer_class.cache_enabled? - key = cache_key(adapter_instance) - cached_attributes.fetch(key) do - serializer_class.cache_store.fetch(key, serializer_class._cache_options) do - attributes(fields, true) - end + key = cache_key(adapter_instance) + cached_attributes.fetch(key) do + fetch(adapter_instance, serializer_class._cache_options, key) do + attributes(fields, true) end - elsif serializer_class.fragment_cache_enabled? - fetch_attributes_fragment(adapter_instance, cached_attributes) - else - attributes(fields, true) 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 - serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do + serializer_class.cache_store.fetch(key, cache_options) do yield end else @@ -242,7 +237,6 @@ module ActiveModel # 1. Determine cached fields from serializer class options # 2. Get non_cached_fields and fetch cache_fields # 3. Merge the two hashes using adapter_instance#fragment_cache - # rubocop:disable Metrics/AbcSize def fetch_attributes_fragment(adapter_instance, cached_attributes = {}) serializer_class._cache_options ||= {} 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) cached_hash = 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) include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys) hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance) @@ -266,7 +260,6 @@ module ActiveModel # Merge both results adapter_instance.fragment_cache(cached_hash, non_cached_hash) end - # rubocop:enable Metrics/AbcSize def cache_key(adapter_instance) return @cache_key if defined?(@cache_key)