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,7 +227,8 @@ 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?
if (serializer = build_association_collection_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class))
{ serializer: serializer } { serializer: serializer }
else else
# BUG: per #2027, JSON API resource relationships are only id and type, and hence either # BUG: per #2027, JSON API resource relationships are only id and type, and hence either
@ -238,6 +239,9 @@ module ActiveModel
# with an object that is non-nil and has no defined serializer. # with an object that is non-nil and has no defined serializer.
{ virtual_value: association_value.try(:as_json) || association_value } { virtual_value: association_value.try(:as_json) || association_value }
end end
else
{ serializer: build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class) }
end
end end
def build_association_options(parent_serializer, parent_serializer_namespace_option, include_slice) def build_association_options(parent_serializer, parent_serializer_namespace_option, include_slice)
@ -254,11 +258,17 @@ 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
build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class)
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 # Make all the parent serializer instance options available to associations
# except ActiveModelSerializers-specific ones we don't want. # except ActiveModelSerializers-specific ones we don't want.
serializer_options = parent_serializer_options.except(:serializer) serializer_options = parent_serializer_options.except(:serializer)
@ -268,5 +278,4 @@ module ActiveModel
end end
end end
end end
end
end end