Skip to content

Commit 06636b2

Browse files
committed
Begin simplifying fragment cache
1 parent 1e10c20 commit 06636b2

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

lib/active_model/serializer/caching.rb

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -216,65 +216,62 @@ def cache_check(adapter_instance)
216216
# 1. Create a CachedSerializer and NonCachedSerializer from the serializer class
217217
# 2. Serialize the above two with the given adapter
218218
# 3. Pass their serializations to the adapter +::fragment_cache+
219+
#
220+
# It will split the serializer into two, one that will be cached and one that will not
221+
#
222+
# Given a resource name
223+
# 1. Dynamically creates a CachedSerializer and NonCachedSerializer
224+
# for a given class 'name'
225+
# 2. Call
226+
# CachedSerializer.cache(serializer._cache_options)
227+
# CachedSerializer.fragmented(serializer)
228+
# NonCachedSerializer.cache(serializer._cache_options)
229+
# 3. Build a hash keyed to the +cached+ and +non_cached+ serializers
230+
# 4. Call +cached_attributes+ on the serializer class and the above hash
231+
# 5. Return the hash
232+
#
233+
# @example
234+
# When +name+ is <tt>User::Admin</tt>
235+
# creates the Serializer classes (if they don't exist).
236+
# CachedUser_AdminSerializer
237+
# NonCachedUser_AdminSerializer
238+
#
239+
# Given a hash of its cached and non-cached serializers
240+
# 1. Determine cached attributes from serializer class options
241+
# 2. Add cached attributes to cached Serializer
242+
# 3. Add non-cached attributes to non-cached Serializer
219243
def fetch_fragment_cache(adapter_instance)
220-
# It will split the serializer into two, one that will be cached and one that will not
221-
222-
# Given a resource name
223-
# 1. Dynamically creates a CachedSerializer and NonCachedSerializer
224-
# for a given class 'name'
225-
# 2. Call
226-
# CachedSerializer.cache(serializer._cache_options)
227-
# CachedSerializer.fragmented(serializer)
228-
# NonCachedSerializer.cache(serializer._cache_options)
229-
# 3. Build a hash keyed to the +cached+ and +non_cached+ serializers
230-
# 4. Call +cached_attributes+ on the serializer class and the above hash
231-
# 5. Return the hash
232-
#
233-
# @example
234-
# When +name+ is <tt>User::Admin</tt>
235-
# creates the Serializer classes (if they don't exist).
236-
# CachedUser_AdminSerializer
237-
# NonCachedUser_AdminSerializer
238-
#
239244
serializer_class_name = self.class.name.gsub('::'.freeze, '_'.freeze)
240-
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
241-
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
242-
243245
self.class._cache_options ||= {}
244246
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
245-
cached_serializer.cache(self.class._cache_options)
246247

247-
cached_serializer.type(self.class._type)
248-
non_cached_serializer.type(self.class._type)
249-
250-
non_cached_serializer.fragmented(self)
251-
cached_serializer.fragmented(self)
252-
253-
# Given a hash of its cached and non-cached serializers
254-
# 1. Determine cached attributes from serializer class options
255-
# 2. Add cached attributes to cached Serializer
256-
# 3. Add non-cached attributes to non-cached Serializer
257248
attributes = self.class._attributes
258249
cache_only = self.class._cache_only
259250
cached_attributes = cache_only ? cache_only : attributes - self.class._cache_except
260251
non_cached_attributes = attributes - cached_attributes
261252
attributes_keys = self.class._attributes_keys
262253

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)
263258
cached_attributes.each do |attribute|
264259
options = attributes_keys[attribute] || {}
265260
cached_serializer.attribute(attribute, options)
266261
end
267-
non_cached_attributes.each do |attribute|
268-
options = attributes_keys[attribute] || {}
269-
non_cached_serializer.attribute(attribute, options)
270-
end
271-
272-
# Get serializable hash from both
273262
cached_hash = ActiveModelSerializers::SerializableResource.new(
274263
object,
275264
serializer: cached_serializer,
276265
adapter: adapter_instance.class
277266
).serializable_hash
267+
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
278275
non_cached_hash = ActiveModelSerializers::SerializableResource.new(
279276
object,
280277
serializer: non_cached_serializer,

0 commit comments

Comments
 (0)