@@ -30,18 +30,17 @@ def setup
30
30
def test_has_many_and_has_one
31
31
@author_serializer . associations . each do |association |
32
32
key = association . key
33
- serializer = association . serializer
34
- options = association . options
33
+ serializer = association . lazy_association . serializer
35
34
36
35
case key
37
36
when :posts
38
- assert_equal true , options . fetch ( : include_data)
37
+ assert_equal true , association . include_data?
39
38
assert_kind_of ( ActiveModelSerializers . config . collection_serializer , serializer )
40
39
when :bio
41
- assert_equal true , options . fetch ( : include_data)
40
+ assert_equal true , association . include_data?
42
41
assert_nil serializer
43
42
when :roles
44
- assert_equal true , options . fetch ( : include_data)
43
+ assert_equal true , association . include_data?
45
44
assert_kind_of ( ActiveModelSerializers . config . collection_serializer , serializer )
46
45
else
47
46
flunk "Unknown association: #{ key } "
@@ -56,12 +55,11 @@ def test_has_many_with_no_serializer
56
55
end
57
56
post_serializer_class . new ( @post ) . associations . each do |association |
58
57
key = association . key
59
- serializer = association . serializer
60
- options = association . options
58
+ serializer = association . lazy_association . serializer
61
59
62
60
assert_equal :tags , key
63
61
assert_nil serializer
64
- assert_equal [ { id : 'tagid' , name : '#hashtagged' } ] . to_json , options [ : virtual_value] . to_json
62
+ assert_equal [ { id : 'tagid' , name : '#hashtagged' } ] . to_json , association . virtual_value . to_json
65
63
end
66
64
end
67
65
@@ -70,7 +68,7 @@ def test_serializer_options_are_passed_into_associations_serializers
70
68
. associations
71
69
. detect { |assoc | assoc . key == :comments }
72
70
73
- comment_serializer = association . serializer . first
71
+ comment_serializer = association . lazy_association . serializer . first
74
72
class << comment_serializer
75
73
def custom_options
76
74
instance_options
@@ -82,7 +80,7 @@ def custom_options
82
80
def test_belongs_to
83
81
@comment_serializer . associations . each do |association |
84
82
key = association . key
85
- serializer = association . serializer
83
+ serializer = association . lazy_association . serializer
86
84
87
85
case key
88
86
when :post
@@ -93,7 +91,7 @@ def test_belongs_to
93
91
flunk "Unknown association: #{ key } "
94
92
end
95
93
96
- assert_equal true , association . options . fetch ( : include_data)
94
+ assert_equal true , association . include_data?
97
95
end
98
96
end
99
97
@@ -203,11 +201,11 @@ def test_associations_namespaced_resources
203
201
@post_serializer . associations . each do |association |
204
202
case association . key
205
203
when :comments
206
- assert_instance_of ( ResourceNamespace ::CommentSerializer , association . serializer . first )
204
+ assert_instance_of ( ResourceNamespace ::CommentSerializer , association . lazy_association . serializer . first )
207
205
when :author
208
- assert_instance_of ( ResourceNamespace ::AuthorSerializer , association . serializer )
206
+ assert_instance_of ( ResourceNamespace ::AuthorSerializer , association . lazy_association . serializer )
209
207
when :description
210
- assert_instance_of ( ResourceNamespace ::DescriptionSerializer , association . serializer )
208
+ assert_instance_of ( ResourceNamespace ::DescriptionSerializer , association . lazy_association . serializer )
211
209
else
212
210
flunk "Unknown association: #{ key } "
213
211
end
@@ -245,11 +243,11 @@ def test_associations_namespaced_resources
245
243
@post_serializer . associations . each do |association |
246
244
case association . key
247
245
when :comments
248
- assert_instance_of ( PostSerializer ::CommentSerializer , association . serializer . first )
246
+ assert_instance_of ( PostSerializer ::CommentSerializer , association . lazy_association . serializer . first )
249
247
when :author
250
- assert_instance_of ( PostSerializer ::AuthorSerializer , association . serializer )
248
+ assert_instance_of ( PostSerializer ::AuthorSerializer , association . lazy_association . serializer )
251
249
when :description
252
- assert_instance_of ( PostSerializer ::DescriptionSerializer , association . serializer )
250
+ assert_instance_of ( PostSerializer ::DescriptionSerializer , association . lazy_association . serializer )
253
251
else
254
252
flunk "Unknown association: #{ key } "
255
253
end
@@ -260,7 +258,7 @@ def test_associations_namespaced_resources
260
258
def test_conditional_associations
261
259
model = Class . new ( ::Model ) do
262
260
attributes :true , :false
263
- associations :association
261
+ associations :something
264
262
end . new ( true : true , false : false )
265
263
266
264
scenarios = [
@@ -284,7 +282,7 @@ def test_conditional_associations
284
282
285
283
scenarios . each do |s |
286
284
serializer = Class . new ( ActiveModel ::Serializer ) do
287
- belongs_to :association , s [ :options ]
285
+ belongs_to :something , s [ :options ]
288
286
289
287
def true
290
288
true
@@ -296,7 +294,7 @@ def false
296
294
end
297
295
298
296
hash = serializable ( model , serializer : serializer ) . serializable_hash
299
- assert_equal ( s [ :included ] , hash . key? ( :association ) , "Error with #{ s [ :options ] } " )
297
+ assert_equal ( s [ :included ] , hash . key? ( :something ) , "Error with #{ s [ :options ] } " )
300
298
end
301
299
end
302
300
@@ -341,8 +339,8 @@ def setup
341
339
@author_serializer = AuthorSerializer . new ( @author )
342
340
@inherited_post_serializer = InheritedPostSerializer . new ( @post )
343
341
@inherited_author_serializer = InheritedAuthorSerializer . new ( @author )
344
- @author_associations = @author_serializer . associations . to_a
345
- @inherited_author_associations = @inherited_author_serializer . associations . to_a
342
+ @author_associations = @author_serializer . associations . to_a . sort_by ( & :name )
343
+ @inherited_author_associations = @inherited_author_serializer . associations . to_a . sort_by ( & :name )
346
344
@post_associations = @post_serializer . associations . to_a
347
345
@inherited_post_associations = @inherited_post_serializer . associations . to_a
348
346
end
@@ -361,28 +359,35 @@ def setup
361
359
362
360
test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
363
361
expected = [ :roles , :bio ] . sort
364
- result = ( @inherited_author_associations - @author_associations ) . map ( &:name ) . sort
362
+ result = ( @inherited_author_associations . map ( & :reflection ) - @author_associations . map ( & :reflection ) ) . map ( &:name )
365
363
assert_equal ( result , expected )
364
+ assert_equal [ true , false , true ] , @inherited_author_associations . map ( &:polymorphic? )
365
+ assert_equal [ false , false , false ] , @author_associations . map ( &:polymorphic? )
366
366
end
367
367
368
368
test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
369
369
assert_equal [ :author , :comments , :blog ] , @post_associations . map ( &:name )
370
370
assert_equal [ :author , :comments , :blog , :comments ] , @inherited_post_associations . map ( &:name )
371
371
372
- refute @post_associations . detect { |assoc | assoc . name == :author } . options . key? ( : polymorphic)
373
- assert_equal true , @inherited_post_associations . detect { |assoc | assoc . name == :author } . options . fetch ( : polymorphic)
372
+ refute @post_associations . detect { |assoc | assoc . name == :author } . polymorphic?
373
+ assert @inherited_post_associations . detect { |assoc | assoc . name == :author } . polymorphic?
374
374
375
- refute @post_associations . detect { |assoc | assoc . name == :comments } . options . key? ( :key )
375
+ refute @post_associations . detect { |assoc | assoc . name == :comments } . key?
376
376
original_comment_assoc , new_comments_assoc = @inherited_post_associations . select { |assoc | assoc . name == :comments }
377
- refute original_comment_assoc . options . key? ( :key )
378
- assert_equal :reviews , new_comments_assoc . options . fetch ( :key )
379
-
380
- assert_equal @post_associations . detect { |assoc | assoc . name == :blog } , @inherited_post_associations . detect { |assoc | assoc . name == :blog }
377
+ refute original_comment_assoc . key?
378
+ assert_equal :reviews , new_comments_assoc . key
379
+
380
+ original_blog = @post_associations . detect { |assoc | assoc . name == :blog }
381
+ inherited_blog = @inherited_post_associations . detect { |assoc | assoc . name == :blog }
382
+ original_parent_serializer = original_blog . lazy_association . options . delete ( :parent_serializer )
383
+ inherited_parent_serializer = inherited_blog . lazy_association . options . delete ( :parent_serializer )
384
+ assert_equal PostSerializer , original_parent_serializer . class
385
+ assert_equal InheritedPostSerializer , inherited_parent_serializer . class
381
386
end
382
387
383
388
test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
384
389
expected = [ :author , :comments , :blog , :reviews ] . sort
385
- result = @inherited_post_serializer . associations . map { | a | a . options . fetch ( :key , a . name ) } . sort
390
+ result = @inherited_post_serializer . associations . map ( & :key ) . sort
386
391
assert_equal ( result , expected )
387
392
end
388
393
end
0 commit comments