upgrade hash syntax in tests

This commit is contained in:
Tee Parham 2013-05-29 23:13:37 -06:00
parent 74ba9dc76c
commit c3fa96456c
7 changed files with 602 additions and 602 deletions

View File

@ -6,63 +6,63 @@ class ArraySerializerTest < ActiveModel::TestCase
def test_array_serializer def test_array_serializer
model = Model.new model = Model.new
user = User.new user = User.new
comments = Comment.new(:title => "Comment1", :id => 1) comments = Comment.new(title: "Comment1", id: 1)
array = [model, user, comments] 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([ assert_equal([
{ :model => "Model" }, { model: "Model" },
{ :last_name => "Valim", :ok => true, :first_name => "Jose", :scope => true }, { last_name: "Valim", ok: true, first_name: "Jose", scope: true },
{ :title => "Comment1" } { title: "Comment1" }
], serializer.as_json) ], serializer.as_json)
end end
def test_array_serializer_with_root def test_array_serializer_with_root
comment1 = Comment.new(:title => "Comment1", :id => 1) comment1 = Comment.new(title: "Comment1", id: 1)
comment2 = Comment.new(:title => "Comment2", :id => 2) comment2 = Comment.new(title: "Comment2", id: 2)
array = [ comment1, 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 => [ assert_equal({ comments: [
{ :title => "Comment1" }, { title: "Comment1" },
{ :title => "Comment2" } { title: "Comment2" }
]}, serializer.as_json) ]}, serializer.as_json)
end end
def test_active_model_with_root def test_active_model_with_root
comment1 = ModelWithActiveModelSerializer.new(:title => "Comment1") comment1 = ModelWithActiveModelSerializer.new(title: "Comment1")
comment2 = ModelWithActiveModelSerializer.new(:title => "Comment2") comment2 = ModelWithActiveModelSerializer.new(title: "Comment2")
array = [ comment1, 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 => [ assert_equal({ comments: [
{ :title => "Comment1" }, { title: "Comment1" },
{ :title => "Comment2" } { title: "Comment2" }
]}, serializer.as_json) ]}, serializer.as_json)
end end
def test_array_serializer_with_hash def test_array_serializer_with_hash
hash = {:value => "something"} hash = { value: "something" }
array = [hash] array = [hash]
serializer = array.active_model_serializer.new(array, :root => :items) serializer = array.active_model_serializer.new(array, root: :items)
assert_equal({ :items => [ hash.as_json ]}, serializer.as_json) assert_equal({ items: [hash.as_json] }, serializer.as_json)
end end
def test_array_serializer_with_specified_serializer def test_array_serializer_with_specified_serializer
post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1) post1 = Post.new(title: "Post1", author: "Author1", id: 1)
post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2) post2 = Post.new(title: "Post2", author: "Author2", id: 2)
array = [ post1, post2 ] 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([ assert_equal([
{ :title => "Post1" }, { title: "Post1" },
{ :title => "Post2" } { title: "Post2" }
], serializer.as_json) ], serializer.as_json)
end end

View File

