mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Don't concat to nil objects
This commit is contained in:
parent
df481b2b35
commit
3e510c829b
@ -46,7 +46,7 @@ module ActiveModel
|
|||||||
def embedded_in_root_associations
|
def embedded_in_root_associations
|
||||||
@object.each_with_object({}) do |item, hash|
|
@object.each_with_object({}) do |item, hash|
|
||||||
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
|
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
|
||||||
if hash.has_key?(type)
|
if hash[type]
|
||||||
hash[type].concat(objects).uniq!
|
hash[type].concat(objects).uniq!
|
||||||
else
|
else
|
||||||
hash[type] = objects
|
hash[type] = objects
|
||||||
|
|||||||
@ -108,6 +108,42 @@ module ActiveModel
|
|||||||
ensure
|
ensure
|
||||||
UserSerializer._associations[:profile] = @old_association
|
UserSerializer._associations[:profile] = @old_association
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_embed_object_in_root_for_has_one_association_with_nil_value
|
||||||
|
@association = UserSerializer._associations[:profile]
|
||||||
|
@old_association = @association.dup
|
||||||
|
|
||||||
|
@association.embed = :ids
|
||||||
|
@association.embed_in_root = true
|
||||||
|
|
||||||
|
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
||||||
|
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
||||||
|
|
||||||
|
class << @user1
|
||||||
|
def profile
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class << @user2
|
||||||
|
def profile
|
||||||
|
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
||||||
|
assert_equal({
|
||||||
|
users: [
|
||||||
|
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
||||||
|
{ name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id }
|
||||||
|
],
|
||||||
|
'profiles' => [
|
||||||
|
{ name: 'Name 1', description: 'Desc 1' }
|
||||||
|
]
|
||||||
|
}, @serializer.as_json)
|
||||||
|
ensure
|
||||||
|
UserSerializer._associations[:profile] = @old_association
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user