Allow using root = false in initialize

This commit is contained in:
Santiago Pastorino 2013-09-14 21:15:04 -03:00
parent 8462a73f3a
commit 626a85bc3e
4 changed files with 23 additions and 2 deletions

View File

@ -15,7 +15,8 @@ module ActiveModel
def initialize(object, options={})
@object = object
@options = options
@root = options[:root] || self.class._root
@root = options[:root]
@root = self.class._root if @root.nil?
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
end

View File

@ -85,7 +85,8 @@ module ActiveModel
alias read_attribute_for_serialization send
def root=(root)
@root = root || self.class._root
@root = root
@root = self.class._root if @root.nil?
@root = self.class.root_name if auto_assign_root?
end

View File

@ -38,6 +38,16 @@ module ActiveModel
]
}, @serializer.as_json(root: 'argument'))
end
def test_using_false_root_in_initialize_takes_precedence
ArraySerializer._root = 'root'
@serializer = ArraySerializer.new([@profile1, @profile2], root: false)
assert_equal([
{ 'name' => 'Name 1', 'description' => 'Description 1' },
{ 'name' => 'Name 2', 'description' => 'Description 2' }
], @serializer.as_json)
end
end
class RootInSerializerTest < ActiveModel::TestCase

View File

@ -45,6 +45,15 @@ module ActiveModel
}
}, @serializer.as_json(root: 'argument'))
end
def test_using_false_root_in_initializer_takes_precedence
ProfileSerializer._root = 'root'
@serializer = ProfileSerializer.new(@profile, root: false)
assert_equal({
'name' => 'Name 1', 'description' => 'Description 1'
}, @serializer.as_json)
end
end
class RootInSerializerTest < ActiveModel::TestCase