mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
upgrade hash syntax in tests
This commit is contained in:
parent
74ba9dc76c
commit
c3fa96456c
@ -6,63 +6,63 @@ class ArraySerializerTest < ActiveModel::TestCase
|
||||
def test_array_serializer
|
||||
model = Model.new
|
||||
user = User.new
|
||||
comments = Comment.new(:title => "Comment1", :id => 1)
|
||||
comments = Comment.new(title: "Comment1", id: 1)
|
||||
|
||||
array = [model, user, comments]
|
||||
serializer = array.active_model_serializer.new(array, :scope => {:scope => true})
|
||||
serializer = array.active_model_serializer.new(array, scope: { scope: true })
|
||||
assert_equal([
|
||||
{ :model => "Model" },
|
||||
{ :last_name => "Valim", :ok => true, :first_name => "Jose", :scope => true },
|
||||
{ :title => "Comment1" }
|
||||
{ model: "Model" },
|
||||
{ last_name: "Valim", ok: true, first_name: "Jose", scope: true },
|
||||
{ title: "Comment1" }
|
||||
], serializer.as_json)
|
||||
end
|
||||
|
||||
def test_array_serializer_with_root
|
||||
comment1 = Comment.new(:title => "Comment1", :id => 1)
|
||||
comment2 = Comment.new(:title => "Comment2", :id => 2)
|
||||
comment1 = Comment.new(title: "Comment1", id: 1)
|
||||
comment2 = Comment.new(title: "Comment2", id: 2)
|
||||
|
||||
array = [ comment1, comment2 ]
|
||||
|
||||
serializer = array.active_model_serializer.new(array, :root => :comments)
|
||||
serializer = array.active_model_serializer.new(array, root: :comments)
|
||||
|
||||
assert_equal({ :comments => [
|
||||
{ :title => "Comment1" },
|
||||
{ :title => "Comment2" }
|
||||
assert_equal({ comments: [
|
||||
{ title: "Comment1" },
|
||||
{ title: "Comment2" }
|
||||
]}, serializer.as_json)
|
||||
end
|
||||
|
||||
def test_active_model_with_root
|
||||
comment1 = ModelWithActiveModelSerializer.new(:title => "Comment1")
|
||||
comment2 = ModelWithActiveModelSerializer.new(:title => "Comment2")
|
||||
comment1 = ModelWithActiveModelSerializer.new(title: "Comment1")
|
||||
comment2 = ModelWithActiveModelSerializer.new(title: "Comment2")
|
||||
|
||||
array = [ comment1, comment2 ]
|
||||
|
||||
serializer = array.active_model_serializer.new(array, :root => :comments)
|
||||
serializer = array.active_model_serializer.new(array, root: :comments)
|
||||
|
||||
assert_equal({ :comments => [
|
||||
{ :title => "Comment1" },
|
||||
{ :title => "Comment2" }
|
||||
assert_equal({ comments: [
|
||||
{ title: "Comment1" },
|
||||
{ title: "Comment2" }
|
||||
]}, serializer.as_json)
|
||||
end
|
||||
|
||||
def test_array_serializer_with_hash
|
||||
hash = {:value => "something"}
|
||||
hash = { value: "something" }
|
||||
array = [hash]
|
||||
serializer = array.active_model_serializer.new(array, :root => :items)
|
||||
assert_equal({ :items => [ hash.as_json ]}, serializer.as_json)
|
||||
serializer = array.active_model_serializer.new(array, root: :items)
|
||||
assert_equal({ items: [hash.as_json] }, serializer.as_json)
|
||||
end
|
||||
|
||||
def test_array_serializer_with_specified_serializer
|
||||
post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
|
||||
post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
|
||||
post1 = Post.new(title: "Post1", author: "Author1", id: 1)
|
||||
post2 = Post.new(title: "Post2", author: "Author2", id: 2)
|
||||
|
||||
array = [ post1, post2 ]
|
||||
|
||||
serializer = array.active_model_serializer.new array, :each_serializer => CustomPostSerializer
|
||||
serializer = array.active_model_serializer.new array, each_serializer: CustomPostSerializer
|
||||
|
||||
assert_equal([
|
||||
{ :title => "Post1" },
|
||||
{ :title => "Post2" }
|
||||
{ title: "Post1" },
|
||||
{ title: "Post2" }
|
||||
], serializer.as_json)
|
||||
end
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ class AssociationTest < ActiveModel::TestCase
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :model => "Model" }
|
||||
{ model: "Model" }
|
||||
end
|
||||
|
||||
def method_missing(meth, *args)
|
||||
@ -33,8 +33,8 @@ class AssociationTest < ActiveModel::TestCase
|
||||
@hash = {}
|
||||
@root_hash = {}
|
||||
|
||||
@post = Model.new(:title => "New Post", :body => "Body")
|
||||
@comment = Model.new(:id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT")
|
||||
@post = Model.new(title: "New Post", body: "Body")
|
||||
@comment = Model.new(id: 1, external_id: "COMM001", body: "ZOMG A COMMENT")
|
||||
@post.comments = [ @comment ]
|
||||
@post.comment = @comment
|
||||
|
||||
@ -46,66 +46,66 @@ class AssociationTest < ActiveModel::TestCase
|
||||
attributes :title, :body
|
||||
end
|
||||
|
||||
@post_serializer = @post_serializer_class.new(@post, :hash => @root_hash)
|
||||
@post_serializer = @post_serializer_class.new(@post, hash: @root_hash)
|
||||
end
|
||||
|
||||
def include!(key, options={})
|
||||
@post_serializer.include! key, {
|
||||
:embed => :ids,
|
||||
:include => true,
|
||||
:node => @hash,
|
||||
:serializer => @comment_serializer_class
|
||||
embed: :ids,
|
||||
include: true,
|
||||
node: @hash,
|
||||
serializer: @comment_serializer_class
|
||||
}.merge(options)
|
||||
end
|
||||
|
||||
def include_bare!(key, options={})
|
||||
@post_serializer.include! key, {
|
||||
:node => @hash,
|
||||
:serializer => @comment_serializer_class
|
||||
node: @hash,
|
||||
serializer: @comment_serializer_class
|
||||
}.merge(options)
|
||||
end
|
||||
|
||||
class NoDefaults < AssociationTest
|
||||
def test_include_bang_has_many_associations
|
||||
include! :comments, :value => @post.comments
|
||||
include! :comments, value: @post.comments
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [ 1 ]
|
||||
comment_ids: [ 1 ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_include_bang_with_embed_false
|
||||
include! :comments, :value => @post.comments, :embed => false
|
||||
include! :comments, value: @post.comments, embed: false
|
||||
|
||||
assert_equal({}, @hash)
|
||||
assert_equal({}, @root_hash)
|
||||
end
|
||||
|
||||
def test_include_bang_with_embed_ids_include_false
|
||||
include! :comments, :value => @post.comments, :embed => :ids, :include => false
|
||||
include! :comments, value: @post.comments, embed: :ids, include: false
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [ 1 ]
|
||||
comment_ids: [ 1 ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({}, @root_hash)
|
||||
end
|
||||
|
||||
def test_include_bang_has_one_associations
|
||||
include! :comment, :value => @post.comment
|
||||
include! :comment, value: @post.comment
|
||||
|
||||
assert_equal({
|
||||
:comment_id => 1
|
||||
comment_id: 1
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }]
|
||||
comments: [{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }]
|
||||
}, @root_hash)
|
||||
end
|
||||
end
|
||||
@ -119,12 +119,12 @@ class AssociationTest < ActiveModel::TestCase
|
||||
include! :comments
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [ 1 ]
|
||||
comment_ids: [ 1 ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
@ -137,134 +137,134 @@ class AssociationTest < ActiveModel::TestCase
|
||||
include! :comment
|
||||
|
||||
assert_equal({
|
||||
:comment_id => 1
|
||||
comment_id: 1
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_with_default_has_many_with_custom_key
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :key => :custom_comments
|
||||
has_many :comments, key: :custom_comments
|
||||
end
|
||||
|
||||
include! :comments
|
||||
|
||||
assert_equal({
|
||||
:custom_comments => [ 1 ]
|
||||
custom_comments: [ 1 ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_with_default_has_one_with_custom_key
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :key => :custom_comment_id
|
||||
has_one :comment, key: :custom_comment_id
|
||||
end
|
||||
|
||||
include! :comment
|
||||
|
||||
assert_equal({
|
||||
:custom_comment_id => 1
|
||||
custom_comment_id: 1
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_with_default_has_many_with_custom_embed_key
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed_key => :external_id
|
||||
has_many :comments, embed_key: :external_id
|
||||
end
|
||||
|
||||
include! :comments
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [ "COMM001" ]
|
||||
comment_ids: [ "COMM001" ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_with_default_has_one_with_custom_embed_key
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :embed_key => :external_id
|
||||
has_one :comment, embed_key: :external_id
|
||||
end
|
||||
|
||||
include! :comment
|
||||
|
||||
assert_equal({
|
||||
:comment_id => "COMM001"
|
||||
comment_id: "COMM001"
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_with_default_has_many_with_custom_key_and_custom_embed_key
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :key => :custom_comments, :embed_key => :external_id
|
||||
has_many :comments, key: :custom_comments, embed_key: :external_id
|
||||
end
|
||||
|
||||
include! :comments
|
||||
|
||||
assert_equal({
|
||||
:custom_comments => [ "COMM001" ]
|
||||
custom_comments: [ "COMM001" ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_with_default_has_one_with_custom_key_and_custom_embed_key
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :key => :custom_comment, :embed_key => :external_id
|
||||
has_one :comment, key: :custom_comment, embed_key: :external_id
|
||||
end
|
||||
|
||||
include! :comment
|
||||
|
||||
assert_equal({
|
||||
:custom_comment => "COMM001"
|
||||
custom_comment: "COMM001"
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_embed_objects_for_has_many_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed => :objects
|
||||
has_many :comments, embed: :objects
|
||||
end
|
||||
|
||||
include_bare! :comments
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @hash)
|
||||
|
||||
@ -273,13 +273,13 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_embed_ids_for_has_many_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed => :ids
|
||||
has_many :comments, embed: :ids
|
||||
end
|
||||
|
||||
include_bare! :comments
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [ 1 ]
|
||||
comment_ids: [ 1 ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({}, @root_hash)
|
||||
@ -287,7 +287,7 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_embed_false_for_has_many_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed => false
|
||||
has_many :comments, embed: false
|
||||
end
|
||||
|
||||
include_bare! :comments
|
||||
@ -298,31 +298,31 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_embed_ids_include_true_for_has_many_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed => :ids, :include => true
|
||||
has_many :comments, embed: :ids, include: true
|
||||
end
|
||||
|
||||
include_bare! :comments
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [ 1 ]
|
||||
comment_ids: [ 1 ]
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
|
||||
def test_embed_ids_for_has_one_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :embed => :ids
|
||||
has_one :comment, embed: :ids
|
||||
end
|
||||
|
||||
include_bare! :comment
|
||||
|
||||
assert_equal({
|
||||
:comment_id => 1
|
||||
comment_id: 1
|
||||
}, @hash)
|
||||
|
||||
assert_equal({}, @root_hash)
|
||||
@ -330,7 +330,7 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_embed_false_for_has_one_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :embed => false
|
||||
has_one :comment, embed: false
|
||||
end
|
||||
|
||||
include_bare! :comment
|
||||
@ -341,18 +341,18 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_embed_ids_include_true_for_has_one_associations
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :embed => :ids, :include => true
|
||||
has_one :comment, embed: :ids, include: true
|
||||
end
|
||||
|
||||
include_bare! :comment
|
||||
|
||||
assert_equal({
|
||||
:comment_id => 1
|
||||
comment_id: 1
|
||||
}, @hash)
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @root_hash)
|
||||
end
|
||||
@ -361,8 +361,8 @@ class AssociationTest < ActiveModel::TestCase
|
||||
@post.recent_comment = @comment
|
||||
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :embed => :ids, :include => true
|
||||
has_one :recent_comment, :embed => :ids, :include => true, :root => :comments
|
||||
has_one :comment, embed: :ids, include: true
|
||||
has_one :recent_comment, embed: :ids, include: true, root: :comments
|
||||
end
|
||||
|
||||
# Count how often the @comment record is serialized.
|
||||
@ -382,7 +382,7 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_include_with_read_association_id_for_serialization_hook
|
||||
@post_serializer_class.class_eval do
|
||||
has_one :comment, :embed => :ids, :include => true
|
||||
has_one :comment, embed: :ids, include: true
|
||||
end
|
||||
|
||||
association_name = nil
|
||||
@ -399,13 +399,13 @@ class AssociationTest < ActiveModel::TestCase
|
||||
include_bare! :comment
|
||||
|
||||
assert_equal({
|
||||
:comment_id => 1
|
||||
comment_id: 1
|
||||
}, @hash)
|
||||
end
|
||||
|
||||
def test_include_with_read_association_ids_for_serialization_hook
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed => :ids, :include => false
|
||||
has_many :comments, embed: :ids, include: false
|
||||
end
|
||||
|
||||
association_name = nil
|
||||
@ -422,7 +422,7 @@ class AssociationTest < ActiveModel::TestCase
|
||||
include_bare! :comments
|
||||
|
||||
assert_equal({
|
||||
:comment_ids => [1]
|
||||
comment_ids: [1]
|
||||
}, @hash)
|
||||
end
|
||||
end
|
||||
@ -433,13 +433,13 @@ class AssociationTest < ActiveModel::TestCase
|
||||
class FooSerializer < ActiveModel::Serializer
|
||||
root :foos
|
||||
attributes :id
|
||||
has_many :bars, :serializer => BarSerializer, :root => :bars, :embed => :ids, :include => true
|
||||
has_many :bars, serializer: BarSerializer, root: :bars, embed: :ids, include: true
|
||||
end
|
||||
|
||||
class BarSerializer < ActiveModel::Serializer
|
||||
root :bars
|
||||
attributes :id
|
||||
has_many :foos, :serializer => FooSerializer, :root => :foos, :embed => :ids, :include => true
|
||||
has_many :foos, serializer: FooSerializer, root: :foos, embed: :ids, include: true
|
||||
end
|
||||
|
||||
class Foo < Model
|
||||
@ -453,26 +453,26 @@ class AssociationTest < ActiveModel::TestCase
|
||||
def setup
|
||||
super
|
||||
|
||||
foo = Foo.new(:id => 1)
|
||||
bar = Bar.new(:id => 2)
|
||||
foo = Foo.new(id: 1)
|
||||
bar = Bar.new(id: 2)
|
||||
|
||||
foo.bars = [ bar ]
|
||||
bar.foos = [ foo ]
|
||||
|
||||
collection = [ foo ]
|
||||
|
||||
@serializer = collection.active_model_serializer.new(collection, :root => :foos)
|
||||
@serializer = collection.active_model_serializer.new(collection, root: :foos)
|
||||
end
|
||||
|
||||
def test_mutual_relation_result
|
||||
assert_equal({
|
||||
:foos => [{
|
||||
:bar_ids => [ 2 ],
|
||||
:id => 1
|
||||
foos: [{
|
||||
bar_ids: [ 2 ],
|
||||
id: 1
|
||||
}],
|
||||
:bars => [{
|
||||
:foo_ids => [ 1 ],
|
||||
:id => 2
|
||||
bars: [{
|
||||
foo_ids: [ 1 ],
|
||||
id: 2
|
||||
}]
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -492,77 +492,77 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
@post_serializer_class.class_eval do
|
||||
root :post
|
||||
embed :ids, :include => true
|
||||
has_many :comments, :serializer => comment_serializer_class
|
||||
embed :ids, include: true
|
||||
has_many :comments, serializer: comment_serializer_class
|
||||
end
|
||||
end
|
||||
|
||||
def test_when_it_is_included
|
||||
post_serializer = @post_serializer_class.new(
|
||||
@post, :include => [:comments]
|
||||
@post, include: [:comments]
|
||||
)
|
||||
|
||||
json = post_serializer.as_json
|
||||
|
||||
assert_equal({
|
||||
:post => {
|
||||
:title => "New Post",
|
||||
:body => "Body",
|
||||
:comment_ids => [ 1 ]
|
||||
post: {
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
comment_ids: [ 1 ]
|
||||
},
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, json)
|
||||
end
|
||||
|
||||
def test_when_it_is_not_included
|
||||
post_serializer = @post_serializer_class.new(
|
||||
@post, :include => []
|
||||
@post, include: []
|
||||
)
|
||||
|
||||
json = post_serializer.as_json
|
||||
|
||||
assert_equal({
|
||||
:post => {
|
||||
:title => "New Post",
|
||||
:body => "Body",
|
||||
:comment_ids => [ 1 ]
|
||||
post: {
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
comment_ids: [ 1 ]
|
||||
}
|
||||
}, json)
|
||||
end
|
||||
|
||||
def test_when_it_is_excluded
|
||||
post_serializer = @post_serializer_class.new(
|
||||
@post, :exclude => [:comments]
|
||||
@post, exclude: [:comments]
|
||||
)
|
||||
|
||||
json = post_serializer.as_json
|
||||
|
||||
assert_equal({
|
||||
:post => {
|
||||
:title => "New Post",
|
||||
:body => "Body",
|
||||
:comment_ids => [ 1 ]
|
||||
post: {
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
comment_ids: [ 1 ]
|
||||
}
|
||||
}, json)
|
||||
end
|
||||
|
||||
def test_when_it_is_not_excluded
|
||||
post_serializer = @post_serializer_class.new(
|
||||
@post, :exclude => []
|
||||
@post, exclude: []
|
||||
)
|
||||
|
||||
json = post_serializer.as_json
|
||||
|
||||
assert_equal({
|
||||
:post => {
|
||||
:title => "New Post",
|
||||
:body => "Body",
|
||||
:comment_ids => [ 1 ]
|
||||
post: {
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
comment_ids: [ 1 ]
|
||||
},
|
||||
:comments => [
|
||||
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, json)
|
||||
end
|
||||
@ -575,14 +575,14 @@ class AssociationTest < ActiveModel::TestCase
|
||||
|
||||
def test_specifying_serializer_class_as_string
|
||||
@post_serializer_class.class_eval do
|
||||
has_many :comments, :embed => :objects
|
||||
has_many :comments, embed: :objects
|
||||
end
|
||||
|
||||
include_bare! :comments, :serializer => "AssociationTest::StringSerializerOption::StringSerializer"
|
||||
include_bare! :comments, serializer: "AssociationTest::StringSerializerOption::StringSerializer"
|
||||
|
||||
assert_equal({
|
||||
:comments => [
|
||||
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||
comments: [
|
||||
{ id: 1, body: "ZOMG A COMMENT" }
|
||||
]
|
||||
}, @hash)
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ class NoSerializationScopeTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :scope => @options[:scope].as_json }
|
||||
{ scope: @options[:scope].as_json }
|
||||
end
|
||||
end
|
||||
|
||||
@ -21,14 +21,14 @@ class NoSerializationScopeTest < ActionController::TestCase
|
||||
serialization_scope nil
|
||||
|
||||
def index
|
||||
render :json => ScopeSerializable.new
|
||||
render json: ScopeSerializable.new
|
||||
end
|
||||
end
|
||||
|
||||
tests NoSerializationScopeController
|
||||
|
||||
def test_disabled_serialization_scope
|
||||
get :index, :format => :json
|
||||
get :index, format: :json
|
||||
assert_equal '{"scope":null}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
@ -21,7 +21,7 @@ class DefaultScopeNameTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def render_new_user
|
||||
render :json => TestUser.new('pete', false), :serializer => UserSerializer
|
||||
render json: TestUser.new('pete', false), serializer: UserSerializer
|
||||
end
|
||||
end
|
||||
|
||||
@ -54,7 +54,7 @@ class SerializationScopeNameTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def render_new_user
|
||||
render :json => TestUser.new('pete', false), :serializer => AdminUserSerializer
|
||||
render json: TestUser.new('pete', false), serializer: AdminUserSerializer
|
||||
end
|
||||
end
|
||||
|
||||
@ -85,7 +85,7 @@ class SerializationActionScopeOverrideTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def render_new_user
|
||||
render :json => TestUser.new('pete', false), :serializer => AdminUserSerializer, :scope => current_admin, :scope_name => :current_admin
|
||||
render json: TestUser.new('pete', false), serializer: AdminUserSerializer, scope: current_admin, scope_name: :current_admin
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -4,13 +4,13 @@ require 'pathname'
|
||||
class RenderJsonTest < ActionController::TestCase
|
||||
class JsonRenderable
|
||||
def as_json(options={})
|
||||
hash = { :a => :b, :c => :d, :e => :f }
|
||||
hash = { a: :b, c: :d, e: :f }
|
||||
hash.except!(*options[:except]) if options[:except]
|
||||
hash
|
||||
end
|
||||
|
||||
def to_json(options = {})
|
||||
super :except => [:c, :e]
|
||||
super except: [:c, :e]
|
||||
end
|
||||
end
|
||||
|
||||
@ -20,9 +20,9 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
hash = { :object => serializable_hash, :scope => @options[:scope].as_json }
|
||||
hash.merge!(:options => true) if @options[:options]
|
||||
hash.merge!(:check_defaults => true) if @options[:check_defaults]
|
||||
hash = { object: serializable_hash, scope: @options[:scope].as_json }
|
||||
hash.merge!(options: true) if @options[:options]
|
||||
hash.merge!(check_defaults: true) if @options[:check_defaults]
|
||||
hash
|
||||
end
|
||||
|
||||
@ -41,7 +41,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :serializable_object => true }
|
||||
{ serializable_object: true }
|
||||
end
|
||||
end
|
||||
|
||||
@ -50,7 +50,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :hello => true }
|
||||
{ hello: true }
|
||||
end
|
||||
end
|
||||
|
||||
@ -59,7 +59,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :rails => 'rocks' }
|
||||
{ rails: 'rocks' }
|
||||
end
|
||||
end
|
||||
|
||||
@ -75,7 +75,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
|
||||
class HypermediaSerializer < ActiveModel::Serializer
|
||||
def as_json(*)
|
||||
{ :link => hypermedia_url }
|
||||
{ link: hypermedia_url }
|
||||
end
|
||||
end
|
||||
|
||||
@ -94,111 +94,111 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def render_json_nil
|
||||
render :json => nil
|
||||
render json: nil
|
||||
end
|
||||
|
||||
def render_json_render_to_string
|
||||
render :text => render_to_string(:json => '[]')
|
||||
render text: render_to_string(json: '[]')
|
||||
end
|
||||
|
||||
def render_json_hello_world
|
||||
render :json => ActiveSupport::JSON.encode(:hello => 'world')
|
||||
render json: ActiveSupport::JSON.encode(hello: 'world')
|
||||
end
|
||||
|
||||
def render_json_hello_world_with_status
|
||||
render :json => ActiveSupport::JSON.encode(:hello => 'world'), :status => 401
|
||||
render json: ActiveSupport::JSON.encode(hello: 'world'), status: 401
|
||||
end
|
||||
|
||||
def render_json_hello_world_with_callback
|
||||
render :json => ActiveSupport::JSON.encode(:hello => 'world'), :callback => 'alert'
|
||||
render json: ActiveSupport::JSON.encode(hello: 'world'), callback: 'alert'
|
||||
end
|
||||
|
||||
def render_json_with_custom_content_type
|
||||
render :json => ActiveSupport::JSON.encode(:hello => 'world'), :content_type => 'text/javascript'
|
||||
render json: ActiveSupport::JSON.encode(hello: 'world'), content_type: 'text/javascript'
|
||||
end
|
||||
|
||||
def render_symbol_json
|
||||
render :json => ActiveSupport::JSON.encode(:hello => 'world')
|
||||
render json: ActiveSupport::JSON.encode(hello: 'world')
|
||||
end
|
||||
|
||||
def render_json_nil_with_custom_serializer
|
||||
render :json => nil, :serializer => DummyCustomSerializer
|
||||
render json: nil, serializer: DummyCustomSerializer
|
||||
end
|
||||
|
||||
|
||||
def render_json_with_extra_options
|
||||
render :json => JsonRenderable.new, :except => [:c, :e]
|
||||
render json: JsonRenderable.new, except: [:c, :e]
|
||||
end
|
||||
|
||||
def render_json_without_options
|
||||
render :json => JsonRenderable.new
|
||||
render json: JsonRenderable.new
|
||||
end
|
||||
|
||||
def render_json_with_serializer
|
||||
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||
render :json => JsonSerializable.new
|
||||
@current_user = Struct.new(:as_json).new(current_user: true)
|
||||
render json: JsonSerializable.new
|
||||
end
|
||||
|
||||
def render_json_with_serializer_and_implicit_root
|
||||
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||
render :json => [JsonSerializable.new]
|
||||
@current_user = Struct.new(:as_json).new(current_user: true)
|
||||
render json: [JsonSerializable.new]
|
||||
end
|
||||
|
||||
def render_json_with_serializer_and_options
|
||||
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||
render :json => JsonSerializable.new, :options => true
|
||||
@current_user = Struct.new(:as_json).new(current_user: true)
|
||||
render json: JsonSerializable.new, options: true
|
||||
end
|
||||
|
||||
def render_json_with_serializer_and_scope_option
|
||||
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||
scope = Struct.new(:as_json).new(:current_user => false)
|
||||
render :json => JsonSerializable.new, :scope => scope
|
||||
@current_user = Struct.new(:as_json).new(current_user: true)
|
||||
scope = Struct.new(:as_json).new(current_user: false)
|
||||
render json: JsonSerializable.new, scope: scope
|
||||
end
|
||||
|
||||
def render_json_with_serializer_api_but_without_serializer
|
||||
@current_user = Struct.new(:as_json).new(:current_user => true)
|
||||
render :json => JsonSerializable.new(true)
|
||||
@current_user = Struct.new(:as_json).new(current_user: true)
|
||||
render json: JsonSerializable.new(true)
|
||||
end
|
||||
|
||||
# To specify a custom serializer for an object, use :serializer.
|
||||
def render_json_with_custom_serializer
|
||||
render :json => Object.new, :serializer => CustomSerializer
|
||||
render json: Object.new, serializer: CustomSerializer
|
||||
end
|
||||
|
||||
# To specify a custom serializer for each item in the Array, use :each_serializer.
|
||||
def render_json_array_with_custom_serializer
|
||||
render :json => [Object.new], :each_serializer => CustomSerializer
|
||||
render json: [Object.new], each_serializer: CustomSerializer
|
||||
end
|
||||
|
||||
def render_json_array_with_wrong_option
|
||||
render :json => [Object.new], :serializer => CustomSerializer
|
||||
render json: [Object.new], serializer: CustomSerializer
|
||||
end
|
||||
|
||||
def render_json_with_links
|
||||
render :json => HypermediaSerializable.new
|
||||
render json: HypermediaSerializable.new
|
||||
end
|
||||
|
||||
def render_json_array_with_no_root
|
||||
render :json => [], :root => false
|
||||
render json: [], root: false
|
||||
end
|
||||
|
||||
def render_json_empty_array
|
||||
render :json => []
|
||||
render json: []
|
||||
end
|
||||
|
||||
def render_json_array_with_custom_array_serializer
|
||||
render :json => [], :serializer => CustomArraySerializer
|
||||
render json: [], serializer: CustomArraySerializer
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def default_serializer_options
|
||||
defaults = {}
|
||||
defaults.merge!(:check_defaults => true) if params[:check_defaults]
|
||||
defaults.merge!(:root => :awesome) if params[:check_default_root]
|
||||
defaults.merge!(:scope => :current_admin) if params[:check_default_scope]
|
||||
defaults.merge!(:serializer => AnotherCustomSerializer) if params[:check_default_serializer]
|
||||
defaults.merge!(:each_serializer => AnotherCustomSerializer) if params[:check_default_each_serializer]
|
||||
defaults.merge!(check_defaults: true) if params[:check_defaults]
|
||||
defaults.merge!(root: :awesome) if params[:check_default_root]
|
||||
defaults.merge!(scope: :current_admin) if params[:check_default_scope]
|
||||
defaults.merge!(serializer: AnotherCustomSerializer) if params[:check_default_serializer]
|
||||
defaults.merge!(each_serializer: AnotherCustomSerializer) if params[:check_default_each_serializer]
|
||||
defaults
|
||||
end
|
||||
end
|
||||
@ -279,19 +279,19 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_with_serializer_checking_defaults
|
||||
get :render_json_with_serializer, :check_defaults => true
|
||||
get :render_json_with_serializer, check_defaults: true
|
||||
assert_match '"scope":{"current_user":true}', @response.body
|
||||
assert_match '"object":{"serializable_object":true}', @response.body
|
||||
assert_match '"check_defaults":true', @response.body
|
||||
end
|
||||
|
||||
def test_render_json_with_serializer_checking_default_serailizer
|
||||
get :render_json_with_serializer, :check_default_serializer => true
|
||||
get :render_json_with_serializer, check_default_serializer: true
|
||||
assert_match '{"rails":"rocks"}', @response.body
|
||||
end
|
||||
|
||||
def test_render_json_with_serializer_checking_default_scope
|
||||
get :render_json_with_serializer, :check_default_scope => true
|
||||
get :render_json_with_serializer, check_default_scope: true
|
||||
assert_match '"scope":"current_admin"', @response.body
|
||||
end
|
||||
|
||||
@ -301,7 +301,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_with_serializer_and_implicit_root_checking_default_each_serailizer
|
||||
get :render_json_with_serializer_and_implicit_root, :check_default_each_serializer => true
|
||||
get :render_json_with_serializer_and_implicit_root, check_default_each_serializer: true
|
||||
assert_match '"test":[{"rails":"rocks"}]', @response.body
|
||||
end
|
||||
|
||||
@ -318,7 +318,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_with_serializer_and_scope_option_checking_default_scope
|
||||
get :render_json_with_serializer_and_scope_option, :check_default_scope => true
|
||||
get :render_json_with_serializer_and_scope_option, check_default_scope: true
|
||||
assert_match '"scope":{"current_user":false}', @response.body
|
||||
end
|
||||
|
||||
@ -333,7 +333,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_with_custom_serializer_checking_default_serailizer
|
||||
get :render_json_with_custom_serializer, :check_default_serializer => true
|
||||
get :render_json_with_custom_serializer, check_default_serializer: true
|
||||
assert_match '{"hello":true}', @response.body
|
||||
end
|
||||
|
||||
@ -349,7 +349,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_array_with_custom_serializer_checking_default_each_serailizer
|
||||
get :render_json_array_with_custom_serializer, :check_default_each_serializer => true
|
||||
get :render_json_array_with_custom_serializer, check_default_each_serializer: true
|
||||
assert_match '{"test":[{"hello":true}]}', @response.body
|
||||
end
|
||||
|
||||
@ -364,7 +364,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_array_with_no_root_checking_default_root
|
||||
get :render_json_array_with_no_root, :check_default_root => true
|
||||
get :render_json_array_with_no_root, check_default_root: true
|
||||
assert_equal '[]', @response.body
|
||||
end
|
||||
|
||||
@ -374,7 +374,7 @@ class RenderJsonTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
def test_render_json_empty_array_checking_default_root
|
||||
get :render_json_empty_array, :check_default_root => true
|
||||
get :render_json_empty_array, check_default_root: true
|
||||
assert_equal '{"awesome":[]}', @response.body
|
||||
end
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -8,7 +8,7 @@ class Model
|
||||
end
|
||||
|
||||
def as_json(*)
|
||||
{ :model => "Model" }
|
||||
{ model: "Model" }
|
||||
end
|
||||
end
|
||||
|
||||
@ -26,7 +26,7 @@ class User
|
||||
attr_accessor :superuser
|
||||
|
||||
def initialize(hash={})
|
||||
@attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password")
|
||||
@attributes = hash.merge(first_name: "Jose", last_name: "Valim", password: "oh noes yugive my password")
|
||||
end
|
||||
|
||||
def read_attribute_for_serialization(name)
|
||||
@ -58,31 +58,31 @@ class UserSerializer < ActiveModel::Serializer
|
||||
attributes :first_name, :last_name
|
||||
|
||||
def serializable_hash
|
||||
attributes.merge(:ok => true).merge(options[:scope])
|
||||
attributes.merge(ok: true).merge(options[:scope])
|
||||
end
|
||||
end
|
||||
|
||||
class UserAttributesWithKeySerializer < ActiveModel::Serializer
|
||||
attributes :first_name => :f_name, :last_name => :l_name
|
||||
attributes first_name: :f_name, last_name: :l_name
|
||||
|
||||
def serializable_hash
|
||||
attributes.merge(:ok => true).merge(options[:scope])
|
||||
attributes.merge(ok: true).merge(options[:scope])
|
||||
end
|
||||
end
|
||||
|
||||
class UserAttributesWithSomeKeySerializer < ActiveModel::Serializer
|
||||
attributes :first_name, :last_name => :l_name
|
||||
attributes :first_name, last_name: :l_name
|
||||
|
||||
def serializable_hash
|
||||
attributes.merge(:ok => true).merge(options[:scope])
|
||||
attributes.merge(ok: true).merge(options[:scope])
|
||||
end
|
||||
end
|
||||
|
||||
class UserAttributesWithUnsymbolizableKeySerializer < ActiveModel::Serializer
|
||||
attributes :first_name, :last_name => :"last-name"
|
||||
attributes :first_name, last_name: :"last-name"
|
||||
|
||||
def serializable_hash
|
||||
attributes.merge(:ok => true).merge(options[:scope])
|
||||
attributes.merge(ok: true).merge(options[:scope])
|
||||
end
|
||||
end
|
||||
|
||||
@ -95,7 +95,7 @@ class MyUserSerializer < ActiveModel::Serializer
|
||||
|
||||
def serializable_hash
|
||||
hash = attributes
|
||||
hash = hash.merge(:super_user => true) if object.super_user?
|
||||
hash = hash.merge(super_user: true) if object.super_user?
|
||||
hash
|
||||
end
|
||||
end
|
||||
@ -108,7 +108,7 @@ class CommentSerializer
|
||||
attr_reader :object
|
||||
|
||||
def serializable_hash
|
||||
{ :title => @object.read_attribute_for_serialization(:title) }
|
||||
{ title: @object.read_attribute_for_serialization(:title) }
|
||||
end
|
||||
|
||||
def as_json(options=nil)
|
||||
@ -116,20 +116,20 @@ class CommentSerializer
|
||||
if options[:root] == false
|
||||
serializable_hash
|
||||
else
|
||||
{ :comment => serializable_hash }
|
||||
{ comment: serializable_hash }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
attributes :title, :body
|
||||
has_many :comments, :serializer => CommentSerializer
|
||||
has_many :comments, serializer: CommentSerializer
|
||||
end
|
||||
|
||||
class PostWithConditionalCommentsSerializer < ActiveModel::Serializer
|
||||
root :post
|
||||
attributes :title, :body
|
||||
has_many :comments, :serializer => CommentSerializer
|
||||
has_many :comments, serializer: CommentSerializer
|
||||
|
||||
def include_associations!
|
||||
include! :comments unless object.comments_disabled
|
||||
@ -139,7 +139,7 @@ end
|
||||
class PostWithMultipleConditionalsSerializer < ActiveModel::Serializer
|
||||
root :post
|
||||
attributes :title, :body, :author
|
||||
has_many :comments, :serializer => CommentSerializer
|
||||
has_many :comments, serializer: CommentSerializer
|
||||
|
||||
def include_comments?
|
||||
!object.comments_disabled
|
||||
@ -159,7 +159,7 @@ class AuthorSerializer < ActiveModel::Serializer
|
||||
end
|
||||
|
||||
class BlogSerializer < ActiveModel::Serializer
|
||||
has_one :author, :serializer => AuthorSerializer
|
||||
has_one :author, serializer: AuthorSerializer
|
||||
end
|
||||
|
||||
class BlogWithRootSerializer < BlogSerializer
|
||||
@ -175,8 +175,8 @@ class CustomBlog < Blog
|
||||
end
|
||||
|
||||
class CustomBlogSerializer < ActiveModel::Serializer
|
||||
has_many :public_posts, :key => :posts, :serializer => PostSerializer
|
||||
has_one :public_user, :key => :user, :serializer => UserSerializer
|
||||
has_many :public_posts, key: :posts, serializer: PostSerializer
|
||||
has_one :public_user, key: :user, serializer: UserSerializer
|
||||
end
|
||||
|
||||
class SomeSerializer < ActiveModel::Serializer
|
||||
|
||||
Loading…
Reference in New Issue
Block a user