Refactor collection reflection

This commit is contained in:
Benjamin Fleischer 2017-04-23 14:53:45 -05:00
parent 1bddd9fdb5
commit 079b3d6841

View File

@ -227,16 +227,20 @@ module ActiveModel
end end
def serialize_association_value!(association_value, serializer_class, parent_serializer, parent_serializer_options) def serialize_association_value!(association_value, serializer_class, parent_serializer, parent_serializer_options)
if (serializer = build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class)) if to_many?
{ serializer: serializer } if (serializer = build_association_collection_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class))
{ serializer: serializer }
else
# BUG: per #2027, JSON API resource relationships are only id and type, and hence either
# *require* a serializer or we need to be a little clever about figuring out the id/type.
# In either case, returning the raw virtual value will almost always be incorrect.
#
# Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
# with an object that is non-nil and has no defined serializer.
{ virtual_value: association_value.try(:as_json) || association_value }
end
else else
# BUG: per #2027, JSON API resource relationships are only id and type, and hence either { serializer: build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class) }
# *require* a serializer or we need to be a little clever about figuring out the id/type.
# In either case, returning the raw virtual value will almost always be incorrect.
#
# Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
# with an object that is non-nil and has no defined serializer.
{ virtual_value: association_value.try(:as_json) || association_value }
end end
end end
@ -254,19 +258,24 @@ module ActiveModel
end end
# NOTE(BF): This serializer throw/catch should only happen when the serializer is a collection # NOTE(BF): This serializer throw/catch should only happen when the serializer is a collection
# serializer. This is a good reason for the reflection to have a to_many? or collection? type method. # serializer.
# #
# @return [ActiveModel::Serializer, nil] # @return [ActiveModel::Serializer, nil]
def build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class) def build_association_collection_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class)
catch(:no_serializer) do catch(:no_serializer) do
# Make all the parent serializer instance options available to associations build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class)
# except ActiveModelSerializers-specific ones we don't want.
serializer_options = parent_serializer_options.except(:serializer)
serializer_options[:serializer_context_class] = parent_serializer.class
serializer_options[:serializer] = serializer if serializer
serializer_class.new(association_value, serializer_options)
end end
end end
# @return [ActiveModel::Serializer, nil]
def build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class)
# Make all the parent serializer instance options available to associations
# except ActiveModelSerializers-specific ones we don't want.
serializer_options = parent_serializer_options.except(:serializer)
serializer_options[:serializer_context_class] = parent_serializer.class
serializer_options[:serializer] = serializer if serializer
serializer_class.new(association_value, serializer_options)
end
end end
end end
end end