The Array serializer should not make a child root for every item in the Array

This commit is contained in:
Yehuda Katz 2011-12-29 18:13:25 -08:00
parent 1e0c9ef93b
commit e23553be23
2 changed files with 16 additions and 1 deletions

View File

@ -26,7 +26,8 @@ module ActiveModel
def as_json(*args)
@hash = {}
array = serializable_array.as_json(*args)
array = serializable_array.map(&:serializable_hash)
if root = @options[:root]
@hash.merge!(root => array)

View File

@ -416,6 +416,20 @@ class SerializerTest < ActiveModel::TestCase
], serializer.as_json)
end
def test_array_serializer
comment1 = Comment.new(:title => "Comment1", :id => 1)
comment2 = Comment.new(:title => "Comment2", :id => 2)
array = [ comment1, comment2 ]
serializer = array.active_model_serializer.new(array, nil, :root => :comments)
assert_equal({ :comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
]}, serializer.as_json)
end
class CustomBlog < Blog
attr_accessor :public_posts, :public_user
end