mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +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)
|
||||
@root = root
|
||||
@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
|
||||
|
||||
def attributes
|
||||
@ -142,11 +142,5 @@ module ActiveModel
|
||||
hash.merge! associations
|
||||
end
|
||||
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
|
||||
|
||||
@ -14,7 +14,7 @@ module ActionController
|
||||
def test_render_using_implicit_serializer
|
||||
get :render_using_implicit_serializer
|
||||
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
|
||||
|
||||
@ -36,7 +36,7 @@ module ActionController
|
||||
def test_render_using_implicit_serializer_and_scope
|
||||
get :render_using_implicit_serializer_and_scope
|
||||
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
|
||||
|
||||
@ -62,7 +62,7 @@ module ActionController
|
||||
def test_render_using_implicit_serializer_and_explicit_scope
|
||||
get :render_using_implicit_serializer_and_explicit_scope
|
||||
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
|
||||
|
||||
@ -88,7 +88,7 @@ module ActionController
|
||||
def test_render_overriding_serialization_scope
|
||||
get :render_overriding_serialization_scope
|
||||
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
|
||||
|
||||
@ -112,7 +112,7 @@ module ActionController
|
||||
def test_render_calling_serialization_scope
|
||||
get :render_calling_serialization_scope
|
||||
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
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ module ActiveModel
|
||||
|
||||
def test_attributes_serialization_using_as_json
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
'ar_profile' => { 'name' => 'Name 1', 'description' => 'Description 1' }
|
||||
}, @profile_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -21,7 +21,7 @@ module ActiveModel
|
||||
|
||||
def test_attributes_serialization_using_as_json
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
'profile' => { 'name' => 'Name 1', 'description' => 'Description 1' }
|
||||
}, @profile_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -29,7 +29,7 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_as_json
|
||||
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)
|
||||
end
|
||||
|
||||
@ -50,7 +50,7 @@ module ActiveModel
|
||||
def test_associations_embedding_objects_serialization_using_as_json
|
||||
@association.embed = :objects
|
||||
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)
|
||||
end
|
||||
|
||||
@ -63,7 +63,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [nil]
|
||||
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comments' => [nil] }
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_as_json
|
||||
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)
|
||||
end
|
||||
|
||||
@ -50,7 +50,7 @@ module ActiveModel
|
||||
def test_associations_embedding_objects_serialization_using_as_json
|
||||
@association.embed = :objects
|
||||
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)
|
||||
end
|
||||
|
||||
@ -63,7 +63,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user