Skip to content

Commit 1bddd9f

Browse files
committed
Refactor
1 parent fad4ef1 commit 1bddd9f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/active_model/serializer/has_many_reflection.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module ActiveModel
22
class Serializer
33
# @api private
44
class HasManyReflection < CollectionReflection
5+
def to_many?
6+
true
7+
end
58
end
69
end
710
end

lib/active_model/serializer/reflection.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def include_data(value = true)
121121
:nil
122122
end
123123

124+
def to_many?
125+
false
126+
end
127+
124128
# Build association. This method is used internally to
125129
# build serializer's association by its reflection.
126130
#
@@ -150,17 +154,9 @@ def build_association(parent_serializer, parent_serializer_options, include_slic
150154
reflection_options ||= settings.merge(include_data: include_data?(include_slice)) # Needs to be after association_value is evaluated unless reflection.block.nil?
151155

152156
if serializer_class
153-
if (serializer = build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class))
154-
reflection_options[:serializer] = serializer
155-
else
156-
# BUG: per #2027, JSON API resource relationships are only id and type, and hence either
157-
# *require* a serializer or we need to be a little clever about figuring out the id/type.
158-
# In either case, returning the raw virtual value will almost always be incorrect.
159-
#
160-
# Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
161-
# with an object that is non-nil and has no defined serializer.
162-
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
163-
end
157+
reflection_options.merge!(
158+
serialize_association_value!(association_value, serializer_class, parent_serializer, parent_serializer_options)
159+
)
164160
elsif !association_value.nil? && !association_value.instance_of?(Object)
165161
reflection_options[:virtual_value] = association_value
166162
end
@@ -230,6 +226,20 @@ def value(serializer, include_slice)
230226
end
231227
end
232228

229+
def serialize_association_value!(association_value, serializer_class, parent_serializer, parent_serializer_options)
230+
if (serializer = build_association_serializer(parent_serializer, parent_serializer_options, association_value, serializer_class))
231+
{ serializer: serializer }
232+
else
233+
# BUG: per #2027, JSON API resource relationships are only id and type, and hence either
234+
# *require* a serializer or we need to be a little clever about figuring out the id/type.
235+
# In either case, returning the raw virtual value will almost always be incorrect.
236+
#
237+
# Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
238+
# with an object that is non-nil and has no defined serializer.
239+
{ virtual_value: association_value.try(:as_json) || association_value }
240+
end
241+
end
242+
233243
def build_association_options(parent_serializer, parent_serializer_namespace_option, include_slice)
234244
serializer_for_options = {
235245
# Pass the parent's namespace onto the child serializer

0 commit comments

Comments
 (0)