Skip to content

Commit 6cd6ed7

Browse files
committed
Move association serialization to association
1 parent c2dccba commit 6cd6ed7

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/active_model/serializer.rb

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def serializable_hash(adapter_options = nil, options = {}, adapter_instance = se
380380
adapter_options ||= {}
381381
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
382382
resource = attributes_hash(adapter_options, options, adapter_instance)
383-
relationships = resource_relationships(adapter_options, options, adapter_instance)
383+
relationships = associations_hash(adapter_options, options, adapter_instance)
384384
resource.merge(relationships)
385385
end
386386
alias to_hash serializable_hash
@@ -423,34 +423,17 @@ def attributes_hash(_adapter_options, options, adapter_instance)
423423
end
424424

425425
# @api private
426-
def resource_relationships(adapter_options, options, adapter_instance)
426+
def associations_hash(adapter_options, options, adapter_instance)
427427
relationships = {}
428428
include_directive = options.fetch(:include_directive)
429429
associations(include_directive).each do |association|
430430
adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
431-
relationships[association.key] ||= relationship_value_for(association, adapter_opts, adapter_instance)
431+
relationships[association.key] ||= association.serializable_hash(adapter_opts, adapter_instance)
432432
end
433433

434434
relationships
435435
end
436436

437-
# @api private
438-
def relationship_value_for(association, adapter_options, adapter_instance)
439-
return association.options[:virtual_value] if association.options[:virtual_value]
440-
association_serializer = association.serializer
441-
association_object = association_serializer && association_serializer.object
442-
return unless association_object
443-
444-
relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
445-
446-
if association.options[:polymorphic] && relationship_value
447-
polymorphic_type = association_object.class.name.underscore
448-
relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
449-
end
450-
451-
relationship_value
452-
end
453-
454437
protected
455438

456439
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
245245
non_cached_fields = fields[:non_cached].dup
246246
non_cached_hash = attributes(non_cached_fields, true)
247247
include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
248-
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)
249249

250250
cached_fields = fields[:cached].dup
251251
key = cache_key(adapter_instance)
@@ -254,7 +254,7 @@ def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
254254
fetch(adapter_instance, serializer_class._cache_options, key) do
255255
hash = attributes(cached_fields, true)
256256
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
257-
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
257+
hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance)
258258
end
259259
end
260260
# Merge both results

0 commit comments

Comments
 (0)