Skip to content

Commit 3fb72d9

Browse files
authored
Merge pull request #2094 from bf4/move_association_concern_to_association
Move association serialization to association
2 parents 6b1a487 + 6cd6ed7 commit 3fb72d9

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

lib/active_model/serializer.rb

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,8 @@ def associations(include_directive = ActiveModelSerializers.default_include_dire
379379
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
380380
adapter_options ||= {}
381381
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
382-
cached_attributes = adapter_options[:cached_attributes] ||= {}
383-
resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
384-
relationships = resource_relationships(adapter_options, options, adapter_instance)
382+
resource = attributes_hash(adapter_options, options, adapter_instance)
383+
relationships = associations_hash(adapter_options, options, adapter_instance)
385384
resource.merge(relationships)
386385
end
387386
alias to_hash serializable_hash
@@ -413,34 +412,28 @@ def read_attribute_for_serialization(attr)
413412
end
414413

415414
# @api private
416-
def resource_relationships(adapter_options, options, adapter_instance)
415+
def attributes_hash(_adapter_options, options, adapter_instance)
416+
if self.class.cache_enabled?
417+
fetch_attributes(options[:fields], options[:cached_attributes] || {}, adapter_instance)
418+
elsif self.class.fragment_cache_enabled?
419+
fetch_attributes_fragment(adapter_instance, options[:cached_attributes] || {})
420+
else
421+
attributes(options[:fields], true)
422+
end
423+
end
424+
425+
# @api private
426+
def associations_hash(adapter_options, options, adapter_instance)
417427
relationships = {}
418428
include_directive = options.fetch(:include_directive)
419429
associations(include_directive).each do |association|
420430
adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
421-
relationships[association.key] ||= relationship_value_for(association, adapter_opts, adapter_instance)
431+
relationships[association.key] ||= association.serializable_hash(adapter_opts, adapter_instance)
422432
end
423433

424434
relationships
425435
end
426436

427-
# @api private
428-
def relationship_value_for(association, adapter_options, adapter_instance)
429-
return association.options[:virtual_value] if association.options[:virtual_value]
430-
association_serializer = association.serializer
431-
association_object = association_serializer && association_serializer.object
432-
return unless association_object
433-
434-
relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
435-
436-
if association.options[:polymorphic] && relationship_value
437-
polymorphic_type = association_object.class.name.underscore
438-
relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
439-
end
440-
441-
relationship_value
442-
end
443-
444437
protected
445438

446439
attr_accessor :instance_options

lib/active_model/serializer/association.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ def links
2929
def meta
3030
options[:meta]
3131
end
32+
33+
# @api private
34+
def serializable_hash(adapter_options, adapter_instance)
35+
return options[:virtual_value] if options[:virtual_value]
36+
object = serializer && serializer.object
37+
return unless object
38+
39+
serialization = serializer.serializable_hash(adapter_options, {}, adapter_instance)
40+
41+
if options[:polymorphic] && serialization
42+
polymorphic_type = object.class.name.underscore
43+
serialization = { type: polymorphic_type, polymorphic_type.to_sym => serialization }
44+
end
45+
46+
serialization
47+
end
3248
end
3349
end
3450
end

lib/active_model/serializer/concerns/caching.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def fragment_cache_enabled?
170170

171171
# Read cache from cache_store
172172
# @return [Hash]
173+
# Used in CollectionSerializer to set :cached_attributes
173174
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
174175
return {} if ActiveModelSerializers.config.cache_store.blank?
175176

@@ -215,23 +216,17 @@ def object_cache_key(serializer, adapter_instance)
215216

216217
### INSTANCE METHODS
217218
def fetch_attributes(fields, cached_attributes, adapter_instance)
218-
if serializer_class.cache_enabled?
219-
key = cache_key(adapter_instance)
220-
cached_attributes.fetch(key) do
221-
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
222-
attributes(fields, true)
223-
end
219+
key = cache_key(adapter_instance)
220+
cached_attributes.fetch(key) do
221+
fetch(adapter_instance, serializer_class._cache_options, key) do
222+
attributes(fields, true)
224223
end
225-
elsif serializer_class.fragment_cache_enabled?
226-
fetch_attributes_fragment(adapter_instance, cached_attributes)
227-
else
228-
attributes(fields, true)
229224
end
230225
end
231226

232-
def fetch(adapter_instance, cache_options = serializer_class._cache_options)
227+
def fetch(adapter_instance, cache_options = serializer_class._cache_options, key = cache_key(adapter_instance))
233228
if serializer_class.cache_store
234-
serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
229+
serializer_class.cache_store.fetch(key, cache_options) do
235230
yield
236231
end
237232
else
@@ -242,7 +237,6 @@ def fetch(adapter_instance, cache_options = serializer_class._cache_options)
242237
# 1. Determine cached fields from serializer class options
243238
# 2. Get non_cached_fields and fetch cache_fields
244239
# 3. Merge the two hashes using adapter_instance#fragment_cache
245-
# rubocop:disable Metrics/AbcSize
246240
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
247241
serializer_class._cache_options ||= {}
248242
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
@@ -251,22 +245,21 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
251245
non_cached_fields = fields[:non_cached].dup
252246
non_cached_hash = attributes(non_cached_fields, true)
253247
include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
254-
non_cached_hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
248+
non_cached_hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance)
255249

256250
cached_fields = fields[:cached].dup
257251
key = cache_key(adapter_instance)
258252
cached_hash =
259253
cached_attributes.fetch(key) do
260-
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
254+
fetch(adapter_instance, serializer_class._cache_options, key) do
261255
hash = attributes(cached_fields, true)
262256
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
263-
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
257+
hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance)
264258
end
265259
end
266260
# Merge both results
267261
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
268262
end
269-
# rubocop:enable Metrics/AbcSize
270263

271264
def cache_key(adapter_instance)
272265
return @cache_key if defined?(@cache_key)

0 commit comments

Comments
 (0)