mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
parent
3cbcf461a3
commit
67c550f2ee
@ -149,15 +149,28 @@ end
|
|||||||
associations.each_with_object({}) do |(name, association), hash|
|
associations.each_with_object({}) do |(name, association), hash|
|
||||||
if included_associations.include? name
|
if included_associations.include? name
|
||||||
if association.embed_in_root?
|
if association.embed_in_root?
|
||||||
hash[association.root_key] = serialize association
|
association_serializer = build_serializer(association)
|
||||||
|
hash.merge! association_serializer.embedded_in_root_associations
|
||||||
|
|
||||||
|
serialized_data = association_serializer.serializable_object
|
||||||
|
key = association.root_key
|
||||||
|
if hash.has_key?(key)
|
||||||
|
hash[key].concat(serialized_data).uniq!
|
||||||
|
else
|
||||||
|
hash[key] = serialized_data
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize(association)
|
def build_serializer(association)
|
||||||
object = send(association.name)
|
object = send(association.name)
|
||||||
association.build_serializer(object, scope: scope).serializable_object
|
association.build_serializer(object, scope: scope)
|
||||||
|
end
|
||||||
|
|
||||||
|
def serialize(association)
|
||||||
|
build_serializer(association).serializable_object
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_ids(association)
|
def serialize_ids(association)
|
||||||
|
|||||||
6
test/fixtures/active_record.rb
vendored
6
test/fixtures/active_record.rb
vendored
@ -80,13 +80,13 @@ ARPost.create(title: 'New post',
|
|||||||
|
|
||||||
short_tag = post.ar_tags.create(name: 'short')
|
short_tag = post.ar_tags.create(name: 'short')
|
||||||
whiny_tag = post.ar_tags.create(name: 'whiny')
|
whiny_tag = post.ar_tags.create(name: 'whiny')
|
||||||
happy_tag = post.ar_tags.create(name: 'happy')
|
happy_tag = ARTag.create(name: 'happy')
|
||||||
|
|
||||||
post.ar_comments.create(body: 'what a dumb post').tap do |comment|
|
post.ar_comments.create(body: 'what a dumb post').tap do |comment|
|
||||||
comment.ar_tags.concat short_tag, whiny_tag
|
comment.ar_tags.concat happy_tag, whiny_tag
|
||||||
end
|
end
|
||||||
|
|
||||||
post.ar_comments.create(body: 'i liked it').tap do |comment|
|
post.ar_comments.create(body: 'i liked it').tap do |comment|
|
||||||
comment.ar_tags.concat short_tag, happy_tag
|
comment.ar_tags.concat happy_tag, short_tag
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,9 +14,9 @@ module ActiveModel
|
|||||||
assert_equal({
|
assert_equal({
|
||||||
'ar_post' => {
|
'ar_post' => {
|
||||||
title: 'New post', body: 'A body!!!',
|
title: 'New post', body: 'A body!!!',
|
||||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'happy' }, { name: 'whiny' }] },
|
||||||
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
{ body: 'i liked it', ar_tags: [{:name=>"happy"}, {:name=>"short"}] }],
|
||||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
ar_tags: [{ name: 'short' }, { name: 'whiny' }],
|
||||||
ar_section: { 'name' => 'ruby' }
|
ar_section: { 'name' => 'ruby' }
|
||||||
}
|
}
|
||||||
}, post_serializer.as_json)
|
}, post_serializer.as_json)
|
||||||
@ -30,7 +30,7 @@ module ActiveModel
|
|||||||
'ar_post' => {
|
'ar_post' => {
|
||||||
title: 'New post', body: 'A body!!!',
|
title: 'New post', body: 'A body!!!',
|
||||||
'ar_comment_ids' => [1, 2],
|
'ar_comment_ids' => [1, 2],
|
||||||
'ar_tag_ids' => [1, 2, 3],
|
'ar_tag_ids' => [1, 2],
|
||||||
'ar_section_id' => 1
|
'ar_section_id' => 1
|
||||||
}
|
}
|
||||||
}, post_serializer.as_json)
|
}, post_serializer.as_json)
|
||||||
@ -41,18 +41,20 @@ module ActiveModel
|
|||||||
post_serializer = ARPostSerializer.new(@post)
|
post_serializer = ARPostSerializer.new(@post)
|
||||||
|
|
||||||
embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
|
embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
|
||||||
assert_equal({
|
embed(ARCommentSerializer, embed: :ids, embed_in_root: true) do
|
||||||
'ar_post' => {
|
assert_equal({
|
||||||
title: 'New post', body: 'A body!!!',
|
'ar_post' => {
|
||||||
'ar_comment_ids' => [1, 2],
|
title: 'New post', body: 'A body!!!',
|
||||||
'ar_tag_ids' => [1, 2, 3],
|
'ar_comment_ids' => [1, 2],
|
||||||
'ar_section_id' => 1
|
'ar_tag_ids' => [1, 2],
|
||||||
},
|
'ar_section_id' => 1
|
||||||
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
},
|
||||||
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
ar_comments: [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
|
||||||
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
|
||||||
'ar_sections' => [{ 'name' => 'ruby' }]
|
ar_tags: [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
|
||||||
}, post_serializer.as_json)
|
'ar_sections' => [{ 'name' => 'ruby' }]
|
||||||
|
}, post_serializer.as_json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -130,7 +130,7 @@ module ActiveModel
|
|||||||
@association.embed_in_root = true
|
@association.embed_in_root = true
|
||||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||||
def content
|
def content
|
||||||
'fake'
|
object.read_attribute_for_serialization(:content) + '!'
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes :content
|
attributes :content
|
||||||
@ -138,7 +138,7 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||||
comments: [{ content: 'fake' }, { content: 'fake' }]
|
comments: [{ content: 'C1!' }, { content: 'C2!' }]
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user