Skip to content

Commit 6e287fd

Browse files
committed
Refactor fragment cache logic some more
1 parent ed86411 commit 6e287fd

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.
@@ -244,33 +252,14 @@ def fetch_fragment_cache(adapter_instance)
244252
self.class._cache_options ||= {}
245253
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
246254

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

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

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

0 commit comments

Comments
 (0)