has_one serialized objects shouldn't be wrapped in an array and it's key is singular

This commit is contained in:
Santiago Pastorino 2013-10-10 19:37:23 -02:00
parent 23e6ed32be
commit 4f70dc2091
4 changed files with 13 additions and 14 deletions

View File

@ -152,7 +152,8 @@ module ActiveModel
if associated_data.respond_to?(:to_ary) if associated_data.respond_to?(:to_ary)
associated_data.map { |elem| association.build_serializer(elem).serializable_hash } associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
else else
[association.build_serializer(associated_data).serializable_hash] result = association.build_serializer(associated_data).serializable_hash
association.is_a?(Association::HasMany) ? [result] : result
end end
end end

View File

@ -12,7 +12,7 @@ module ActiveModel
@embed_in_root = @embed_ids && (options[:include] || SETTINGS[:include]) @embed_in_root = @embed_ids && (options[:include] || SETTINGS[:include])
@embed_key = options[:embed_key] || :id @embed_key = options[:embed_key] || :id
@key = options[:key] @key = options[:key]
@embedded_key = options[:root] @embedded_key = options[:root] || name
self.serializer_class = @options[:serializer] self.serializer_class = @options[:serializer]
end end
@ -41,7 +41,6 @@ module ActiveModel
def initialize(*args) def initialize(*args)
super super
@key ||= "#{name}_id" @key ||= "#{name}_id"
@embedded_key ||= name.pluralize
end end
end end
@ -49,7 +48,6 @@ module ActiveModel
def initialize(*args) def initialize(*args)
super super
@key ||= "#{name.singularize}_ids" @key ||= "#{name.singularize}_ids"
@embedded_key ||= name
end end
end end
end end

View File

@ -43,14 +43,14 @@ module ActiveModel
def test_associations_embedding_objects_serialization_using_serializable_hash def test_associations_embedding_objects_serialization_using_serializable_hash
@association.embed = :objects @association.embed = :objects
assert_equal({ assert_equal({
title: 'Title 1', body: 'Body 1', 'comments' => [{ content: 'C1' }, { content: 'C2' }] title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }]
}, @post_serializer.serializable_hash) }, @post_serializer.serializable_hash)
end end
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({
'post' => { 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({
'post' => { 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
@ -109,7 +109,7 @@ module ActiveModel
assert_equal({ assert_equal({
'post' => { 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 } },
'comments' => [{ content: 'fake' }, { content: 'fake' }] comments: [{ content: 'fake' }, { content: 'fake' }]
}, @post_serializer.as_json) }, @post_serializer.as_json)
end end
end end

View File

@ -43,14 +43,14 @@ module ActiveModel
def test_associations_embedding_objects_serialization_using_serializable_hash def test_associations_embedding_objects_serialization_using_serializable_hash
@association.embed = :objects @association.embed = :objects
assert_equal({ assert_equal({
name: 'Name 1', email: 'mail@server.com', 'profiles' => [{ name: 'N1', description: 'D1' }] name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' }
}, @user_serializer.serializable_hash) }, @user_serializer.serializable_hash)
end end
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({
'user' => { name: 'Name 1', email: 'mail@server.com', 'profiles' => [{ name: 'N1', description: 'D1' }] } 'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' } }
}, @user_serializer.as_json) }, @user_serializer.as_json)
end end
@ -75,7 +75,7 @@ module ActiveModel
end end
assert_equal({ assert_equal({
'user' => { name: 'Name 1', email: 'mail@server.com', 'profiles' => [nil] } 'user' => { name: 'Name 1', email: 'mail@server.com', profile: nil }
}, @user_serializer.as_json) }, @user_serializer.as_json)
end end
@ -83,7 +83,7 @@ module ActiveModel
@association.embed = :objects @association.embed = :objects
@association.embedded_key = 'root' @association.embedded_key = 'root'
assert_equal({ assert_equal({
name: 'Name 1', email: 'mail@server.com', 'root' => [{ name: 'N1', description: 'D1' }] name: 'Name 1', email: 'mail@server.com', 'root' => { name: 'N1', description: 'D1' }
}, @user_serializer.serializable_hash) }, @user_serializer.serializable_hash)
end end
@ -100,7 +100,7 @@ module ActiveModel
@user_serializer.root = nil @user_serializer.root = nil
assert_equal({ assert_equal({
'user' => { 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 },
'profiles' => [{ name: 'N1', description: 'D1' }] profile: { name: 'N1', description: 'D1' }
}, @user_serializer.as_json) }, @user_serializer.as_json)
end end
@ -117,7 +117,7 @@ module ActiveModel
assert_equal({ assert_equal({
'user' => { 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 },
'profiles' => [{ name: 'fake' }] profile: { name: 'fake' }
}, @user_serializer.as_json) }, @user_serializer.as_json)
end end
end end