@@ -63,6 +63,14 @@ def _skip_digest?
63
63
_cache_options && _cache_options [ :skip_digest ]
64
64
end
65
65
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
+
66
74
# @api private
67
75
# Used by FragmentCache on the CachedSerializer
68
76
# to call attribute methods on the fragmented cached serializer.
@@ -244,33 +252,14 @@ def fetch_fragment_cache(adapter_instance)
244
252
self . class . _cache_options ||= { }
245
253
self . class . _cache_options [ :key ] = self . class . _cache_key if self . class . _cache_key
246
254
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 )
261
256
cached_hash = ActiveModelSerializers ::SerializableResource . new (
262
257
object ,
263
258
serializer : cached_serializer ,
264
259
adapter : adapter_instance . class
265
260
) . serializable_hash
266
261
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 )
274
263
non_cached_hash = ActiveModelSerializers ::SerializableResource . new (
275
264
object ,
276
265
serializer : non_cached_serializer ,
@@ -281,6 +270,29 @@ def fetch_fragment_cache(adapter_instance)
281
270
adapter_instance . fragment_cache ( cached_hash , non_cached_hash )
282
271
end
283
272
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
+
284
296
def _get_or_create_fragment_serializer ( name )
285
297
return Object . const_get ( name ) if Object . const_defined? ( name )
286
298
Object . const_set ( name , Class . new ( ActiveModel ::Serializer ) )
0 commit comments