Merge pull request #451 from plexus/embed_in_root_merge

embed_in_root only returns the associated objects of a single instance
This commit is contained in:
Santiago Pastorino 2013-12-16 10:39:53 -08:00
commit 5c00ce0443
3 changed files with 51 additions and 1 deletions

View File

@ -44,7 +44,10 @@ module ActiveModel
def embedded_in_root_associations
@object.each_with_object({}) do |item, hash|
hash.merge!(serializer_for(item).embedded_in_root_associations)
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
hash[type] = hash.fetch(type, []).concat(objects)
hash[type].uniq!
end
end
end
end

View File

@ -26,6 +26,8 @@ class Profile < Model
end
class Post < Model
attr_writer :comments
def comments
@comments ||= [Comment.new(content: 'C1'),
Comment.new(content: 'C2')]

View File

@ -0,0 +1,45 @@
require 'test_helper'
module ActiveModel
class ArraySerializer
class EmbedInRootTest < ActiveModel::TestCase
def setup
@association = PostSerializer._associations[:comments]
@old_association = @association.dup
@post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
@post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
@post2.comments = [
Comment.new(content: 'C3'),
Comment.new(content: 'C4')
]
@serializer = ArraySerializer.new([@post1, @post2], root: :posts)
end
def teardown
PostSerializer._associations[:comments] = @old_association
end
def test_associated_objects_of_multiple_instances_embedded_in_root
@association.embed = :ids
@association.embed_in_root = true
assert_equal({
posts: [
{title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
{title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
],
comments: [
{content: "C1"},
{content: "C2"},
{content: "C3"},
{content: "C4"}
]
}, @serializer.as_json)
end
end
end
end