mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Refactor
This commit is contained in:
parent
fad4ef1046
commit
1bddd9fdb5
@ -2,6 +2,9 @@ module ActiveModel
|
||||
class Serializer
|
||||
# @api private
|
||||
class HasManyReflection < CollectionReflection
|
||||
def to_many?
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -121,6 +121,10 @@ module ActiveModel
|
||||
:nil
|
||||
end
|
||||
|
||||
def to_many?
|
||||
false
|
||||
end
|
||||
|
||||
# Build association. This method is used internally to
|
||||
# build serializer's association by its reflection.
|
||||
#
|
||||
@ -150,17 +154,9 @@ module ActiveModel
|
||||
reflection_options ||= settings.merge(include_data: include_data?(include_slice)) # Needs to be after association_value is evaluated unless reflection.block.nil?
|
||||
|
||||
if serializer_class
|
||||
if (serializer = build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class))
|
||||
reflection_options[: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.
|
||||
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
|
||||
end
|
||||
reflection_options.merge!(
|
||||
serialize_association_value!(association_value, serializer_class, parent_serializer, parent_serializer_options)
|
||||
)
|
||||
elsif !association_value.nil? && !association_value.instance_of?(Object)
|
||||
reflection_options[:virtual_value] = association_value
|
||||
end
|
||||
@ -230,6 +226,20 @@ module ActiveModel
|
||||
end
|
||||
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))
|
||||
{ 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
|
||||
end
|
||||
|
||||
def build_association_options(parent_serializer, parent_serializer_namespace_option, include_slice)
|
||||
serializer_for_options = {
|
||||
# Pass the parent's namespace onto the child serializer
|
||||
|
||||
Loading…
Reference in New Issue
Block a user