Skip to content

Commit cc80eba

Browse files
committed
Refactor fragment cache logic some more
1 parent 06636b2 commit cc80eba

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

lib/active_model/serializer/caching.rb

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ def _skip_digest?
6363
_cache_options && _cache_options[:skip_digest]
6464
end
6565

66+
def cached_attributes
67+
_cache_only ? _cache_only : _attributes - _cache_except
68+
end
69+
70+
def non_cached_attributes
71+
_attributes - cached_attributes
72+
end
73+
6674
# @api private
6775
# Used by FragmentCache on the CachedSerializer
6876
# to call attribute methods on the fragmented cached serializer.
@@ -245,33 +253,14 @@ def fetch_fragment_cache(adapter_instance)
245253
self.class._cache_options ||= {}
246254
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
247255

248-
attributes = self.class._attributes
249-
cache_only = self.class._cache_only
250-
cached_attributes = cache_only ? cache_only : attributes - self.class._cache_except
251-
non_cached_attributes = attributes - cached_attributes
252-
attributes_keys = self.class._attributes_keys
253-
254-
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
255-
cached_serializer.cache(self.class._cache_options)
256-
cached_serializer.type(self.class._type)
257-
cached_serializer.fragmented(self)
258-
cached_attributes.each do |attribute|
259-
options = attributes_keys[attribute] || {}
260-
cached_serializer.attribute(attribute, options)
261-
end
256+
cached_serializer = _get_or_create_fragment_cached_serializer(serializer_class_name)
262257
cached_hash = ActiveModelSerializers::SerializableResource.new(
263258
object,
264259
serializer: cached_serializer,
265260
adapter: adapter_instance.class
266261
).serializable_hash
267262

268-
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
269-
non_cached_serializer.type(self.class._type)
270-
non_cached_serializer.fragmented(self)
271-
non_cached_attributes.each do |attribute|
272-
options = attributes_keys[attribute] || {}
273-
non_cached_serializer.attribute(attribute, options)
274-
end
263+
non_cached_serializer = _get_or_create_fragment_non_cached_serializer(serializer_class_name)
275264
non_cached_hash = ActiveModelSerializers::SerializableResource.new(
276265
object,
277266
serializer: non_cached_serializer,
@@ -282,6 +271,29 @@ def fetch_fragment_cache(adapter_instance)
282271
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
283272
end
284273

274+
def _get_or_create_fragment_cached_serializer(serializer_class_name)
275+
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
276+
cached_serializer.cache(self.class._cache_options)
277+
cached_serializer.type(self.class._type)
278+
cached_serializer.fragmented(self)
279+
self.class.cached_attributes.each do |attribute|
280+
options = self.class._attributes_keys[attribute] || {}
281+
cached_serializer.attribute(attribute, options)
282+
end
283+
cached_serializer
284+
end
285+
286+
def _get_or_create_fragment_non_cached_serializer(serializer_class_name)
287+
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
288+
non_cached_serializer.type(self.class._type)
289+
non_cached_serializer.fragmented(self)
290+
self.class.non_cached_attributes.each do |attribute|
291+
options = self.class._attributes_keys[attribute] || {}
292+
non_cached_serializer.attribute(attribute, options)
293+
end
294+
non_cached_serializer
295+
end
296+
285297
def _get_or_create_fragment_serializer(name)
286298
return Object.const_get(name) if Object.const_defined?(name)
287299
Object.const_set(name, Class.new(ActiveModel::Serializer))

0 commit comments

Comments
 (0)