Don't wrap array items in root element

This commit is contained in:
Thomas Scholtes 2013-05-24 10:23:59 +02:00
parent ee846f39af
commit 258248d6c0
3 changed files with 23 additions and 1 deletions

View File

@ -53,7 +53,7 @@ module ActiveModel
serializer = DefaultSerializer
end
serializable = serializer.new(item, options)
serializable = serializer.new(item, options.merge(:root => nil))
if serializable.respond_to?(:serializable_hash)
serializable.serializable_hash

View File

@ -31,6 +31,20 @@ class ArraySerializerTest < ActiveModel::TestCase
]}, serializer.as_json)
end
def test_active_model_with_root
comment1 = ModelWithActiveModelSerializer.new(:title => "Comment1")
comment2 = ModelWithActiveModelSerializer.new(:title => "Comment2")
array = [ comment1, comment2 ]
serializer = array.active_model_serializer.new(array, :root => :comments)
assert_equal({ :comments => [
{ :title => "Comment1" },
{ :title => "Comment2" }
]}, serializer.as_json)
end
def test_array_serializer_with_hash
hash = {:value => "something"}
array = [hash]

View File

@ -12,6 +12,14 @@ class Model
end
end
class ModelWithActiveModelSerializer < Model
include ActiveModel::Serializers::JSON
attr_accessor :attributes
def read_attribute_for_serialization(name)
@attributes[name]
end
end
class User
include ActiveModel::SerializerSupport