File tree Expand file tree Collapse file tree 3 files changed +18
-5
lines changed Expand file tree Collapse file tree 3 files changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -176,19 +176,20 @@ def attributes(options = {})
176
176
end
177
177
178
178
def each_association ( &block )
179
- self . class . _associations . dup . each do |name , options |
179
+ self . class . _associations . dup . each do |name , association_options |
180
180
next unless object
181
181
182
182
association = object . send ( name )
183
183
association_value = send ( name )
184
- serializer_class = ActiveModel ::Serializer . serializer_for ( association , options )
184
+ serializer_class = ActiveModel ::Serializer . serializer_for ( association , association_options )
185
+
185
186
serializer = serializer_class . new (
186
187
association_value ,
187
- serializer_from_options ( options )
188
+ serializer_from_options ( association_options ) . merge ( options )
188
189
) if serializer_class
189
190
190
191
if block_given?
191
- block . call ( name , serializer , options [ :association_options ] )
192
+ block . call ( name , serializer , association_options [ :association_options ] )
192
193
end
193
194
end
194
195
end
Original file line number Diff line number Diff line change @@ -99,6 +99,10 @@ def self.root_name
99
99
100
100
belongs_to :post
101
101
belongs_to :author
102
+
103
+ def custom_options
104
+ options
105
+ end
102
106
end
103
107
104
108
AuthorSerializer = Class . new ( ActiveModel ::Serializer ) do
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ def setup
37
37
@post . author = @author
38
38
@author . posts = [ @post ]
39
39
40
- @post_serializer = PostSerializer . new ( @post )
40
+ @post_serializer = PostSerializer . new ( @post , { custom_options : true } )
41
41
@author_serializer = AuthorSerializer . new ( @author )
42
42
@comment_serializer = CommentSerializer . new ( @comment )
43
43
end
@@ -65,6 +65,14 @@ def test_has_many_and_has_one
65
65
end
66
66
end
67
67
68
+ def test_serializer_options_are_passed_into_associations_serializers
69
+ @post_serializer . each_association do |name , association |
70
+ if name == :comments
71
+ assert association . first . custom_options [ :custom_options ]
72
+ end
73
+ end
74
+ end
75
+
68
76
def test_belongs_to
69
77
assert_equal (
70
78
{ post : { type : :belongs_to , association_options : { } } ,
You can’t perform that action at this time.
0 commit comments