Skip to content

Commit edfd38b

Browse files
committed
converted has many to use string key for association, makeing it compatable with has_one; fixed tests that relied on symbol keys for has_many associations
1 parent 34d684e commit edfd38b

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

lib/active_model/serializer/association/has_many.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ class Association
44
class HasMany < Association
55
def initialize(name, *args)
66
super
7-
@root_key = @embedded_key
7+
@root_key = @embedded_key.to_s
88
@key ||= case CONFIG.default_key_type
99
when :name then name.to_s.pluralize
1010
else "#{name.to_s.singularize}_ids"
1111
end
12-
1312
end
1413

1514
def serializer_class(object, _)
@@ -37,4 +36,4 @@ def use_array_serializer?
3736
end
3837
end
3938
end
40-
end
39+
end

test/integration/active_record/active_record_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def test_serialization_embedding_ids_including_in_root
4949
'ar_tag_ids' => [1, 2],
5050
'ar_section_id' => 1
5151
},
52-
ar_comments: [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
52+
'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
5353
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
54-
ar_tags: [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
54+
'ar_tags' => [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
5555
'ar_sections' => [{ 'name' => 'ruby' }]
5656
}, post_serializer.as_json)
5757
end

test/unit/active_model/array_serializer/serialization_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class << @post2
8585
{title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
8686
{title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
8787
],
88-
comments: [
88+
'comments' => [
8989
{content: "C1"},
9090
{content: "C2"},
9191
{content: "C3"},

test/unit/active_model/serializer/has_many_polymorphic_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_associations_embedding_ids_including_objects_serialization_using_as_jso
140140
{ id: c.object_id, type: model_name(c) }
141141
end,
142142
},
143-
attachments: [
143+
'attachments' => [
144144
{ type: :image, image: { url: 'U1' }},
145145
{ type: :video, video: { html: 'H1' }}
146146
]
@@ -153,7 +153,7 @@ def test_associations_embedding_nothing_including_objects_serialization_using_as
153153

154154
assert_equal({
155155
'mail' => { body: 'Body 1' },
156-
attachments: [
156+
'attachments' => [
157157
{ type: :image, image: { url: 'U1' }},
158158
{ type: :video, video: { html: 'H1' }}
159159
]
@@ -178,7 +178,7 @@ def fake
178178
{ id: c.object_id, type: model_name(c) }
179179
end
180180
},
181-
attachments: [
181+
'attachments' => [
182182
{ type: :image, image: { fake: 'fake' }},
183183
{ type: :video, video: { fake: 'fake' }}
184184
]

test/unit/active_model/serializer/has_many_test.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_associations_embedding_ids_including_objects_serialization_using_as_jso
111111

112112
assert_equal({
113113
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
114-
comments: [{ content: 'C1' }, { content: 'C2' }]
114+
'comments' => [{ content: 'C1' }, { content: 'C2' }]
115115
}, @post_serializer.as_json)
116116
end
117117

@@ -128,7 +128,7 @@ def test_associations_embedding_ids_including_objects_serialization_when_invoked
128128
name: 'Name 1',
129129
posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }]
130130
},
131-
comments: [{ content: 'C1' }, { content: 'C2' }]
131+
"comments" => [{ content: 'C1' }, { content: 'C2' }]
132132
}, category_serializer.as_json)
133133
end
134134

@@ -138,7 +138,7 @@ def test_associations_embedding_nothing_including_objects_serialization_using_as
138138

139139
assert_equal({
140140
'post' => { title: 'Title 1', body: 'Body 1' },
141-
comments: [{ content: 'C1' }, { content: 'C2' }]
141+
'comments' => [{ content: 'C1' }, { content: 'C2' }]
142142
}, @post_serializer.as_json)
143143
end
144144

@@ -155,7 +155,7 @@ def content
155155

156156
assert_equal({
157157
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
158-
comments: [{ content: 'C1!' }, { content: 'C2!' }]
158+
'comments' => [{ content: 'C1!' }, { content: 'C2!' }]
159159
}, @post_serializer.as_json)
160160
end
161161

@@ -170,7 +170,7 @@ def serializable_object
170170

171171
assert_equal({
172172
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
173-
comments: { my_content: ['fake'] }
173+
'comments' => { my_content: ['fake'] }
174174
}, @post_serializer.as_json)
175175
end
176176

@@ -191,16 +191,16 @@ def test_associations_embedding_ids_including_objects_serialization_with_embed_i
191191
@association.embed_in_root_key = :linked
192192
@association.embed = :ids
193193
assert_equal({
194+
'post' => {
195+
title: 'Title 1', body: 'Body 1',
196+
'comment_ids' => @post.comments.map(&:object_id)
197+
},
194198
linked: {
195-
comments: [
199+
'comments' => [
196200
{ content: 'C1' },
197201
{ content: 'C2' }
198202
]
199203
},
200-
'post' => {
201-
title: 'Title 1', body: 'Body 1',
202-
'comment_ids' => @post.comments.map(&:object_id)
203-
}
204204
}, @post_serializer.as_json)
205205
end
206206

@@ -211,18 +211,18 @@ def test_associations_embedding_ids_using_embed_namespace_including_object_seria
211211
@association.embed_namespace = :links
212212
@association.key = :comments
213213
assert_equal({
214-
linked: {
215-
comments: [
216-
{ content: 'C1' },
217-
{ content: 'C2' }
218-
]
219-
},
220214
'post' => {
221215
title: 'Title 1', body: 'Body 1',
222216
links: {
223217
comments: @post.comments.map(&:object_id)
224218
}
225-
}
219+
},
220+
linked: {
221+
'comments' => [
222+
{ content: 'C1' },
223+
{ content: 'C2' }
224+
]
225+
},
226226
}, @post_serializer.as_json)
227227
end
228228

0 commit comments

Comments
 (0)