Skip to content

Commit 3abc80b

Browse files
committed
ipw
1 parent a6cf864 commit 3abc80b

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

lib/active_model/serializer/association.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ class Serializer
99
# @example
1010
# Association.new(:comments, CommentSummarySerializer)
1111
#
12-
Association = Struct.new(:name, :serializer, :options, :links, :meta) do
12+
Association = Struct.new(:name, :reflection, :options, :links, :meta) do
1313
# @return [Symbol]
1414
def key
1515
options.fetch(:key, name)
1616
end
17+
18+
def as_json(fields)
19+
20+
21+
end
1722
end
1823
end
1924
end

lib/active_model/serializer/belongs_to_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 BelongsToReflection < SingularReflection
5+
def foreign_key_on
6+
:self
7+
end
58
end
69
end
710
end

lib/active_model/serializer/reflection.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def include_data(value = true)
5858
:nil
5959
end
6060

61+
def foreign_key_on
62+
:related
63+
end
64+
6165
# @param serializer [ActiveModel::Serializer]
6266
# @yield [ActiveModel::Serializer]
6367
# @return [:nil, associated resource or resource collection]
@@ -90,7 +94,7 @@ def value(serializer)
9094
# Build association. This method is used internally to
9195
# build serializer's association by its reflection.
9296
#
93-
# @param [Serializer] subject is a parent serializer for given association
97+
# @param [Serializer] parent_serializer is a parent serializer for given association
9498
# @param [Hash{Symbol => Object}] parent_serializer_options
9599
#
96100
# @example
@@ -108,26 +112,30 @@ def value(serializer)
108112
#
109113
# @api private
110114
#
111-
def build_association(subject, parent_serializer_options)
112-
association_value = value(subject)
115+
def build_association(parent_serializer, parent_serializer_options)
116+
# association_value = value(parent_serializer)
113117
reflection_options = options.dup
114-
serializer_class = subject.class.serializer_for(association_value, reflection_options)
118+
# model_class = class_name.constantize
119+
# serializer_class = parent_serializer.class.get_serializer_for(model_class)
120+
115121
reflection_options[:include_data] = @_include_data
122+
reflection_options[:parent_serializer] = parent_serializer
123+
reflection_options[:parent_serializer_options] = parent_serializer_options
116124

117-
if serializer_class
118-
begin
119-
serializer = serializer_class.new(
120-
association_value,
121-
serializer_options(subject, parent_serializer_options, reflection_options)
122-
)
123-
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
124-
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
125-
end
126-
elsif !association_value.nil? && !association_value.instance_of?(Object)
127-
reflection_options[:virtual_value] = association_value
128-
end
125+
# if serializer_class
126+
# begin
127+
# serializer = serializer_class.new(
128+
# association_value,
129+
# serializer_options(parent_serializer, parent_serializer_options, reflection_options)
130+
# )
131+
# rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
132+
# reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
133+
# end
134+
# elsif !association_value.nil? && !association_value.instance_of?(Object)
135+
# reflection_options[:virtual_value] = association_value
136+
# end
129137

130-
Association.new(name, serializer, reflection_options, @_links, @_meta)
138+
Association.new(name, self, reflection_options, @_links, @_meta)
131139
end
132140

133141
protected
@@ -136,12 +144,12 @@ def build_association(subject, parent_serializer_options)
136144

137145
private
138146

139-
def serializer_options(subject, parent_serializer_options, reflection_options)
147+
def serializer_options(parent_serializer, parent_serializer_options, reflection_options)
140148
serializer = reflection_options.fetch(:serializer, nil)
141149

142150
serializer_options = parent_serializer_options.except(:serializer)
143151
serializer_options[:serializer] = serializer if serializer
144-
serializer_options[:serializer_context_class] = subject.class
152+
serializer_options[:serializer_context_class] = parent_serializer.class
145153
serializer_options
146154
end
147155
end

0 commit comments

Comments
 (0)