mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Use serializer name as root when root not set
This commit is contained in:
parent
626a85bc3e
commit
6f3503c965
@ -87,7 +87,7 @@ module ActiveModel
|
|||||||
def root=(root)
|
def root=(root)
|
||||||
@root = root
|
@root = root
|
||||||
@root = self.class._root if @root.nil?
|
@root = self.class._root if @root.nil?
|
||||||
@root = self.class.root_name if auto_assign_root?
|
@root = self.class.root_name if @root == true || @root.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
@ -142,11 +142,5 @@ module ActiveModel
|
|||||||
hash.merge! associations
|
hash.merge! associations
|
||||||
end
|
end
|
||||||
alias serializable_object serializable_hash
|
alias serializable_object serializable_hash
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def auto_assign_root?
|
|
||||||
@root == true || !@root && self.class._associations.any? { |a| a.embed_in_root? }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -14,7 +14,7 @@ module ActionController
|
|||||||
def test_render_using_implicit_serializer
|
def test_render_using_implicit_serializer
|
||||||
get :render_using_implicit_serializer
|
get :render_using_implicit_serializer
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
|
assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ module ActionController
|
|||||||
def test_render_using_implicit_serializer_and_scope
|
def test_render_using_implicit_serializer_and_scope
|
||||||
get :render_using_implicit_serializer_and_scope
|
get :render_using_implicit_serializer_and_scope
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_user"}', @response.body
|
assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ module ActionController
|
|||||||
def test_render_using_implicit_serializer_and_explicit_scope
|
def test_render_using_implicit_serializer_and_explicit_scope
|
||||||
get :render_using_implicit_serializer_and_explicit_scope
|
get :render_using_implicit_serializer_and_explicit_scope
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_admin"}', @response.body
|
assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ module ActionController
|
|||||||
def test_render_overriding_serialization_scope
|
def test_render_overriding_serialization_scope
|
||||||
get :render_overriding_serialization_scope
|
get :render_overriding_serialization_scope
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_admin"}', @response.body
|
assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ module ActionController
|
|||||||
def test_render_calling_serialization_scope
|
def test_render_calling_serialization_scope
|
||||||
get :render_calling_serialization_scope
|
get :render_calling_serialization_scope
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_user"}', @response.body
|
assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -22,7 +22,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_attributes_serialization_using_as_json
|
def test_attributes_serialization_using_as_json
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'description' => 'Description 1'
|
'ar_profile' => { 'name' => 'Name 1', 'description' => 'Description 1' }
|
||||||
}, @profile_serializer.as_json)
|
}, @profile_serializer.as_json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -21,7 +21,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_attributes_serialization_using_as_json
|
def test_attributes_serialization_using_as_json
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'description' => 'Description 1'
|
'profile' => { 'name' => 'Name 1', 'description' => 'Description 1' }
|
||||||
}, @profile_serializer.as_json)
|
}, @profile_serializer.as_json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -29,7 +29,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_serialization_using_as_json
|
def test_associations_embedding_ids_serialization_using_as_json
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ module ActiveModel
|
|||||||
def test_associations_embedding_objects_serialization_using_as_json
|
def test_associations_embedding_objects_serialization_using_as_json
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }] }
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [nil]
|
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [nil] }
|
||||||
}, @post_serializer.as_json)
|
}, @post_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -29,7 +29,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_associations_embedding_ids_serialization_using_as_json
|
def test_associations_embedding_ids_serialization_using_as_json
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
|
'user' => { 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id }
|
||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ module ActiveModel
|
|||||||
def test_associations_embedding_objects_serialization_using_as_json
|
def test_associations_embedding_objects_serialization_using_as_json
|
||||||
@association.embed = :objects
|
@association.embed = :objects
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profiles' => [{ 'name' => 'N1', 'description' => 'D1' }]
|
'user' => { 'name' => 'Name 1', 'email' => 'mail@server.com', 'profiles' => [{ 'name' => 'N1', 'description' => 'D1' }] }
|
||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profiles' => [nil]
|
'user' => { 'name' => 'Name 1', 'email' => 'mail@server.com', 'profiles' => [nil] }
|
||||||
}, @user_serializer.as_json)
|
}, @user_serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user