@@ -14,14 +14,21 @@ def setup
14
14
@second_post = Post . new ( id : 20 , title : 'New Post' , body : 'Body' )
15
15
@third_post = Post . new ( id : 30 , title : 'Yet Another Post' , body : 'Body' )
16
16
@blog = Blog . new ( { name : 'AMS Blog' } )
17
+ @first_comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
18
+ @second_comment = Comment . new ( id : 2 , body : 'ZOMG ANOTHER COMMENT' )
17
19
@first_post . blog = @blog
18
20
@second_post . blog = @blog
19
21
@third_post . blog = nil
20
- @first_post . comments = [ ]
22
+ @first_post . comments = [ @first_comment , @second_comment ]
21
23
@second_post . comments = [ ]
24
+ @third_post . comments = [ ]
22
25
@first_post . author = @author1
23
26
@second_post . author = @author2
24
27
@third_post . author = @author1
28
+ @first_comment . post = @first_post
29
+ @first_comment . author = nil
30
+ @second_comment . post = @first_post
31
+ @second_comment . author = nil
25
32
@author1 . posts = [ @first_post , @third_post ]
26
33
@author1 . bio = @bio1
27
34
@author1 . roles = [ ]
@@ -33,116 +40,144 @@ def setup
33
40
end
34
41
35
42
def test_include_multiple_posts_and_linked
36
- @serializer = ArraySerializer . new ( [ @first_post , @second_post ] )
37
- @adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( @serializer , include : 'author,author.bio,comments' )
38
-
39
- @first_comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
40
- @second_comment = Comment . new ( id : 2 , body : 'ZOMG ANOTHER COMMENT' )
41
- @first_post . comments = [ @first_comment , @second_comment ]
42
- @first_comment . post = @first_post
43
- @first_comment . author = nil
44
- @second_comment . post = @first_post
45
- @second_comment . author = nil
46
- assert_equal ( [
47
- { title : "Hello!!" , body : "Hello, world!!" , id : "10" , links : { comments : [ '1' , '2' ] , blog : "999" , author : "1" } } ,
48
- { title : "New Post" , body : "Body" , id : "20" , links : { comments : [ ] , blog : "999" , author : "2" } }
49
- ] , @adapter . serializable_hash [ :posts ] )
50
-
43
+ serializer = ArraySerializer . new ( [ @first_post , @second_post ] )
44
+ adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new (
45
+ serializer ,
46
+ include : [ 'author' , 'author.bio' , 'comments' ]
47
+ )
48
+ alt_adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new (
49
+ serializer ,
50
+ include : 'author,author.bio,comments'
51
+ )
51
52
52
53
expected = {
53
- comments : [ {
54
- id : "1" ,
55
- body : "ZOMG A COMMENT" ,
56
- links : {
57
- post : "10" ,
58
- author : nil
54
+ linked : {
55
+ comments : [
56
+ {
57
+ id : "1" ,
58
+ body : "ZOMG A COMMENT" ,
59
+ links : {
60
+ post : "1" ,
61
+ author : nil
62
+ }
63
+ } , {
64
+ id : "2" ,
65
+ body : "ZOMG ANOTHER COMMENT" ,
66
+ links : {
67
+ post : "1" ,
68
+ author : nil
69
+ }
70
+ }
71
+ ] ,
72
+ authors : [
73
+ {
74
+ id : "1" ,
75
+ name : "Steve K." ,
76
+ links : {
77
+ posts : [ "1" , "3" ] ,
78
+ roles : [ ] ,
79
+ bio : "1"
80
+ }
81
+ } , {
82
+ id : "2" ,
83
+ name : "Tenderlove" ,
84
+ links : {
85
+ posts : [ "2" ] ,
86
+ roles : [ ] ,
87
+ bio : "2"
88
+ }
89
+ }
90
+ ] ,
91
+ bios : [
92
+ {
93
+ id : "1" ,
94
+ content : "AMS Contributor" ,
95
+ links : {
96
+ author : "1"
97
+ }
98
+ } , {
99
+ id : "2" ,
100
+ content : "Rails Contributor" ,
101
+ links : {
102
+ author : "2"
103
+ }
104
+ }
105
+ ]
106
+ } ,
107
+ posts : [
108
+ {
109
+ id : "10" ,
110
+ title : "Hello!!" ,
111
+ body : "Hello, world!!" ,
112
+ links : {
113
+ comments : [ '1' , '2' ] ,
114
+ blog : "999" ,
115
+ author : "1"
116
+ }
117
+ } ,
118
+ {
119
+ id : "2" ,
120
+ title : "New Post" ,
121
+ body : "Body" ,
122
+ links : {
123
+ comments : [ ] ,
124
+ blog : "999" ,
125
+ author : "2"
126
+ }
59
127
}
60
- } , {
61
- id : "2" ,
62
- body : "ZOMG ANOTHER COMMENT" ,
63
- links : {
64
- post : "10" ,
65
- author : nil
66
- }
67
- } ] ,
68
- authors : [ {
69
- id : "1" ,
70
- name : "Steve K." ,
71
- links : {
72
- posts : [ "10" , "30" ] ,
73
- roles : [ ] ,
74
- bio : "1"
75
- }
76
- } , {
77
- id : "2" ,
78
- name : "Tenderlove" ,
79
- links : {
80
- posts : [ "20" ] ,
81
- roles : [ ] ,
82
- bio : "2"
83
- }
84
- } ] ,
85
- bios : [ {
86
- id : "1" ,
87
- content : "AMS Contributor" ,
88
- links : {
89
- author : "1"
90
- }
91
- } , {
92
- id : "2" ,
93
- content : "Rails Contributor" ,
94
- links : {
95
- author : "2"
96
- }
97
- } ]
128
+ ]
98
129
}
99
- assert_equal expected , @adapter . serializable_hash [ :linked ]
130
+ assert_equal expected , adapter . serializable_hash
131
+ assert_equal expected , alt_adapter . serializable_hash
100
132
end
101
133
102
- def test_include_bio_and_linked
103
- @serializer = BioSerializer . new ( @bio1 )
104
- @adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( @serializer , include : 'author,author.posts' )
105
-
106
- @first_comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
107
- @second_comment = Comment . new ( id : 2 , body : 'ZOMG ANOTHER COMMENT' )
108
- @first_post . comments = [ @first_comment , @second_comment ]
109
- @third_post . comments = [ ]
110
- @first_comment . post = @first_post
111
- @first_comment . author = nil
112
- @second_comment . post = @first_post
113
- @second_comment . author = nil
134
+ def test_include_multiple_posts_and_linked
135
+ serializer = BioSerializer . new @bio1
136
+ adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new (
137
+ serializer ,
138
+ include : [ 'author' , 'author.posts' ]
139
+ )
140
+ alt_adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new (
141
+ serializer ,
142
+ include : 'author,author.posts'
143
+ )
114
144
115
145
expected = {
116
- authors : [ {
117
- id : "1" ,
118
- name : "Steve K." ,
119
- links : {
120
- posts : [ "10" , "30" ] ,
121
- roles : [ ] ,
122
- bio : "1"
123
- }
124
- } ] ,
125
- posts : [ {
126
- title : "Hello!!" ,
127
- body : "Hello, world!!" ,
128
- id : "10" ,
129
- links : {
130
- comments : [ "1" , "2" ] ,
131
- blog : "999" ,
132
- author : "1"
146
+ authors : [
147
+ {
148
+ id : "1" ,
149
+ name : "Steve K." ,
150
+ links : {
151
+ posts : [ "10" , "30" ] ,
152
+ roles : [ ] ,
153
+ bio : "1"
154
+ }
133
155
}
134
- } , {
135
- title : "Yet Another Post" ,
136
- body : "Body" ,
137
- id : "30" ,
138
- links : {
139
- comments : [ ] ,
140
- blog : nil ,
141
- author : "1"
156
+ ] ,
157
+ posts : [
158
+ {
159
+ id : "10" ,
160
+ title : "Hello!!" ,
161
+ body : "Hello, world!!" ,
162
+ links : {
163
+ comments : [ "1" , "2" ] ,
164
+ blog : "999" ,
165
+ author : "1"
166
+ }
167
+ } , {
168
+ id : "30" ,
169
+ title : "Yet Another Post" ,
170
+ body : "Body" ,
171
+ links : {
172
+ comments : [ ] ,
173
+ blog : nil ,
174
+ author : "1"
175
+ }
142
176
}
143
- } ]
177
+ ]
144
178
}
145
- assert_equal expected , @adapter . serializable_hash [ :linked ]
179
+ assert_equal expected , adapter . serializable_hash [ :linked ]
180
+ assert_equal expected , alt_adapter . serializable_hash [ :linked ]
146
181
end
147
182
148
183
def test_ignore_model_namespace_for_linked_resource_type
0 commit comments