From 1bddd9fdb5342e346102e08d93439e3d8d9a1509 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 23 Apr 2017 14:47:55 -0500 Subject: [PATCH] Refactor --- .../serializer/has_many_reflection.rb | 3 ++ lib/active_model/serializer/reflection.rb | 32 ++++++++++++------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/active_model/serializer/has_many_reflection.rb b/lib/active_model/serializer/has_many_reflection.rb index 60ccc481..aab67a4f 100644 --- a/lib/active_model/serializer/has_many_reflection.rb +++ b/lib/active_model/serializer/has_many_reflection.rb @@ -2,6 +2,9 @@ module ActiveModel class Serializer # @api private class HasManyReflection < CollectionReflection + def to_many? + true + end end end end diff --git a/lib/active_model/serializer/reflection.rb b/lib/active_model/serializer/reflection.rb index d0ab2847..12131073 100644 --- a/lib/active_model/serializer/reflection.rb +++ b/lib/active_model/serializer/reflection.rb @@ -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