File tree Expand file tree Collapse file tree 5 files changed +28
-7
lines changed Expand file tree Collapse file tree 5 files changed +28
-7
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 @@ -7,12 +7,14 @@ class ArraySerializer
7
7
attr_reader :meta , :meta_key
8
8
9
9
def initialize ( objects , options = { } )
10
+ options . merge! ( root : nil )
11
+
10
12
@objects = objects . map do |object |
11
13
serializer_class = options . fetch (
12
14
:serializer ,
13
15
ActiveModel ::Serializer . serializer_for ( object )
14
16
)
15
- serializer_class . new ( object )
17
+ serializer_class . new ( object , options )
16
18
end
17
19
@meta = options [ :meta ]
18
20
@meta_key = options [ :meta_key ]
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ class ArraySerializerTest < Minitest::Test
6
6
def setup
7
7
@comment = Comment . new
8
8
@post = Post . new
9
- @serializer = ArraySerializer . new ( [ @comment , @post ] )
9
+ @serializer = ArraySerializer . new ( [ @comment , @post ] , { some : :options } )
10
10
end
11
11
12
12
def test_respond_to_each
@@ -21,6 +21,8 @@ def test_each_object_should_be_serialized_with_appropriate_serializer
21
21
22
22
assert_kind_of PostSerializer , serializers . last
23
23
assert_kind_of Post , serializers . last . object
24
+
25
+ assert_equal serializers . last . custom_options [ :some ] , :options
24
26
end
25
27
end
26
28
end
Original file line number Diff line number Diff line change @@ -78,6 +78,10 @@ module Spam; end
78
78
def blog
79
79
Blog . new ( id : 999 , name : "Custom blog" )
80
80
end
81
+
82
+ def custom_options
83
+ options
84
+ end
81
85
end
82
86
83
87
SpammyPostSerializer = Class . new ( ActiveModel ::Serializer ) do
@@ -95,6 +99,10 @@ def self.root_name
95
99
96
100
belongs_to :post
97
101
belongs_to :author
102
+
103
+ def custom_options
104
+ options
105
+ end
98
106
end
99
107
100
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