mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Don't treat has_one associations embed objects as an array
Closes #442 Thanks @arrtchiu for reporing and giving a test case
This commit is contained in:
@@ -130,7 +130,7 @@ module ActiveModel
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_using_a_given_array_serializer
|
||||
def test_associations_embedding_ids_using_a_given_array_serializer
|
||||
@association.embed = :ids
|
||||
@association.embed_in_root = true
|
||||
@association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
|
||||
@@ -144,6 +144,18 @@ module ActiveModel
|
||||
comments: { my_content: ['fake'] }
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_using_a_given_array_serializer
|
||||
@association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
|
||||
def serializable_object
|
||||
{ my_content: ['fake'] }
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -116,7 +116,7 @@ module ActiveModel
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_using_a_given_serializer
|
||||
def test_associations_embedding_ids_using_a_given_serializer
|
||||
@association.embed = :ids
|
||||
@association.embed_in_root = true
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
@@ -132,6 +132,20 @@ module ActiveModel
|
||||
'profiles' => [{ name: 'fake' }]
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_using_a_given_serializer
|
||||
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
||||
def name
|
||||
'fake'
|
||||
end
|
||||
|
||||
attributes :name
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'fake' } }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user