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
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 }
else
# 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.
{ virtual_value: association_value.try(:as_json) || association_value }
end
else
{ serializer: build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class) }
end
end
def build_association_options(parent_serializer, parent_serializer_namespace_option, include_slice)
@ -254,11 +258,17 @@ module ActiveModel
end
# 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]
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
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
# except ActiveModelSerializers-specific ones we don't want.
serializer_options = parent_serializer_options.except(:serializer)
@ -269,4 +279,3 @@ module ActiveModel
end
end
end
end