mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Allow using root = false in initialize
This commit is contained in:
parent
8462a73f3a
commit
626a85bc3e
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user