Support for setting root element

This commit is contained in:
vad4msiu 2013-04-04 15:10:29 +04:00
parent ab5d40f38b
commit 95081410d2
3 changed files with 20 additions and 1 deletions

View File

@ -304,7 +304,12 @@ module ActiveModel
return false if self._root == false
class_name = self.class.name.demodulize.underscore.sub(/_serializer$/, '').to_sym unless self.class.name.blank?
self._root || class_name
if self._root == true
class_name
else
self._root || class_name
end
end
def url_options

View File

@ -318,6 +318,16 @@ class SerializerTest < ActiveModel::TestCase
assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
end
def test_true_root
blog = Blog.new
assert_equal({
:blog_with_root => {
:author => nil,
}
}, BlogWithRootSerializer.new(blog).as_json)
end
def test_root_false_on_load_active_model_serializers
begin
ActiveSupport.on_load(:active_model_serializers) do

View File

@ -146,6 +146,10 @@ class BlogSerializer < ActiveModel::Serializer
has_one :author, :serializer => AuthorSerializer
end
class BlogWithRootSerializer < BlogSerializer
root true
end
class CustomPostSerializer < ActiveModel::Serializer
attributes :title
end