Merge pull request #321 from teeparham/ruby19

Remove support for ruby 1.8 (fixes #310)
This commit is contained in:
Santiago Pastorino 2013-05-31 15:51:25 -07:00
commit 1d7cb2da75
20 changed files with 656 additions and 687 deletions

View File

@ -1,13 +1,8 @@
language: ruby language: ruby
rvm: rvm:
- 1.8.7
- 1.9.2
- 1.9.3 - 1.9.3
- 2.0.0 - 2.0.0
- ree
- jruby-18mode
- jruby-19mode - jruby-19mode
- rbx-18mode
- rbx-19mode - rbx-19mode
gemfile: gemfile:
- Gemfile - Gemfile
@ -15,18 +10,6 @@ gemfile:
matrix: matrix:
allow_failures: allow_failures:
- gemfile: Gemfile.edge - gemfile: Gemfile.edge
exclude:
# Edge Rails is only compatible with 1.9.3
- gemfile: Gemfile.edge
rvm: 1.8.7
- gemfile: Gemfile.edge
rvm: 1.9.2
- gemfile: Gemfile.edge
rvm: ree
- gemfile: Gemfile.edge
rvm: jruby-18mode
- gemfile: Gemfile.edge
rvm: rbx-18mode
notifications: notifications:
email: false email: false
campfire: campfire:

View File

@ -4,26 +4,26 @@
AM::Serializer and AM::ArraySerializer. Basically enable objects to be AM::Serializer and AM::ArraySerializer. Basically enable objects to be
serializable by implementing an options method to handle the options serializable by implementing an options method to handle the options
of the serialization and a serialize method that returns an object to of the serialization and a serialize method that returns an object to
be converted to json by the module. This also removes duplicate code. be converted to json by the module. This also removes duplicate code.
https://github.com/rails-api/active_model_serializers/commit/6c6bc8872d3b0f040a200854fa5530a775824dbf https://github.com/rails-api/active_model_serializers/commit/6c6bc8872d3b0f040a200854fa5530a775824dbf
* ActiveModel::Serializer::Caching module was created it enables * ActiveModel::Serializer::Caching module was created it enables
Serializers to be able to cache to\_json and serialize calls. This Serializers to be able to cache to\_json and serialize calls. This
also helps removing duplicate code. also helps removing duplicate code.
https://github.com/rails-api/active_model_serializers/commit/3e27110df78696ac48cafd1568f72216f348a188 https://github.com/rails-api/active_model_serializers/commit/3e27110df78696ac48cafd1568f72216f348a188
* We got rid of the Association.refine method which generated * We got rid of the Association.refine method which generated
subclasses. subclasses.
https://github.com/rails-api/active_model_serializers/commit/24923722d4f215c7cfcdf553fd16582e28e3801b https://github.com/rails-api/active_model_serializers/commit/24923722d4f215c7cfcdf553fd16582e28e3801b
* Associations doesn't know anymore about the source serializer. * Associations doesn't know anymore about the source serializer.
That didn't make any sense. That didn't make any sense.
https://github.com/rails-api/active_model_serializers/commit/2252e8fe6dbf45660c6a35f35e2423792f2c3abf https://github.com/rails-api/active_model_serializers/commit/2252e8fe6dbf45660c6a35f35e2423792f2c3abf
https://github.com/rails-api/active_model_serializers/commit/87eadd09b9a988bc1d9b30d9a501ef7e3fc6bb87 https://github.com/rails-api/active_model_serializers/commit/87eadd09b9a988bc1d9b30d9a501ef7e3fc6bb87
https://github.com/rails-api/active_model_serializers/commit/79a6e13e8f7fae2eb4f48e83a9633e74beb6739e https://github.com/rails-api/active_model_serializers/commit/79a6e13e8f7fae2eb4f48e83a9633e74beb6739e
* Passing options[:hash] is not public API of include!. That was * Passing options[:hash] is not public API of include!. That was
removed. removed.
https://github.com/rails-api/active_model_serializers/commit/5cbf9317051002a32c90c3f995b8b2f126f70d0c https://github.com/rails-api/active_model_serializers/commit/5cbf9317051002a32c90c3f995b8b2f126f70d0c
* ActiveModel::Serializer::Associations::Config is now * ActiveModel::Serializer::Associations::Config is now
@ -31,8 +31,8 @@
thing so shouldn't bother. thing so shouldn't bother.
ActiveModel::Serializer::Associations::Has\* are now ActiveModel::Serializer::Associations::Has\* are now
ActiveModel::Serializer::Association::Has\* and inherit from ActiveModel::Serializer::Association::Has\* and inherit from
ActiveModel::Serializer::Association ActiveModel::Serializer::Association
https://github.com/rails-api/active_model_serializers/commit/f5de334ddf1f3b9764d914a717311532021785d2 https://github.com/rails-api/active_model_serializers/commit/f5de334ddf1f3b9764d914a717311532021785d2
https://github.com/rails-api/active_model_serializers/commit/3dd422d99e8c57f113880da34f6abe583c4dadf9 https://github.com/rails-api/active_model_serializers/commit/3dd422d99e8c57f113880da34f6abe583c4dadf9
* serialize\_ids call methods on the corresponding serializer if they * serialize\_ids call methods on the corresponding serializer if they
@ -42,6 +42,10 @@
* Array items are not wrapped anymore in root element. * Array items are not wrapped anymore in root element.
* Remove support for ruby 1.8 versions.
* Require rails >= 3.2.
# VERSION 0.8.1 # VERSION 0.8.1
* Fix bug whereby a serializer using 'options' would blow up. * Fix bug whereby a serializer using 'options' would blow up.

View File

@ -3,4 +3,4 @@ source 'https://rubygems.org'
# Specify gem dependencies in active_model_serializers.gemspec # Specify gem dependencies in active_model_serializers.gemspec
gemspec gemspec
gem "coveralls", :require => false gem "coveralls", require: false

View File

@ -13,13 +13,13 @@ content.
In short, **serializers replace hash-driven development with object-oriented In short, **serializers replace hash-driven development with object-oriented
development.** development.**
# Installing Serializers # Installing
The easiest way to install `ActiveModel::Serializers` is to add it to your The easiest way to install `ActiveModel::Serializers` is to add it to your
`Gemfile`: `Gemfile`:
```ruby ```ruby
gem "active_model_serializers", "~> 0.8.0" gem "active_model_serializers"
``` ```
Then, install it on the command line: Then, install it on the command line:
@ -28,6 +28,16 @@ Then, install it on the command line:
$ bundle install $ bundle install
``` ```
#### Ruby 1.8 is no longer supported!
If you must use a ruby 1.8 version (MRI 1.8.7, REE, Rubinius 1.8, or JRuby 1.8), you need to use version 0.8.x.
Versions after 0.9.0 do not support ruby 1.8. To specify version 0.8, include this in your Gemfile:
```ruby
gem "active_model_serializers", "~> 0.8.0"
```
# Creating a Serializer # Creating a Serializer
The easiest way to create a new serializer is to generate a new resource, which The easiest way to create a new serializer is to generate a new resource, which

View File

@ -15,4 +15,4 @@ task :bench do
load 'bench/perf.rb' load 'bench/perf.rb'
end end
task :default => :test task default: :test

View File

@ -17,8 +17,11 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"] gem.require_paths = ["lib"]
gem.version = ActiveModel::Serializer::VERSION gem.version = ActiveModel::Serializer::VERSION
gem.add_dependency 'activemodel', '>= 3.0' gem.required_ruby_version = ">= 1.9.3"
gem.add_development_dependency "rails", ">= 3.0"
gem.add_dependency "activemodel", ">= 3.2"
gem.add_development_dependency "rails", ">= 3.2"
gem.add_development_dependency "pry" gem.add_development_dependency "pry"
gem.add_development_dependency "simplecov" gem.add_development_dependency "simplecov"
gem.add_development_dependency "coveralls" gem.add_development_dependency "coveralls"

View File

@ -52,7 +52,7 @@ module ActiveModel
end end
serializer ||= DefaultSerializer serializer ||= DefaultSerializer
serializable = serializer.new(item, options.merge(:root => nil)) serializable = serializer.new(item, options.merge(root: nil))
if serializable.respond_to?(:serializable_hash) if serializable.respond_to?(:serializable_hash)
serializable.serializable_hash serializable.serializable_hash

View File

@ -13,7 +13,7 @@ module ActiveModel
# it expects two objects as arguments, a resource and options. For example, # it expects two objects as arguments, a resource and options. For example,
# one may do in a controller: # one may do in a controller:
# #
# PostSerializer.new(@post, :scope => current_user).to_json # PostSerializer.new(@post, scope: current_user).to_json
# #
# The object to be serialized is the +@post+ and the current user is passed # The object to be serialized is the +@post+ and the current user is passed
# in for authorization purposes. # in for authorization purposes.
@ -30,7 +30,7 @@ module ActiveModel
# #
# def attributes # def attributes
# hash = super # hash = super
# hash.merge!(:email => post.email) if author? # hash.merge!(email: post.email) if author?
# hash # hash
# end # end
# #
@ -46,7 +46,7 @@ module ActiveModel
include ActiveModel::Serializer::Caching include ActiveModel::Serializer::Caching
INCLUDE_METHODS = {} INCLUDE_METHODS = {}
INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" } INSTRUMENT = { serialize: :"serialize.serializer", associations: :"associations.serializer" }
class IncludeError < StandardError class IncludeError < StandardError
attr_reader :source, :association attr_reader :source, :association
@ -86,7 +86,7 @@ module ActiveModel
attrs.each do |attr| attrs.each do |attr|
if Hash === attr if Hash === attr
attr.each {|attr_real, key| attribute attr_real, :key => key } attr.each {|attr_real, key| attribute(attr_real, key: key) }
else else
attribute attr attribute attr
end end
@ -172,20 +172,20 @@ module ActiveModel
# #
# The +attributes+ hash looks like this: # The +attributes+ hash looks like this:
# #
# { :name => :string, :age => :integer } # { name: :string, age: :integer }
# #
# The +associations+ hash looks like this: # The +associations+ hash looks like this:
# { :posts => { :has_many => :posts } } # { posts: { has_many: :posts } }
# #
# If :key is used: # If :key is used:
# #
# class PostsSerializer < ActiveModel::Serializer # class PostsSerializer < ActiveModel::Serializer
# has_many :posts, :key => :my_posts # has_many :posts, key: :my_posts
# end # end
# #
# the hash looks like this: # the hash looks like this:
# #
# { :my_posts => { :has_many => :posts } # { my_posts: { has_many: :posts }
# #
# This information is extracted from the serializer's model class, # This information is extracted from the serializer's model class,
# which is provided by +SerializerClass.model_class+. # which is provided by +SerializerClass.model_class+.
@ -232,7 +232,7 @@ module ActiveModel
end end
end end
{ :attributes => attrs, :associations => associations } { attributes: attrs, associations: associations }
end end
# The model class associated with this serializer. # The model class associated with this serializer.
@ -244,7 +244,7 @@ module ActiveModel
# #
# embed :objects # Embed associations as full objects # embed :objects # Embed associations as full objects
# embed :ids # Embed only the association ids # embed :ids # Embed only the association ids
# embed :ids, :include => true # Embed the association ids and include objects in the root # embed :ids, include: true # Embed the association ids and include objects in the root
# #
def embed(type, options={}) def embed(type, options={})
self._embed = type self._embed = type
@ -323,7 +323,7 @@ module ActiveModel
# Returns a json representation of the serializable # Returns a json representation of the serializable
# object including the root. # object including the root.
def as_json(args={}) def as_json(args={})
super(:root => args.fetch(:root, options.fetch(:root, root_name))) super(root: args.fetch(:root, options.fetch(:root, root_name)))
end end
def serialize_object def serialize_object
@ -451,8 +451,8 @@ module ActiveModel
def default_embed_options def default_embed_options
{ {
:embed => _embed, embed: _embed,
:include => _root_embed include: _root_embed
} }
end end
end end

View File

@ -12,7 +12,7 @@ module ActiveModel
# embed: Define how associations should be embedded. # embed: Define how associations should be embedded.
# - :objects # Embed associations as full objects. # - :objects # Embed associations as full objects.
# - :ids # Embed only the association ids. # - :ids # Embed only the association ids.
# - :ids, :include => true # Embed the association ids and include objects in the root. # - :ids, include: true # Embed the association ids and include objects in the root.
# #
# include: Used in conjunction with embed :ids. Includes the objects in the root. # include: Used in conjunction with embed :ids. Includes the objects in the root.
# #
@ -158,8 +158,8 @@ module ActiveModel
if polymorphic? if polymorphic?
{ {
:type => polymorphic_key, type: polymorphic_key,
:id => id id: id
} }
else else
id id

View File

@ -11,8 +11,6 @@ if defined?(Rails)
module ActiveModel module ActiveModel
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
generators do |app| generators do |app|
app ||= Rails.application # Rails 3.0.x does not yield `app`
Rails::Generators.configure!(app.config.generators) Rails::Generators.configure!(app.config.generators)
Rails::Generators.hidden_namespaces.uniq! Rails::Generators.hidden_namespaces.uniq!
require_relative "generators/resource_override" require_relative "generators/resource_override"
@ -74,8 +72,8 @@ Array.send(:include, ActiveModel::ArraySerializerSupport)
Set.send(:include, ActiveModel::ArraySerializerSupport) Set.send(:include, ActiveModel::ArraySerializerSupport)
{ {
:active_record => 'ActiveRecord::Relation', active_record: 'ActiveRecord::Relation',
:mongoid => 'Mongoid::Criteria' mongoid: 'Mongoid::Criteria'
}.each do |orm, rel_class| }.each do |orm, rel_class|
ActiveSupport.on_load(orm) do ActiveSupport.on_load(orm) do
include ActiveModel::SerializerSupport include ActiveModel::SerializerSupport

View File

@ -2,20 +2,17 @@ module Rails
module Generators module Generators
class SerializerGenerator < NamedBase class SerializerGenerator < NamedBase
source_root File.expand_path("../templates", __FILE__) source_root File.expand_path("../templates", __FILE__)
check_class_collision :suffix => "Serializer" check_class_collision suffix: "Serializer"
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" argument :attributes, type: :array, default: [], banner: "field:type field:type"
class_option :parent, :type => :string, :desc => "The parent class for the generated serializer" class_option :parent, type: :string, desc: "The parent class for the generated serializer"
def create_serializer_file def create_serializer_file
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb") template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
end end
private private
def generate_id_method
RUBY_VERSION =~ /1\.8/
end
def attributes_names def attributes_names
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym } [:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
@ -28,9 +25,6 @@ module Rails
def parent_class_name def parent_class_name
if options[:parent] if options[:parent]
options[:parent] options[:parent]
# Only works on 3.2
# elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
# "ApplicationSerializer"
elsif defined?(::ApplicationSerializer) elsif defined?(::ApplicationSerializer)
"ApplicationSerializer" "ApplicationSerializer"
else else

View File

@ -4,16 +4,5 @@ class <%= class_name %>Serializer < <%= parent_class_name %>
<% association_names.each do |attribute| -%> <% association_names.each do |attribute| -%>
has_one :<%= attribute %> has_one :<%= attribute %>
<% end -%> <% end -%>
<% if generate_id_method %>
# due to the difference between 1.8 and 1.9 with respect to #id and
# #object_id, we recommend that if you wish to serialize id columns, you
# do this. Feel free to remove this if you don't feel that it's appropriate.
#
# For more: https://github.com/rails-api/active_model_serializers/issues/127
def id
object.read_attribute_for_serialization(:id)
end
<% end -%>
end end
<% end -%> <% end -%>

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

@ -36,18 +36,6 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
Object.send :remove_const, :ApplicationSerializer Object.send :remove_const, :ApplicationSerializer
end end
def test_serializer_gets_id
run_generator
assert_file "app/serializers/account_serializer.rb" do |content|
if RUBY_VERSION =~ /1.8/
assert_match /def id/, content
else
assert_no_match /def id/, content
end
end
end
# def test_uses_namespace_application_serializer_if_one_exists # def test_uses_namespace_application_serializer_if_one_exists
# Object.const_set(:SerializerNamespace, Module.new) # Object.const_set(:SerializerNamespace, Module.new)
# SerializerNamespace.const_set(:ApplicationSerializer, Class.new) # SerializerNamespace.const_set(:ApplicationSerializer, Class.new)

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