@ -15,7 +15,7 @@ class AssociationTest < ActiveModel::TestCase
end end
def as_json(*) def as_json(*)
{ :model => "Model" } { model: "Model" }
end end
def method_missing(meth, *args) def method_missing(meth, *args)
@ -33,8 +33,8 @@ class AssociationTest < ActiveModel::TestCase
@hash = {} @hash = {}
@root_hash = {} @root_hash = {}
@post = Model.new(:title => "New Post", :body => "Body") @post = Model.new(title: "New Post", body: "Body")
@comment = Model.new(:id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT") @comment = Model.new(id: 1, external_id: "COMM001", body: "ZOMG A COMMENT")
@post.comments = [ @comment ] @post.comments = [ @comment ]
@post.comment = @comment @post.comment = @comment
@ -46,66 +46,66 @@ class AssociationTest < ActiveModel::TestCase
attributes :title, :body attributes :title, :body
end end
@post_serializer = @post_serializer_class.new(@post, :hash => @root_hash) @post_serializer = @post_serializer_class.new(@post, hash: @root_hash)
end end
def include!(key, options={}) def include!(key, options={})
@post_serializer.include! key, { @post_serializer.include! key, {
:embed => :ids, embed: :ids,
:include => true, include: true,
:node => @hash, node: @hash,
:serializer => @comment_serializer_class serializer: @comment_serializer_class
}.merge(options) }.merge(options)
end end
def include_bare!(key, options={}) def include_bare!(key, options={})
@post_serializer.include! key, { @post_serializer.include! key, {
:node => @hash, node: @hash,
:serializer => @comment_serializer_class serializer: @comment_serializer_class
}.merge(options) }.merge(options)
end end
class NoDefaults < AssociationTest class NoDefaults < AssociationTest
def test_include_bang_has_many_associations def test_include_bang_has_many_associations
include! :comments, :value => @post.comments include! :comments, value: @post.comments
assert_equal({ assert_equal({
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_include_bang_with_embed_false 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({}, @hash)
assert_equal({}, @root_hash) assert_equal({}, @root_hash)
end end
def test_include_bang_with_embed_ids_include_false 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({ assert_equal({
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, @hash) }, @hash)
assert_equal({}, @root_hash) assert_equal({}, @root_hash)
end end
def test_include_bang_has_one_associations def test_include_bang_has_one_associations
include! :comment, :value => @post.comment include! :comment, value: @post.comment
assert_equal({ assert_equal({
:comment_id => 1 comment_id: 1
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" }] comments: [{ id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }]
}, @root_hash) }, @root_hash)
end end
end end
@ -119,12 +119,12 @@ class AssociationTest < ActiveModel::TestCase
include! :comments include! :comments
assert_equal({ assert_equal({
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
@ -137,134 +137,134 @@ class AssociationTest < ActiveModel::TestCase
include! :comment include! :comment
assert_equal({ assert_equal({
:comment_id => 1 comment_id: 1
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_with_default_has_many_with_custom_key def test_with_default_has_many_with_custom_key
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :key => :custom_comments has_many :comments, key: :custom_comments
end end
include! :comments include! :comments
assert_equal({ assert_equal({
:custom_comments => [ 1 ] custom_comments: [ 1 ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_with_default_has_one_with_custom_key def test_with_default_has_one_with_custom_key
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :key => :custom_comment_id has_one :comment, key: :custom_comment_id
end end
include! :comment include! :comment
assert_equal({ assert_equal({
:custom_comment_id => 1 custom_comment_id: 1
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_with_default_has_many_with_custom_embed_key def test_with_default_has_many_with_custom_embed_key
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed_key => :external_id has_many :comments, embed_key: :external_id
end end
include! :comments include! :comments
assert_equal({ assert_equal({
:comment_ids => [ "COMM001" ] comment_ids: [ "COMM001" ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_with_default_has_one_with_custom_embed_key def test_with_default_has_one_with_custom_embed_key
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :embed_key => :external_id has_one :comment, embed_key: :external_id
end end
include! :comment include! :comment
assert_equal({ assert_equal({
:comment_id => "COMM001" comment_id: "COMM001"
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_with_default_has_many_with_custom_key_and_custom_embed_key def test_with_default_has_many_with_custom_key_and_custom_embed_key
@post_serializer_class.class_eval do @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 end
include! :comments include! :comments
assert_equal({ assert_equal({
:custom_comments => [ "COMM001" ] custom_comments: [ "COMM001" ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_with_default_has_one_with_custom_key_and_custom_embed_key def test_with_default_has_one_with_custom_key_and_custom_embed_key
@post_serializer_class.class_eval do @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 end
include! :comment include! :comment
assert_equal({ assert_equal({
:custom_comment => "COMM001" custom_comment: "COMM001"
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_embed_objects_for_has_many_associations def test_embed_objects_for_has_many_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed => :objects has_many :comments, embed: :objects
end end
include_bare! :comments include_bare! :comments
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @hash) }, @hash)
@ -273,13 +273,13 @@ class AssociationTest < ActiveModel::TestCase
def test_embed_ids_for_has_many_associations def test_embed_ids_for_has_many_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed => :ids has_many :comments, embed: :ids
end end
include_bare! :comments include_bare! :comments
assert_equal({ assert_equal({
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, @hash) }, @hash)
assert_equal({}, @root_hash) assert_equal({}, @root_hash)
@ -287,7 +287,7 @@ class AssociationTest < ActiveModel::TestCase
def test_embed_false_for_has_many_associations def test_embed_false_for_has_many_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed => false has_many :comments, embed: false
end end
include_bare! :comments include_bare! :comments
@ -298,31 +298,31 @@ class AssociationTest < ActiveModel::TestCase
def test_embed_ids_include_true_for_has_many_associations def test_embed_ids_include_true_for_has_many_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed => :ids, :include => true has_many :comments, embed: :ids, include: true
end end
include_bare! :comments include_bare! :comments
assert_equal({ assert_equal({
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
def test_embed_ids_for_has_one_associations def test_embed_ids_for_has_one_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :embed => :ids has_one :comment, embed: :ids
end end
include_bare! :comment include_bare! :comment
assert_equal({ assert_equal({
:comment_id => 1 comment_id: 1
}, @hash) }, @hash)
assert_equal({}, @root_hash) assert_equal({}, @root_hash)
@ -330,7 +330,7 @@ class AssociationTest < ActiveModel::TestCase
def test_embed_false_for_has_one_associations def test_embed_false_for_has_one_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :embed => false has_one :comment, embed: false
end end
include_bare! :comment include_bare! :comment
@ -341,18 +341,18 @@ class AssociationTest < ActiveModel::TestCase
def test_embed_ids_include_true_for_has_one_associations def test_embed_ids_include_true_for_has_one_associations
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :embed => :ids, :include => true has_one :comment, embed: :ids, include: true
end end
include_bare! :comment include_bare! :comment
assert_equal({ assert_equal({
:comment_id => 1 comment_id: 1
}, @hash) }, @hash)
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, @root_hash) }, @root_hash)
end end
@ -361,8 +361,8 @@ class AssociationTest < ActiveModel::TestCase
@post.recent_comment = @comment @post.recent_comment = @comment
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :embed => :ids, :include => true has_one :comment, embed: :ids, include: true
has_one :recent_comment, :embed => :ids, :include => true, :root => :comments has_one :recent_comment, embed: :ids, include: true, root: :comments
end end
# Count how often the @comment record is serialized. # 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 def test_include_with_read_association_id_for_serialization_hook
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_one :comment, :embed => :ids, :include => true has_one :comment, embed: :ids, include: true
end end
association_name = nil association_name = nil
@ -399,13 +399,13 @@ class AssociationTest < ActiveModel::TestCase
include_bare! :comment include_bare! :comment
assert_equal({ assert_equal({
:comment_id => 1 comment_id: 1
}, @hash) }, @hash)
end end
def test_include_with_read_association_ids_for_serialization_hook def test_include_with_read_association_ids_for_serialization_hook
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed => :ids, :include => false has_many :comments, embed: :ids, include: false
end end
association_name = nil association_name = nil
@ -422,7 +422,7 @@ class AssociationTest < ActiveModel::TestCase
include_bare! :comments include_bare! :comments
assert_equal({ assert_equal({
:comment_ids => [1] comment_ids: [1]
}, @hash) }, @hash)
end end
end end
@ -433,13 +433,13 @@ class AssociationTest < ActiveModel::TestCase
class FooSerializer < ActiveModel::Serializer class FooSerializer < ActiveModel::Serializer
root :foos root :foos
attributes :id 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 end
class BarSerializer < ActiveModel::Serializer class BarSerializer < ActiveModel::Serializer
root :bars root :bars
attributes :id 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 end
class Foo < Model class Foo < Model
@ -453,26 +453,26 @@ class AssociationTest < ActiveModel::TestCase
def setup def setup
super super
foo = Foo.new(:id => 1) foo = Foo.new(id: 1)
bar = Bar.new(:id => 2) bar = Bar.new(id: 2)
foo.bars = [ bar ] foo.bars = [ bar ]
bar.foos = [ foo ] bar.foos = [ foo ]
collection = [ foo ] collection = [ foo ]
@serializer = collection.active_model_serializer.new(collection, :root => :foos) @serializer = collection.active_model_serializer.new(collection, root: :foos)
end end
def test_mutual_relation_result def test_mutual_relation_result
assert_equal({ assert_equal({
:foos => [{ foos: [{
:bar_ids => [ 2 ], bar_ids: [ 2 ],
:id => 1 id: 1
}], }],
:bars => [{ bars: [{
:foo_ids => [ 1 ], foo_ids: [ 1 ],
:id => 2 id: 2
}] }]
}, @serializer.as_json) }, @serializer.as_json)
end end
@ -492,77 +492,77 @@ class AssociationTest < ActiveModel::TestCase
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
root :post root :post
embed :ids, :include => true embed :ids, include: true
has_many :comments, :serializer => comment_serializer_class has_many :comments, serializer: comment_serializer_class
end end
end end
def test_when_it_is_included def test_when_it_is_included
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, :include => [:comments] @post, include: [:comments]
) )
json = post_serializer.as_json json = post_serializer.as_json
assert_equal({ assert_equal({
:post => { post: {
:title => "New Post", title: "New Post",
:body => "Body", body: "Body",
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, },
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, json) }, json)
end end
def test_when_it_is_not_included def test_when_it_is_not_included
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, :include => [] @post, include: []
) )
json = post_serializer.as_json json = post_serializer.as_json
assert_equal({ assert_equal({
:post => { post: {
:title => "New Post", title: "New Post",
:body => "Body", body: "Body",
:comment_ids => [ 1 ] comment_ids: [ 1 ]
} }
}, json) }, json)
end end
def test_when_it_is_excluded def test_when_it_is_excluded
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, :exclude => [:comments] @post, exclude: [:comments]
) )
json = post_serializer.as_json json = post_serializer.as_json
assert_equal({ assert_equal({
:post => { post: {
:title => "New Post", title: "New Post",
:body => "Body", body: "Body",
:comment_ids => [ 1 ] comment_ids: [ 1 ]
} }
}, json) }, json)
end end
def test_when_it_is_not_excluded def test_when_it_is_not_excluded
post_serializer = @post_serializer_class.new( post_serializer = @post_serializer_class.new(
@post, :exclude => [] @post, exclude: []
) )
json = post_serializer.as_json json = post_serializer.as_json
assert_equal({ assert_equal({
:post => { post: {
:title => "New Post", title: "New Post",
:body => "Body", body: "Body",
:comment_ids => [ 1 ] comment_ids: [ 1 ]
}, },
:comments => [ comments: [
{ :id => 1, :external_id => "COMM001", :body => "ZOMG A COMMENT" } { id: 1, external_id: "COMM001", body: "ZOMG A COMMENT" }
] ]
}, json) }, json)
end end
@ -575,14 +575,14 @@ class AssociationTest < ActiveModel::TestCase
def test_specifying_serializer_class_as_string def test_specifying_serializer_class_as_string
@post_serializer_class.class_eval do @post_serializer_class.class_eval do
has_many :comments, :embed => :objects has_many :comments, embed: :objects
end end
include_bare! :comments, :serializer => "AssociationTest::StringSerializerOption::StringSerializer" include_bare! :comments, serializer: "AssociationTest::StringSerializerOption::StringSerializer"
assert_equal({ assert_equal({
:comments => [ comments: [
{ :id => 1, :body => "ZOMG A COMMENT" } { id: 1, body: "ZOMG A COMMENT" }
] ]
}, @hash) }, @hash)

View File

@ -7,7 +7,7 @@ class NoSerializationScopeTest < ActionController::TestCase
end end
def as_json(*) def as_json(*)
{ :scope => @options[:scope].as_json } { scope: @options[:scope].as_json }
end end
end end
@ -21,14 +21,14 @@ class NoSerializationScopeTest < ActionController::TestCase
serialization_scope nil serialization_scope nil
def index def index
render :json => ScopeSerializable.new render json: ScopeSerializable.new
end end
end end
tests NoSerializationScopeController tests NoSerializationScopeController
def test_disabled_serialization_scope def test_disabled_serialization_scope
get :index, :format => :json get :index, format: :json
assert_equal '{"scope":null}', @response.body assert_equal '{"scope":null}', @response.body
end end
end end

View File

@ -21,7 +21,7 @@ class DefaultScopeNameTest < ActionController::TestCase
end end
def render_new_user def render_new_user
render :json => TestUser.new('pete', false), :serializer => UserSerializer render json: TestUser.new('pete', false), serializer: UserSerializer
end end
end end
@ -54,7 +54,7 @@ class SerializationScopeNameTest < ActionController::TestCase
end end
def render_new_user def render_new_user
render :json => TestUser.new('pete', false), :serializer => AdminUserSerializer render json: TestUser.new('pete', false), serializer: AdminUserSerializer
end end
end end
@ -85,7 +85,7 @@ class SerializationActionScopeOverrideTest < ActionController::TestCase
end end
def render_new_user 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
end end

View File

@ -4,13 +4,13 @@ require 'pathname'
class RenderJsonTest < ActionController::TestCase class RenderJsonTest < ActionController::TestCase
class JsonRenderable class JsonRenderable
def as_json(options={}) 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.except!(*options[:except]) if options[:except]
hash hash
end end
def to_json(options = {}) def to_json(options = {})
super :except => [:c, :e] super except: [:c, :e]
end end
end end
@ -20,9 +20,9 @@ class RenderJsonTest < ActionController::TestCase
end end
def as_json(*) def as_json(*)
hash = { :object => serializable_hash, :scope => @options[:scope].as_json } hash = { object: serializable_hash, scope: @options[:scope].as_json }
hash.merge!(:options => true) if @options[:options] hash.merge!(options: true) if @options[:options]
hash.merge!(:check_defaults => true) if @options[:check_defaults] hash.merge!(check_defaults: true) if @options[:check_defaults]
hash hash
end end
@ -41,7 +41,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def as_json(*) def as_json(*)
{ :serializable_object => true } { serializable_object: true }
end end
end end
@ -50,7 +50,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def as_json(*) def as_json(*)
{ :hello => true } { hello: true }
end end
end end
@ -59,7 +59,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def as_json(*) def as_json(*)
{ :rails => 'rocks' } { rails: 'rocks' }
end end
end end
@ -75,7 +75,7 @@ class RenderJsonTest < ActionController::TestCase
class HypermediaSerializer < ActiveModel::Serializer class HypermediaSerializer < ActiveModel::Serializer
def as_json(*) def as_json(*)
{ :link => hypermedia_url } { link: hypermedia_url }
end end
end end
@ -94,111 +94,111 @@ class RenderJsonTest < ActionController::TestCase
end end
def render_json_nil def render_json_nil
render :json => nil render json: nil
end end
def render_json_render_to_string def render_json_render_to_string
render :text => render_to_string(:json => '[]') render text: render_to_string(json: '[]')
end end
def render_json_hello_world def render_json_hello_world
render :json => ActiveSupport::JSON.encode(:hello => 'world') render json: ActiveSupport::JSON.encode(hello: 'world')
end end
def render_json_hello_world_with_status 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 end
def render_json_hello_world_with_callback 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 end
def render_json_with_custom_content_type 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 end
def render_symbol_json def render_symbol_json
render :json => ActiveSupport::JSON.encode(:hello => 'world') render json: ActiveSupport::JSON.encode(hello: 'world')
end end
def render_json_nil_with_custom_serializer def render_json_nil_with_custom_serializer
render :json => nil, :serializer => DummyCustomSerializer render json: nil, serializer: DummyCustomSerializer
end end
def render_json_with_extra_options def render_json_with_extra_options
render :json => JsonRenderable.new, :except => [:c, :e] render json: JsonRenderable.new, except: [:c, :e]
end end
def render_json_without_options def render_json_without_options
render :json => JsonRenderable.new render json: JsonRenderable.new
end end
def render_json_with_serializer def render_json_with_serializer
@current_user = Struct.new(:as_json).new(:current_user => true) @current_user = Struct.new(:as_json).new(current_user: true)
render :json => JsonSerializable.new render json: JsonSerializable.new
end end
def render_json_with_serializer_and_implicit_root def render_json_with_serializer_and_implicit_root
@current_user = Struct.new(:as_json).new(:current_user => true) @current_user = Struct.new(:as_json).new(current_user: true)
render :json => [JsonSerializable.new] render json: [JsonSerializable.new]
end end
def render_json_with_serializer_and_options def render_json_with_serializer_and_options
@current_user = Struct.new(:as_json).new(:current_user => true) @current_user = Struct.new(:as_json).new(current_user: true)
render :json => JsonSerializable.new, :options => true render json: JsonSerializable.new, options: true
end end
def render_json_with_serializer_and_scope_option def render_json_with_serializer_and_scope_option
@current_user = Struct.new(:as_json).new(:current_user => true) @current_user = Struct.new(:as_json).new(current_user: true)
scope = Struct.new(:as_json).new(:current_user => false) scope = Struct.new(:as_json).new(current_user: false)
render :json => JsonSerializable.new, :scope => scope render json: JsonSerializable.new, scope: scope
end end
def render_json_with_serializer_api_but_without_serializer def render_json_with_serializer_api_but_without_serializer
@current_user = Struct.new(:as_json).new(:current_user => true) @current_user = Struct.new(:as_json).new(current_user: true)
render :json => JsonSerializable.new(true) render json: JsonSerializable.new(true)
end end
# To specify a custom serializer for an object, use :serializer. # To specify a custom serializer for an object, use :serializer.
def render_json_with_custom_serializer def render_json_with_custom_serializer
render :json => Object.new, :serializer => CustomSerializer render json: Object.new, serializer: CustomSerializer
end end
# To specify a custom serializer for each item in the Array, use :each_serializer. # To specify a custom serializer for each item in the Array, use :each_serializer.
def render_json_array_with_custom_serializer def render_json_array_with_custom_serializer
render :json => [Object.new], :each_serializer => CustomSerializer render json: [Object.new], each_serializer: CustomSerializer
end end
def render_json_array_with_wrong_option def render_json_array_with_wrong_option
render :json => [Object.new], :serializer => CustomSerializer render json: [Object.new], serializer: CustomSerializer
end end
def render_json_with_links def render_json_with_links
render :json => HypermediaSerializable.new render json: HypermediaSerializable.new
end end
def render_json_array_with_no_root def render_json_array_with_no_root
render :json => [], :root => false render json: [], root: false
end end
def render_json_empty_array def render_json_empty_array
render :json => [] render json: []
end end
def render_json_array_with_custom_array_serializer def render_json_array_with_custom_array_serializer
render :json => [], :serializer => CustomArraySerializer render json: [], serializer: CustomArraySerializer
end end
private private
def default_serializer_options def default_serializer_options
defaults = {} defaults = {}
defaults.merge!(:check_defaults => true) if params[:check_defaults] defaults.merge!(check_defaults: true) if params[:check_defaults]
defaults.merge!(:root => :awesome) if params[:check_default_root] defaults.merge!(root: :awesome) if params[:check_default_root]
defaults.merge!(:scope => :current_admin) if params[:check_default_scope] defaults.merge!(scope: :current_admin) if params[:check_default_scope]
defaults.merge!(:serializer => AnotherCustomSerializer) if params[:check_default_serializer] defaults.merge!(serializer: AnotherCustomSerializer) if params[:check_default_serializer]
defaults.merge!(:each_serializer => AnotherCustomSerializer) if params[:check_default_each_serializer] defaults.merge!(each_serializer: AnotherCustomSerializer) if params[:check_default_each_serializer]
defaults defaults
end end
end end
@ -279,19 +279,19 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_with_serializer_checking_defaults 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 '"scope":{"current_user":true}', @response.body
assert_match '"object":{"serializable_object":true}', @response.body assert_match '"object":{"serializable_object":true}', @response.body
assert_match '"check_defaults":true', @response.body assert_match '"check_defaults":true', @response.body
end end
def test_render_json_with_serializer_checking_default_serailizer 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 assert_match '{"rails":"rocks"}', @response.body
end end
def test_render_json_with_serializer_checking_default_scope 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 assert_match '"scope":"current_admin"', @response.body
end end
@ -301,7 +301,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_with_serializer_and_implicit_root_checking_default_each_serailizer 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 assert_match '"test":[{"rails":"rocks"}]', @response.body
end end
@ -318,7 +318,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_with_serializer_and_scope_option_checking_default_scope 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 assert_match '"scope":{"current_user":false}', @response.body
end end
@ -333,7 +333,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_with_custom_serializer_checking_default_serailizer 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 assert_match '{"hello":true}', @response.body
end end
@ -349,7 +349,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_array_with_custom_serializer_checking_default_each_serailizer 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 assert_match '{"test":[{"hello":true}]}', @response.body
end end
@ -364,7 +364,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_array_with_no_root_checking_default_root 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 assert_equal '[]', @response.body
end end
@ -374,7 +374,7 @@ class RenderJsonTest < ActionController::TestCase
end end
def test_render_json_empty_array_checking_default_root 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 assert_equal '{"awesome":[]}', @response.body
end end

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@ class Model
end end
def as_json(*) def as_json(*)
{ :model => "Model" } { model: "Model" }
end end
end end
@ -26,7 +26,7 @@ class User
attr_accessor :superuser attr_accessor :superuser
def initialize(hash={}) 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 end
def read_attribute_for_serialization(name) def read_attribute_for_serialization(name)
@ -58,31 +58,31 @@ class UserSerializer < ActiveModel::Serializer
attributes :first_name, :last_name attributes :first_name, :last_name
def serializable_hash def serializable_hash
attributes.merge(:ok => true).merge(options[:scope]) attributes.merge(ok: true).merge(options[:scope])
end end
end end
class UserAttributesWithKeySerializer < ActiveModel::Serializer 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 def serializable_hash
attributes.merge(:ok => true).merge(options[:scope]) attributes.merge(ok: true).merge(options[:scope])
end end
end end
class UserAttributesWithSomeKeySerializer < ActiveModel::Serializer class UserAttributesWithSomeKeySerializer < ActiveModel::Serializer
attributes :first_name, :last_name => :l_name attributes :first_name, last_name: :l_name
def serializable_hash def serializable_hash
attributes.merge(:ok => true).merge(options[:scope]) attributes.merge(ok: true).merge(options[:scope])
end end
end end
class UserAttributesWithUnsymbolizableKeySerializer < ActiveModel::Serializer class UserAttributesWithUnsymbolizableKeySerializer < ActiveModel::Serializer
attributes :first_name, :last_name => :"last-name" attributes :first_name, last_name: :"last-name"
def serializable_hash def serializable_hash
attributes.merge(:ok => true).merge(options[:scope]) attributes.merge(ok: true).merge(options[:scope])
end end
end end
@ -95,7 +95,7 @@ class MyUserSerializer < ActiveModel::Serializer
def serializable_hash def serializable_hash
hash = attributes hash = attributes
hash = hash.merge(:super_user => true) if object.super_user? hash = hash.merge(super_user: true) if object.super_user?
hash hash
end end
end end
@ -108,7 +108,7 @@ class CommentSerializer
attr_reader :object attr_reader :object
def serializable_hash def serializable_hash
{ :title => @object.read_attribute_for_serialization(:title) } { title: @object.read_attribute_for_serialization(:title) }
end end
def as_json(options=nil) def as_json(options=nil)
@ -116,20 +116,20 @@ class CommentSerializer
if options[:root] == false if options[:root] == false
serializable_hash serializable_hash
else else
{ :comment => serializable_hash } { comment: serializable_hash }
end end
end end
end end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
attributes :title, :body attributes :title, :body
has_many :comments, :serializer => CommentSerializer has_many :comments, serializer: CommentSerializer
end end
class PostWithConditionalCommentsSerializer < ActiveModel::Serializer class PostWithConditionalCommentsSerializer < ActiveModel::Serializer
root :post root :post
attributes :title, :body attributes :title, :body
has_many :comments, :serializer => CommentSerializer has_many :comments, serializer: CommentSerializer
def include_associations! def include_associations!
include! :comments unless object.comments_disabled include! :comments unless object.comments_disabled
@ -139,7 +139,7 @@ end
class PostWithMultipleConditionalsSerializer < ActiveModel::Serializer class PostWithMultipleConditionalsSerializer < ActiveModel::Serializer
root :post root :post
attributes :title, :body, :author attributes :title, :body, :author
has_many :comments, :serializer => CommentSerializer has_many :comments, serializer: CommentSerializer
def include_comments? def include_comments?
!object.comments_disabled !object.comments_disabled
@ -159,7 +159,7 @@ class AuthorSerializer < ActiveModel::Serializer
end end
class BlogSerializer < ActiveModel::Serializer class BlogSerializer < ActiveModel::Serializer
has_one :author, :serializer => AuthorSerializer has_one :author, serializer: AuthorSerializer
end end
class BlogWithRootSerializer < BlogSerializer class BlogWithRootSerializer < BlogSerializer
@ -175,8 +175,8 @@ class CustomBlog < Blog
end end
class CustomBlogSerializer < ActiveModel::Serializer class CustomBlogSerializer < ActiveModel::Serializer
has_many :public_posts, :key => :posts, :serializer => PostSerializer has_many :public_posts, key: :posts, serializer: PostSerializer
has_one :public_user, :key => :user, :serializer => UserSerializer has_one :public_user, key: :user, serializer: UserSerializer
end end
class SomeSerializer < ActiveModel::Serializer class SomeSerializer < ActiveModel::Serializer