Skip to content

Commit c2dccba

Browse files
committed
Move attributes cache method out of concern
1 parent 6b1a487 commit c2dccba

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

lib/active_model/serializer.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ def associations(include_directive = ActiveModelSerializers.default_include_dire
379379
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
380380
adapter_options ||= {}
381381
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
382-
cached_attributes = adapter_options[:cached_attributes] ||= {}
383-
resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
382+
resource = attributes_hash(adapter_options, options, adapter_instance)
384383
relationships = resource_relationships(adapter_options, options, adapter_instance)
385384
resource.merge(relationships)
386385
end
@@ -412,6 +411,17 @@ def read_attribute_for_serialization(attr)
412411
end
413412
end
414413

414+
# @api private
415+
def attributes_hash(_adapter_options, options, adapter_instance)
416+
if self.class.cache_enabled?
417+
fetch_attributes(options[:fields], options[:cached_attributes] || {}, adapter_instance)
418+
elsif self.class.fragment_cache_enabled?
419+
fetch_attributes_fragment(adapter_instance, options[:cached_attributes] || {})
420+
else
421+
attributes(options[:fields], true)
422+
end
423+
end
424+
415425
# @api private
416426
def resource_relationships(adapter_options, options, adapter_instance)
417427
relationships = {}

lib/active_model/serializer/concerns/caching.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def fragment_cache_enabled?
170170

171171
# Read cache from cache_store
172172
# @return [Hash]
173+
# Used in CollectionSerializer to set :cached_attributes
173174
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
174175
return {} if ActiveModelSerializers.config.cache_store.blank?
175176

@@ -215,23 +216,17 @@ def object_cache_key(serializer, adapter_instance)
215216

216217
### INSTANCE METHODS
217218
def fetch_attributes(fields, cached_attributes, adapter_instance)
218-
if serializer_class.cache_enabled?
219-
key = cache_key(adapter_instance)
220-
cached_attributes.fetch(key) do
221-
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
222-
attributes(fields, true)
223-
end
219+
key = cache_key(adapter_instance)
220+
cached_attributes.fetch(key) do
221+
fetch(adapter_instance, serializer_class._cache_options, key) do
222+
attributes(fields, true)
224223
end
225-
elsif serializer_class.fragment_cache_enabled?
226-
fetch_attributes_fragment(adapter_instance, cached_attributes)
227-
else
228-
attributes(fields, true)
229224
end
230225
end
231226

232-
def fetch(adapter_instance, cache_options = serializer_class._cache_options)
227+
def fetch(adapter_instance, cache_options = serializer_class._cache_options, key = cache_key(adapter_instance))
233228
if serializer_class.cache_store
234-
serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
229+
serializer_class.cache_store.fetch(key, cache_options) do
235230
yield
236231
end
237232
else
@@ -242,7 +237,6 @@ def fetch(adapter_instance, cache_options = serializer_class._cache_options)
242237
# 1. Determine cached fields from serializer class options
243238
# 2. Get non_cached_fields and fetch cache_fields
244239
# 3. Merge the two hashes using adapter_instance#fragment_cache
245-
# rubocop:disable Metrics/AbcSize
246240
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
247241
serializer_class._cache_options ||= {}
248242
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
@@ -257,7 +251,7 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
257251
key = cache_key(adapter_instance)
258252
cached_hash =
259253
cached_attributes.fetch(key) do
260-
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
254+
fetch(adapter_instance, serializer_class._cache_options, key) do
261255
hash = attributes(cached_fields, true)
262256
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
263257
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
@@ -266,7 +260,6 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
266260
# Merge both results
267261
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
268262
end
269-
# rubocop:enable Metrics/AbcSize
270263

271264
def cache_key(adapter_instance)
272265
return @cache_key if defined?(@cache_key)

0 commit comments

Comments
 (0)