mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #264 from vad4msiu/features/root_element
Support for setting root element
This commit is contained in:
commit
bb8900e308
@ -304,7 +304,12 @@ module ActiveModel
|
|||||||
return false if self._root == false
|
return false if self._root == false
|
||||||
|
|
||||||
class_name = self.class.name.demodulize.underscore.sub(/_serializer$/, '').to_sym unless self.class.name.blank?
|
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
|
end
|
||||||
|
|
||||||
def url_options
|
def url_options
|
||||||
|
|||||||
@ -329,6 +329,16 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
|
assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
|
||||||
end
|
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
|
def test_root_false_on_load_active_model_serializers
|
||||||
begin
|
begin
|
||||||
ActiveSupport.on_load(:active_model_serializers) do
|
ActiveSupport.on_load(:active_model_serializers) do
|
||||||
|
|||||||
@ -154,6 +154,10 @@ class BlogSerializer < ActiveModel::Serializer
|
|||||||
has_one :author, :serializer => AuthorSerializer
|
has_one :author, :serializer => AuthorSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class BlogWithRootSerializer < BlogSerializer
|
||||||
|
root true
|
||||||
|
end
|
||||||
|
|
||||||
class CustomPostSerializer < ActiveModel::Serializer
|
class CustomPostSerializer < ActiveModel::Serializer
|
||||||
attributes :title
|
attributes :title
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user