@@ -37,6 +37,24 @@ def render_array_using_implicit_serializer
37
37
render json : array ,
38
38
each_serializer : ProfilePreviewSerializer
39
39
end
40
+
41
+ def render_array_using_explicit_serializer_and_custom_serializers
42
+ @post = Post . new ( title : 'New Post' , body : 'Body' )
43
+ @author = Author . new ( name : 'Jane Blogger' )
44
+ @author . posts = [ @post ]
45
+ @post . author = @author
46
+ @first_comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
47
+ @second_comment = Comment . new ( id : 2 , body : 'ZOMG ANOTHER COMMENT' )
48
+ @post . comments = [ @first_comment , @second_comment ]
49
+ @first_comment . post = @post
50
+ @first_comment . author = nil
51
+ @second_comment . post = @post
52
+ @second_comment . author = nil
53
+ @blog = Blog . new ( id : 23 , name : 'AMS Blog' )
54
+ @post . blog = @blog
55
+
56
+ render json : [ @post ] , each_serializer : PostPreviewSerializer
57
+ end
40
58
end
41
59
42
60
tests MyController
@@ -73,6 +91,20 @@ def test_render_array_using_explicit_serializer
73
91
assert_equal expected . to_json , @response . body
74
92
end
75
93
94
+ def test_render_array_using_explicit_serializer_and_custom_serializers
95
+ get :render_array_using_explicit_serializer_and_custom_serializers
96
+
97
+ expected = [
98
+ { "title" => "New Post" ,
99
+ "body" => "Body" ,
100
+ "id" => assigns ( :post ) . id ,
101
+ "comments" => [ { "id" => 1 } , { "id" => 2 } ] ,
102
+ "author" => { "id" => assigns ( :author ) . id }
103
+ }
104
+ ]
105
+
106
+ assert_equal expected . to_json , @response . body
107
+ end
76
108
end
77
109
end
78
110
end
0 commit comments