@@ -31,13 +31,13 @@ def test_has_many_and_has_one
31
31
32
32
case key
33
33
when :posts
34
- assert_equal ( { include_data : true } , options )
34
+ assert_equal true , options . fetch ( :include_data )
35
35
assert_kind_of ( ActiveModelSerializers . config . collection_serializer , serializer )
36
36
when :bio
37
- assert_equal ( { include_data : true } , options )
37
+ assert_equal true , options . fetch ( :include_data )
38
38
assert_nil serializer
39
39
when :roles
40
- assert_equal ( { include_data : true } , options )
40
+ assert_equal true , options . fetch ( :include_data )
41
41
assert_kind_of ( ActiveModelSerializers . config . collection_serializer , serializer )
42
42
else
43
43
flunk "Unknown association: #{ key } "
@@ -79,7 +79,7 @@ def test_belongs_to
79
79
flunk "Unknown association: #{ key } "
80
80
end
81
81
82
- assert_equal ( { include_data : true } , association . options )
82
+ assert_equal true , association . options . fetch ( :include_data )
83
83
end
84
84
end
85
85
@@ -291,11 +291,23 @@ def test_illegal_conditional_associations
291
291
end
292
292
293
293
class InheritedSerializerTest < ActiveSupport ::TestCase
294
+ class PostSerializer < ActiveModel ::Serializer
295
+ belongs_to :author
296
+ has_many :comments
297
+ belongs_to :blog
298
+ end
299
+
294
300
class InheritedPostSerializer < PostSerializer
295
301
belongs_to :author , polymorphic : true
296
302
has_many :comments , key : :reviews
297
303
end
298
304
305
+ class AuthorSerializer < ActiveModel ::Serializer
306
+ has_many :posts
307
+ has_many :roles
308
+ has_one :bio
309
+ end
310
+
299
311
class InheritedAuthorSerializer < AuthorSerializer
300
312
has_many :roles , polymorphic : true
301
313
has_one :bio , polymorphic : true
@@ -333,9 +345,18 @@ def setup
333
345
end
334
346
335
347
test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
336
- expected = [ :author , :comments , :blog ] . sort
337
- result = ( @inherited_post_associations - @post_associations ) . map ( &:name ) . sort
338
- assert_equal ( result , expected )
348
+ assert_equal [ :author , :comments , :blog ] , @post_associations . map ( &:name )
349
+ assert_equal [ :author , :comments , :blog , :comments ] , @inherited_post_associations . map ( &:name )
350
+
351
+ refute @post_associations . detect { |assoc | assoc . name == :author } . options . key? ( :polymorphic )
352
+ assert_equal true , @inherited_post_associations . detect { |assoc | assoc . name == :author } . options . fetch ( :polymorphic )
353
+
354
+ refute @post_associations . detect { |assoc | assoc . name == :comments } . options . key? ( :key )
355
+ original_comment_assoc , new_comments_assoc = @inherited_post_associations . select { |assoc | assoc . name == :comments }
356
+ refute original_comment_assoc . options . key? ( :key )
357
+ assert_equal :reviews , new_comments_assoc . options . fetch ( :key )
358
+
359
+ assert_equal @post_associations . detect { |assoc | assoc . name == :blog } , @inherited_post_associations . detect { |assoc | assoc . name == :blog }
339
360
end
340
361
341
362
test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
0 commit comments