@@ -19,19 +19,25 @@ class RelationshipAuthorSerializer < ActiveModel::Serializer
19
19
20
20
has_many :locations do
21
21
link :related do
22
- ids = object . locations . map! ( &:id ) . join ( ',' )
22
+ ids = object . locations . map ( &:id ) . join ( ',' )
23
23
href "//example.com/locations/#{ ids } "
24
24
end
25
25
end
26
26
27
27
has_many :posts do
28
28
link :related do
29
- ids = object . posts . map! ( &:id ) . join ( ',' )
29
+ ids = object . posts . map ( &:id ) . join ( ',' )
30
30
href "//example.com/posts/#{ ids } "
31
31
meta ids : ids
32
32
end
33
33
end
34
34
35
+ has_many :comments do
36
+ link :self do
37
+ meta ids : [ 1 ]
38
+ end
39
+ end
40
+
35
41
has_many :roles do
36
42
meta count : object . posts . count
37
43
end
@@ -48,7 +54,7 @@ class RelationshipAuthorSerializer < ActiveModel::Serializer
48
54
49
55
has_many :likes do
50
56
link :related do
51
- ids = object . likes . map! ( &:id ) . join ( ',' )
57
+ ids = object . likes . map ( &:id ) . join ( ',' )
52
58
href "//example.com/likes/#{ ids } "
53
59
meta ids : ids
54
60
end
@@ -65,6 +71,7 @@ def setup
65
71
@profile = Profile . new ( id : 1337 )
66
72
@location = Location . new ( id : 1337 )
67
73
@reviewer = Author . new ( id : 1337 )
74
+ @comment = Comment . new ( id : 1337 )
68
75
@author = RelationshipAuthor . new (
69
76
id : 1337 ,
70
77
posts : [ @post ] ,
@@ -74,12 +81,12 @@ def setup
74
81
likes : [ @like ] ,
75
82
roles : [ @role ] ,
76
83
locations : [ @location ] ,
77
- profile : @profile
84
+ profile : @profile ,
85
+ comments : [ @comment ]
78
86
)
79
87
end
80
88
81
89
def test_relationship_simple_link
82
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
83
90
expected = {
84
91
data : {
85
92
id : '1337' ,
@@ -89,31 +96,28 @@ def test_relationship_simple_link
89
96
self : '//example.com/link_author/relationships/bio'
90
97
}
91
98
}
92
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ : bio] )
99
+ assert_relationship ( : bio, expected )
93
100
end
94
101
95
102
def test_relationship_block_link
96
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
97
103
expected = {
98
104
data : { id : '1337' , type : 'profiles' } ,
99
105
links : { related : '//example.com/profiles/1337' }
100
106
}
101
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ : profile] )
107
+ assert_relationship ( : profile, expected )
102
108
end
103
109
104
110
def test_relationship_block_link_href
105
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
106
111
expected = {
107
112
data : [ { id : '1337' , type : 'locations' } ] ,
108
113
links : {
109
114
related : { href : '//example.com/locations/1337' }
110
115
}
111
116
}
112
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ : locations] )
117
+ assert_relationship ( : locations, expected )
113
118
end
114
119
115
- def test_relationship_block_link_meta
116
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
120
+ def test_relationship_block_link_href_and_meta
117
121
expected = {
118
122
data : [ { id : '1337' , type : 'posts' } ] ,
119
123
links : {
@@ -123,37 +127,45 @@ def test_relationship_block_link_meta
123
127
}
124
128
}
125
129
}
126
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ :posts ] )
130
+ assert_relationship ( :posts , expected )
131
+ end
132
+
133
+ def test_relationship_block_link_meta
134
+ expected = {
135
+ data : [ { id : '1337' , type : 'comments' } ] ,
136
+ links : {
137
+ self : {
138
+ meta : { ids : [ 1 ] }
139
+ }
140
+ }
141
+ }
142
+ assert_relationship ( :comments , expected )
127
143
end
128
144
129
145
def test_relationship_meta
130
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
131
146
expected = {
132
147
data : [ { id : '1337' , type : 'roles' } ] ,
133
148
meta : { count : 1 }
134
149
}
135
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ : roles] )
150
+ assert_relationship ( : roles, expected )
136
151
end
137
152
138
153
def test_relationship_not_including_data
139
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
140
154
expected = {
141
155
links : { self : '//example.com/link_author/relationships/blog' }
142
156
}
143
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ : blog] )
157
+ assert_relationship ( : blog, expected )
144
158
end
145
159
146
160
def test_relationship_including_data_explicit
147
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
148
161
expected = {
149
162
data : { id : '1337' , type : 'authors' } ,
150
163
meta : { name : 'Dan Brown' }
151
164
}
152
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ : reviewer] )
165
+ assert_relationship ( : reviewer, expected )
153
166
end
154
167
155
168
def test_relationship_with_everything
156
- hash = serializable ( @author , adapter : :json_api ) . serializable_hash
157
169
expected = {
158
170
data : [ { id : '1337' , type : 'likes' } ] ,
159
171
links : {
@@ -164,7 +176,14 @@ def test_relationship_with_everything
164
176
} ,
165
177
meta : { liked : true }
166
178
}
167
- assert_equal ( expected , hash [ :data ] [ :relationships ] [ :likes ] )
179
+ assert_relationship ( :likes , expected )
180
+ end
181
+
182
+ private
183
+
184
+ def assert_relationship ( relationship_name , expected )
185
+ hash = serializable ( @author , adapter : :json_api ) . serializable_hash
186
+ assert_equal ( expected , hash [ :data ] [ :relationships ] [ relationship_name ] )
168
187
end
169
188
end
170
189
end
0 commit comments