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