mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Move association serialization to association
This commit is contained in:
@@ -29,6 +29,22 @@ module ActiveModel
|
||||
def meta
|
||||
options[:meta]
|
||||
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
|
||||
|
||||
@@ -245,7 +245,7 @@ module ActiveModel
|
||||
non_cached_fields = fields[:non_cached].dup
|
||||
non_cached_hash = attributes(non_cached_fields, true)
|
||||
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
|
||||
key = cache_key(adapter_instance)
|
||||
@@ -254,7 +254,7 @@ module ActiveModel
|
||||
fetch(adapter_instance, serializer_class._cache_options, key) do
|
||||
hash = attributes(cached_fields, true)
|
||||
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
|
||||
# Merge both results
|
||||
|
||||
Reference in New Issue
Block a user