mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
has_one serialized objects shouldn't be wrapped in an array and it's key is singular
This commit is contained in:
parent
23e6ed32be
commit
4f70dc2091
@ -152,7 +152,8 @@ module ActiveModel
|
||||
if associated_data.respond_to?(:to_ary)
|
||||
associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
|
||||
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
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ module ActiveModel
|
||||
@embed_in_root = @embed_ids && (options[:include] || SETTINGS[:include])
|
||||
@embed_key = options[:embed_key] || :id
|
||||
@key = options[:key]
|
||||
@embedded_key = options[:root]
|
||||
@embedded_key = options[:root] || name
|
||||
|
||||
self.serializer_class = @options[:serializer]
|
||||
end
|
||||
@ -41,7 +41,6 @@ module ActiveModel
|
||||
def initialize(*args)
|
||||
super
|
||||
@key ||= "#{name}_id"
|
||||
@embedded_key ||= name.pluralize
|
||||
end
|
||||
end
|
||||
|
||||
@ -49,7 +48,6 @@ module ActiveModel
|
||||
def initialize(*args)
|
||||
super
|
||||
@key ||= "#{name.singularize}_ids"
|
||||
@embedded_key ||= name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -43,14 +43,14 @@ module ActiveModel
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||
@association.embed = :objects
|
||||
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)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_as_json
|
||||
@association.embed = :objects
|
||||
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)
|
||||
end
|
||||
|
||||
@ -63,7 +63,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'post' => { title: 'Title 1', body: 'Body 1', 'comments' => [nil] }
|
||||
'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] }
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -109,7 +109,7 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'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)
|
||||
end
|
||||
end
|
||||
|
||||
@ -43,14 +43,14 @@ module ActiveModel
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||
@association.embed = :objects
|
||||
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)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_as_json
|
||||
@association.embed = :objects
|
||||
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)
|
||||
end
|
||||
|
||||
@ -75,7 +75,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
@ -83,7 +83,7 @@ module ActiveModel
|
||||
@association.embed = :objects
|
||||
@association.embedded_key = 'root'
|
||||
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)
|
||||
end
|
||||
|
||||
@ -100,7 +100,7 @@ module ActiveModel
|
||||
@user_serializer.root = nil
|
||||
assert_equal({
|
||||
'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)
|
||||
end
|
||||
|
||||
@ -117,7 +117,7 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
||||
'profiles' => [{ name: 'fake' }]
|
||||
profile: { name: 'fake' }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user