@@ -30,18 +30,17 @@ def setup
3030 def test_has_many_and_has_one
3131 @author_serializer . associations . each do |association |
3232 key = association . key
33- serializer = association . serializer
34- options = association . options
33+ serializer = association . lazy_association . serializer
3534
3635 case key
3736 when :posts
38- assert_equal true , options . fetch ( : include_data)
37+ assert_equal true , association . include_data?
3938 assert_kind_of ( ActiveModelSerializers . config . collection_serializer , serializer )
4039 when :bio
41- assert_equal true , options . fetch ( : include_data)
40+ assert_equal true , association . include_data?
4241 assert_nil serializer
4342 when :roles
44- assert_equal true , options . fetch ( : include_data)
43+ assert_equal true , association . include_data?
4544 assert_kind_of ( ActiveModelSerializers . config . collection_serializer , serializer )
4645 else
4746 flunk "Unknown association: #{ key } "
@@ -56,12 +55,11 @@ def test_has_many_with_no_serializer
5655 end
5756 post_serializer_class . new ( @post ) . associations . each do |association |
5857 key = association . key
59- serializer = association . serializer
60- options = association . options
58+ serializer = association . lazy_association . serializer
6159
6260 assert_equal :tags , key
6361 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
6563 end
6664 end
6765
@@ -70,7 +68,7 @@ def test_serializer_options_are_passed_into_associations_serializers
7068 . associations
7169 . detect { |assoc | assoc . key == :comments }
7270
73- comment_serializer = association . serializer . first
71+ comment_serializer = association . lazy_association . serializer . first
7472 class << comment_serializer
7573 def custom_options
7674 instance_options
@@ -82,7 +80,7 @@ def custom_options
8280 def test_belongs_to
8381 @comment_serializer . associations . each do |association |
8482 key = association . key
85- serializer = association . serializer
83+ serializer = association . lazy_association . serializer
8684
8785 case key
8886 when :post
@@ -93,7 +91,7 @@ def test_belongs_to
9391 flunk "Unknown association: #{ key } "
9492 end
9593
96- assert_equal true , association . options . fetch ( : include_data)
94+ assert_equal true , association . include_data?
9795 end
9896 end
9997
@@ -203,11 +201,11 @@ def test_associations_namespaced_resources
203201 @post_serializer . associations . each do |association |
204202 case association . key
205203 when :comments
206- assert_instance_of ( ResourceNamespace ::CommentSerializer , association . serializer . first )
204+ assert_instance_of ( ResourceNamespace ::CommentSerializer , association . lazy_association . serializer . first )
207205 when :author
208- assert_instance_of ( ResourceNamespace ::AuthorSerializer , association . serializer )
206+ assert_instance_of ( ResourceNamespace ::AuthorSerializer , association . lazy_association . serializer )
209207 when :description
210- assert_instance_of ( ResourceNamespace ::DescriptionSerializer , association . serializer )
208+ assert_instance_of ( ResourceNamespace ::DescriptionSerializer , association . lazy_association . serializer )
211209 else
212210 flunk "Unknown association: #{ key } "
213211 end
@@ -245,11 +243,11 @@ def test_associations_namespaced_resources
245243 @post_serializer . associations . each do |association |
246244 case association . key
247245 when :comments
248- assert_instance_of ( PostSerializer ::CommentSerializer , association . serializer . first )
246+ assert_instance_of ( PostSerializer ::CommentSerializer , association . lazy_association . serializer . first )
249247 when :author
250- assert_instance_of ( PostSerializer ::AuthorSerializer , association . serializer )
248+ assert_instance_of ( PostSerializer ::AuthorSerializer , association . lazy_association . serializer )
251249 when :description
252- assert_instance_of ( PostSerializer ::DescriptionSerializer , association . serializer )
250+ assert_instance_of ( PostSerializer ::DescriptionSerializer , association . lazy_association . serializer )
253251 else
254252 flunk "Unknown association: #{ key } "
255253 end
@@ -260,7 +258,7 @@ def test_associations_namespaced_resources
260258 def test_conditional_associations
261259 model = Class . new ( ::Model ) do
262260 attributes :true , :false
263- associations :association
261+ associations :something
264262 end . new ( true : true , false : false )
265263
266264 scenarios = [
@@ -284,7 +282,7 @@ def test_conditional_associations
284282
285283 scenarios . each do |s |
286284 serializer = Class . new ( ActiveModel ::Serializer ) do
287- belongs_to :association , s [ :options ]
285+ belongs_to :something , s [ :options ]
288286
289287 def true
290288 true
@@ -296,7 +294,7 @@ def false
296294 end
297295
298296 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 ] } " )
300298 end
301299 end
302300
@@ -341,8 +339,8 @@ def setup
341339 @author_serializer = AuthorSerializer . new ( @author )
342340 @inherited_post_serializer = InheritedPostSerializer . new ( @post )
343341 @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 )
346344 @post_associations = @post_serializer . associations . to_a
347345 @inherited_post_associations = @inherited_post_serializer . associations . to_a
348346 end
@@ -361,28 +359,35 @@ def setup
361359
362360 test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
363361 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 )
365363 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? )
366366 end
367367
368368 test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
369369 assert_equal [ :author , :comments , :blog ] , @post_associations . map ( &:name )
370370 assert_equal [ :author , :comments , :blog , :comments ] , @inherited_post_associations . map ( &:name )
371371
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?
374374
375- refute @post_associations . detect { |assoc | assoc . name == :comments } . options . key? ( :key )
375+ refute @post_associations . detect { |assoc | assoc . name == :comments } . key?
376376 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
381386 end
382387
383388 test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
384389 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
386391 assert_equal ( result , expected )
387392 end
388393 end
0 commit comments