mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Don't wrap array items in root element
This commit is contained in:
parent
ee846f39af
commit
258248d6c0
@ -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
|
||||
|
||||
@ -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]
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user