mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Do not convert root and meta_key to Strings
This commit is contained in:
parent
75e9a2599d
commit
daa9304398
@ -2,7 +2,7 @@ module ActiveModel
|
||||
module Serializable
|
||||
def as_json(options={})
|
||||
if root = options[:root] || self.root
|
||||
hash = { root.to_s => serializable_object }
|
||||
hash = { root => serializable_object }
|
||||
hash.merge!(serializable_data)
|
||||
hash
|
||||
else
|
||||
@ -12,7 +12,7 @@ module ActiveModel
|
||||
|
||||
def serializable_data
|
||||
if respond_to?(:meta) && meta
|
||||
{ meta_key.to_s => meta }
|
||||
{ meta_key => meta }
|
||||
else
|
||||
{}
|
||||
end
|
||||
|
||||
@ -11,7 +11,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta
|
||||
@serializer.meta = { 'total' => 10 }
|
||||
@serializer.meta = { total: 10 }
|
||||
|
||||
assert_equal({
|
||||
'profiles' => [
|
||||
@ -23,15 +23,15 @@ module ActiveModel
|
||||
description: 'Description 2'
|
||||
}
|
||||
],
|
||||
'meta' => {
|
||||
'total' => 10
|
||||
meta: {
|
||||
total: 10
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
|
||||
def test_meta_using_meta_key
|
||||
@serializer.meta_key = :my_meta
|
||||
@serializer.meta = { 'total' => 10 }
|
||||
@serializer.meta = { total: 10 }
|
||||
|
||||
assert_equal({
|
||||
'profiles' => [
|
||||
@ -43,8 +43,8 @@ module ActiveModel
|
||||
description: 'Description 2'
|
||||
}
|
||||
],
|
||||
'my_meta' => {
|
||||
'total' => 10
|
||||
my_meta: {
|
||||
total: 10
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
|
||||
@ -7,7 +7,7 @@ module ActiveModel
|
||||
@old_root = ArraySerializer._root
|
||||
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
||||
@serializer = ArraySerializer.new([@profile1, @profile2], root: 'initialize')
|
||||
@serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
||||
end
|
||||
|
||||
def teardown
|
||||
@ -23,7 +23,7 @@ module ActiveModel
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'initialize' => [
|
||||
initialize: [
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
@ -32,11 +32,11 @@ module ActiveModel
|
||||
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => [
|
||||
argument: [
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
}, @serializer.as_json(root: 'argument'))
|
||||
}, @serializer.as_json(root: :argument))
|
||||
end
|
||||
|
||||
def test_using_false_root_in_initialize_takes_precedence
|
||||
@ -53,7 +53,7 @@ module ActiveModel
|
||||
class RootInSerializerTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@old_root = ArraySerializer._root
|
||||
ArraySerializer._root = 'in_serializer'
|
||||
ArraySerializer._root = :in_serializer
|
||||
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
||||
@serializer = ArraySerializer.new([@profile1, @profile2])
|
||||
@ -73,7 +73,7 @@ module ActiveModel
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'in_serializer' => [
|
||||
in_serializer: [
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
@ -82,7 +82,7 @@ module ActiveModel
|
||||
|
||||
def test_root_in_initializer_takes_precedence
|
||||
assert_equal({
|
||||
'initialize' => [
|
||||
initialize: [
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
@ -91,7 +91,7 @@ module ActiveModel
|
||||
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => [
|
||||
argument: [
|
||||
{ name: 'Name 1', description: 'Description 1' },
|
||||
{ name: 'Name 2', description: 'Description 2' }
|
||||
]
|
||||
|
||||
@ -8,29 +8,29 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_meta
|
||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta: { 'total' => 10 })
|
||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta: { total: 10 })
|
||||
|
||||
assert_equal({
|
||||
'profile' => {
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
},
|
||||
'meta' => {
|
||||
'total' => 10
|
||||
meta: {
|
||||
total: 10
|
||||
}
|
||||
}, profile_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_meta_using_meta_key
|
||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta_key: :my_meta, my_meta: { 'total' => 10 })
|
||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta_key: :my_meta, my_meta: { total: 10 })
|
||||
|
||||
assert_equal({
|
||||
'profile' => {
|
||||
name: 'Name 1',
|
||||
description: 'Description 1'
|
||||
},
|
||||
'my_meta' => {
|
||||
'total' => 10
|
||||
my_meta: {
|
||||
total: 10
|
||||
}
|
||||
}, profile_serializer.as_json)
|
||||
end
|
||||
|
||||
@ -6,7 +6,7 @@ module ActiveModel
|
||||
def setup
|
||||
@old_root = ProfileSerializer._root
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@serializer = ProfileSerializer.new(@profile, root: 'initialize')
|
||||
@serializer = ProfileSerializer.new(@profile, root: :initialize)
|
||||
ProfileSerializer._root = true
|
||||
end
|
||||
|
||||
@ -22,7 +22,7 @@ module ActiveModel
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'initialize' => {
|
||||
initialize: {
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
@ -40,10 +40,10 @@ module ActiveModel
|
||||
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => {
|
||||
argument: {
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json(root: 'argument'))
|
||||
}, @serializer.as_json(root: :argument))
|
||||
end
|
||||
|
||||
def test_using_false_root_in_initializer_takes_precedence
|
||||
@ -59,7 +59,7 @@ module ActiveModel
|
||||
class RootInSerializerTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@old_root = ProfileSerializer._root
|
||||
ProfileSerializer._root = 'in_serializer'
|
||||
ProfileSerializer._root = :in_serializer
|
||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@serializer = ProfileSerializer.new(profile)
|
||||
@rooted_serializer = ProfileSerializer.new(profile, root: :initialize)
|
||||
@ -77,7 +77,7 @@ module ActiveModel
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'in_serializer' => {
|
||||
in_serializer: {
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
@ -85,7 +85,7 @@ module ActiveModel
|
||||
|
||||
def test_root_in_initializer_takes_precedence
|
||||
assert_equal({
|
||||
'initialize' => {
|
||||
initialize: {
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @rooted_serializer.as_json)
|
||||
@ -93,7 +93,7 @@ module ActiveModel
|
||||
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => {
|
||||
argument: {
|
||||
name: 'Name 1', description: 'Description 1'
|
||||
}
|
||||
}, @rooted_serializer.as_json(root: :argument))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user