mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Move association serialization to association
This commit is contained in:
parent
c2dccbac5f
commit
6cd6ed7e78
@ -380,7 +380,7 @@ module ActiveModel
|
|||||||
adapter_options ||= {}
|
adapter_options ||= {}
|
||||||
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
||||||
resource = attributes_hash(adapter_options, options, adapter_instance)
|
resource = attributes_hash(adapter_options, options, adapter_instance)
|
||||||
relationships = resource_relationships(adapter_options, options, adapter_instance)
|
relationships = associations_hash(adapter_options, options, adapter_instance)
|
||||||
resource.merge(relationships)
|
resource.merge(relationships)
|
||||||
end
|
end
|
||||||
alias to_hash serializable_hash
|
alias to_hash serializable_hash
|
||||||
@ -423,34 +423,17 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
def resource_relationships(adapter_options, options, adapter_instance)
|
def associations_hash(adapter_options, options, adapter_instance)
|
||||||
relationships = {}
|
relationships = {}
|
||||||
include_directive = options.fetch(:include_directive)
|
include_directive = options.fetch(:include_directive)
|
||||||
associations(include_directive).each do |association|
|
associations(include_directive).each do |association|
|
||||||
adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
|
adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
|
||||||
relationships[association.key] ||= relationship_value_for(association, adapter_opts, adapter_instance)
|
relationships[association.key] ||= association.serializable_hash(adapter_opts, adapter_instance)
|
||||||
end
|
end
|
||||||
|
|
||||||
relationships
|
relationships
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
|
||||||
def relationship_value_for(association, adapter_options, adapter_instance)
|
|
||||||
return association.options[:virtual_value] if association.options[:virtual_value]
|
|
||||||
association_serializer = association.serializer
|
|
||||||
association_object = association_serializer && association_serializer.object
|
|
||||||
return unless association_object
|
|
||||||
|
|
||||||
relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
|
|
||||||
|
|
||||||
if association.options[:polymorphic] && relationship_value
|
|
||||||
polymorphic_type = association_object.class.name.underscore
|
|
||||||
relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
|
|
||||||
end
|
|
||||||
|
|
||||||
relationship_value
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
attr_accessor :instance_options
|
attr_accessor :instance_options
|
||||||
|
|||||||
@ -29,6 +29,22 @@ module ActiveModel
|
|||||||
def meta
|
def meta
|
||||||
options[:meta]
|
options[:meta]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
def serializable_hash(adapter_options, adapter_instance)
|
||||||
|
return options[:virtual_value] if options[:virtual_value]
|
||||||
|
object = serializer && serializer.object
|
||||||
|
return unless object
|
||||||
|
|
||||||
|
serialization = serializer.serializable_hash(adapter_options, {}, adapter_instance)
|
||||||
|
|
||||||
|
if options[:polymorphic] && serialization
|
||||||
|
polymorphic_type = object.class.name.underscore
|
||||||
|
serialization = { type: polymorphic_type, polymorphic_type.to_sym => serialization }
|
||||||
|
end
|
||||||
|
|
||||||
|
serialization
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -245,7 +245,7 @@ module ActiveModel
|
|||||||
non_cached_fields = fields[:non_cached].dup
|
non_cached_fields = fields[:non_cached].dup
|
||||||
non_cached_hash = attributes(non_cached_fields, true)
|
non_cached_hash = attributes(non_cached_fields, true)
|
||||||
include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
|
include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
|
||||||
non_cached_hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
non_cached_hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance)
|
||||||
|
|
||||||
cached_fields = fields[:cached].dup
|
cached_fields = fields[:cached].dup
|
||||||
key = cache_key(adapter_instance)
|
key = cache_key(adapter_instance)
|
||||||
@ -254,7 +254,7 @@ module ActiveModel
|
|||||||
fetch(adapter_instance, serializer_class._cache_options, key) do
|
fetch(adapter_instance, serializer_class._cache_options, key) do
|
||||||
hash = attributes(cached_fields, true)
|
hash = attributes(cached_fields, true)
|
||||||
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
||||||
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
hash.merge! associations_hash({}, { include_directive: include_directive }, adapter_instance)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# Merge both results
|
# Merge both results
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user