@@ -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.
@@ -245,33 +253,14 @@ def fetch_fragment_cache(adapter_instance)
245
253
self . class . _cache_options ||= { }
246
254
self . class . _cache_options [ :key ] = self . class . _cache_key if self . class . _cache_key
247
255
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 )
262
257
cached_hash = ActiveModelSerializers ::SerializableResource . new (
263
258
object ,
264
259
serializer : cached_serializer ,
265
260
adapter : adapter_instance . class
266
261
) . serializable_hash
267
262
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 )
275
264
non_cached_hash = ActiveModelSerializers ::SerializableResource . new (
276
265
object ,
277
266
serializer : non_cached_serializer ,
@@ -282,6 +271,29 @@ def fetch_fragment_cache(adapter_instance)
282
271
adapter_instance . fragment_cache ( cached_hash , non_cached_hash )
283
272
end
284
273
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
+
285
297
def _get_or_create_fragment_serializer ( name )
286
298
return Object . const_get ( name ) if Object . const_defined? ( name )
287
299
Object . const_set ( name , Class . new ( ActiveModel ::Serializer ) )
0 commit comments