mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Store attributes as they are instead of converting them into Strings
This commit is contained in:
parent
10e882a14f
commit
75e9a2599d
@ -39,7 +39,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def attributes(*attrs)
|
||||
@_attributes.concat attrs.map(&:to_s)
|
||||
@_attributes.concat attrs
|
||||
|
||||
attrs.each do |attr|
|
||||
unless method_defined?(attr)
|
||||
|
||||
@ -10,19 +10,19 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_attributes_definition
|
||||
assert_equal(['name', 'description'],
|
||||
assert_equal([:name, :description],
|
||||
@profile_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_attributes_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}, @profile_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_attributes_serialization_using_as_json
|
||||
assert_equal({
|
||||
'ar_profile' => { 'name' => 'Name 1', 'description' => 'Description 1' }
|
||||
'ar_profile' => { name: 'Name 1', description: 'Description 1' }
|
||||
}, @profile_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -16,11 +16,11 @@ module ActiveModel
|
||||
assert_equal({
|
||||
'profiles' => [
|
||||
{
|
||||
'name' => 'Name 1',
|
||||
'description' => 'Description 1'
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
}, {
|
||||
'name' => 'Name 2',
|
||||
'description' => 'Description 2'
|
||||
name: 'Name 2',
|
||||
description: 'Description 2'
|
||||
}
|
||||
],
|
||||
'meta' => {
|
||||
@ -36,11 +36,11 @@ module ActiveModel
|
||||
assert_equal({
|
||||
'profiles' => [
|
||||
{
|
||||
'name' => 'Name 1',
|
||||
'description' => 'Description 1'
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
}, {
|
||||
'name' => 'Name 2',
|
||||
'description' => 'Description 2'
|
||||
name: 'Name 2',
|
||||
description: 'Description 2'
|
||||
}
|
||||
],
|
||||
'my_meta' => {
|
||||
|
||||
@ -16,16 +16,16 @@ module ActiveModel
|
||||
|
||||
def test_root_is_not_displayed_using_serializable_array
|
||||
assert_equal([
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
], @serializer.serializable_array)
|
||||
end
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'initialize' => [
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -33,8 +33,8 @@ module ActiveModel
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => [
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
}, @serializer.as_json(root: 'argument'))
|
||||
end
|
||||
@ -44,8 +44,8 @@ module ActiveModel
|
||||
@serializer = ArraySerializer.new([@profile1, @profile2], root: false)
|
||||
|
||||
assert_equal([
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
], @serializer.as_json)
|
||||
end
|
||||
end
|
||||
@ -66,16 +66,16 @@ module ActiveModel
|
||||
|
||||
def test_root_is_not_displayed_using_serializable_hash
|
||||
assert_equal([
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
], @serializer.serializable_array)
|
||||
end
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'in_serializer' => [
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -83,8 +83,8 @@ module ActiveModel
|
||||
def test_root_in_initializer_takes_precedence
|
||||
assert_equal({
|
||||
'initialize' => [
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
}, @rooted_serializer.as_json)
|
||||
end
|
||||
@ -92,8 +92,8 @@ module ActiveModel
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => [
|
||||
{ 'name' => 'Name 1', 'description' => 'Description 1' },
|
||||
{ 'name' => 'Name 2', 'description' => 'Description 2' }
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
}, @rooted_serializer.as_json(root: :argument))
|
||||
end
|
||||
|
||||
@ -8,8 +8,8 @@ module ActiveModel
|
||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||
serializer = ArraySerializer.new(array, scope: current_user)
|
||||
|
||||
expected = [{'name' => 'Name 1', 'description' => 'Description 1 - user'},
|
||||
{'name' => 'Name 2', 'description' => 'Description 2 - user'}]
|
||||
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
||||
{ name: 'Name 2', description: 'Description 2 - user' }]
|
||||
|
||||
assert_equal expected, serializer.serializable_array
|
||||
end
|
||||
|
||||
@ -22,8 +22,8 @@ module ActiveModel
|
||||
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||
serializer = ArraySerializer.new(array)
|
||||
|
||||
expected = [{'name' => 'Name 1', 'description' => 'Description 1'},
|
||||
{'name' => 'Name 2', 'description' => 'Description 2'}]
|
||||
expected = [{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }]
|
||||
|
||||
assert_equal expected, serializer.serializable_array
|
||||
assert_equal expected, serializer.as_json
|
||||
@ -34,8 +34,8 @@ module ActiveModel
|
||||
::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
||||
serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
|
||||
|
||||
expected = [{'name' => 'Name 1', 'description' => 'Description 1'},
|
||||
{'name' => 'Name 2', 'description' => 'Description 2'}]
|
||||
expected = [{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }]
|
||||
|
||||
assert_equal expected, serializer.serializable_array
|
||||
assert_equal expected, serializer.as_json
|
||||
|
||||
@ -9,19 +9,19 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_attributes_definition
|
||||
assert_equal(['name', 'description'],
|
||||
assert_equal([:name, :description],
|
||||
@profile_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_attributes_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}, @profile_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_attributes_serialization_using_as_json
|
||||
assert_equal({
|
||||
'profile' => { 'name' => 'Name 1', 'description' => 'Description 1' }
|
||||
'profile' => { name: 'Name 1', description: 'Description 1' }
|
||||
}, @profile_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -23,34 +23,34 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||
title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_as_json
|
||||
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 } }
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
||||
@association.key = 'key'
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
|
||||
title: 'Title 1', body: 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
@ -71,7 +71,7 @@ module ActiveModel
|
||||
@association.embed = :objects
|
||||
@association.embedded_key = 'root'
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'root' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }]
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
@ -79,7 +79,7 @@ module ActiveModel
|
||||
@association.embed_in_root = true
|
||||
@post_serializer.root = nil
|
||||
assert_equal({
|
||||
'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||
title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
||||
}, @post_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
@ -89,8 +89,8 @@ module ActiveModel
|
||||
|
||||
@post_serializer.root = nil
|
||||
assert_equal({
|
||||
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||
'comments' => [{ 'content' => 'C1' }, { 'content' => 'C2' }]
|
||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
||||
}, @post_serializer.as_json)
|
||||
ensure
|
||||
SETTINGS.clear
|
||||
@ -108,8 +108,8 @@ module ActiveModel
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'post' => { 'title' => 'Title 1', 'body' => 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||
'comments' => [{ 'content' => 'fake' }, { 'content' => 'fake' }]
|
||||
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
||||
'comments' => [{ content: 'fake' }, { content: 'fake' }]
|
||||
}, @post_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -23,34 +23,34 @@ module ActiveModel
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_as_json
|
||||
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 }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
||||
@association.key = 'key'
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'key' => @user.profile.object_id
|
||||
name: 'Name 1', email: 'mail@server.com', 'key' => @user.profile.object_id
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
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', 'profiles' => [{ 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', 'profiles' => [{ name: 'N1', description: 'D1' }] }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -63,7 +63,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'user' => { 'name' => 'Name 1', 'email' => 'mail@server.com', 'profiles' => [nil] }
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profiles' => [nil] }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -71,7 +71,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
|
||||
|
||||
@ -79,7 +79,7 @@ module ActiveModel
|
||||
@association.embed_in_root = true
|
||||
@user_serializer.root = nil
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
@ -87,8 +87,8 @@ module ActiveModel
|
||||
@association.embed_in_root = true
|
||||
@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' }]
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
||||
'profiles' => [{ name: 'N1', description: 'D1' }]
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -104,8 +104,8 @@ module ActiveModel
|
||||
end
|
||||
|
||||
assert_equal({
|
||||
'user' => { 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
||||
'profiles' => [{ 'name' => 'fake' }]
|
||||
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
||||
'profiles' => [{ name: 'fake' }]
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
@ -12,8 +12,8 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'profile' => {
|
||||
'name' => 'Name 1',
|
||||
'description' => 'Description 1'
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
},
|
||||
'meta' => {
|
||||
'total' => 10
|
||||
@ -26,8 +26,8 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'profile' => {
|
||||
'name' => 'Name 1',
|
||||
'description' => 'Description 1'
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
},
|
||||
'my_meta' => {
|
||||
'total' => 10
|
||||
|
||||
@ -16,14 +16,14 @@ module ActiveModel
|
||||
|
||||
def test_root_is_not_displayed_using_serializable_hash
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}, @serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'initialize' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -33,7 +33,7 @@ module ActiveModel
|
||||
|
||||
assert_equal({
|
||||
'profile' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -41,7 +41,7 @@ module ActiveModel
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json(root: 'argument'))
|
||||
end
|
||||
@ -51,7 +51,7 @@ module ActiveModel
|
||||
@serializer = ProfileSerializer.new(@profile, root: false)
|
||||
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
end
|
||||
@ -71,14 +71,14 @@ module ActiveModel
|
||||
|
||||
def test_root_is_not_displayed_using_serializable_hash
|
||||
assert_equal({
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}, @serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'in_serializer' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -86,7 +86,7 @@ module ActiveModel
|
||||
def test_root_in_initializer_takes_precedence
|
||||
assert_equal({
|
||||
'initialize' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @rooted_serializer.as_json)
|
||||
end
|
||||
@ -94,7 +94,7 @@ module ActiveModel
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @rooted_serializer.as_json(root: :argument))
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user