Move association serialization to association

This commit is contained in:
Benjamin Fleischer
2017-03-31 12:55:46 -05:00
parent c2dccbac5f
commit 6cd6ed7e78
3 changed files with 21 additions and 22 deletions

View File

@@ -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

View File

@@ -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