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
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- ree
- jruby-18mode
- jruby-19mode
- rbx-18mode
- rbx-19mode
gemfile:
- Gemfile
@ -15,18 +10,6 @@ gemfile:
matrix:
allow_failures:
- 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:
email: false
campfire:

View File

@ -4,26 +4,26 @@
AM::Serializer and AM::ArraySerializer. Basically enable objects to be
serializable by implementing an options method to handle the options
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
* ActiveModel::Serializer::Caching module was created it enables
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
* We got rid of the Association.refine method which generated
subclasses.
subclasses.
https://github.com/rails-api/active_model_serializers/commit/24923722d4f215c7cfcdf553fd16582e28e3801b
* Associations doesn't know anymore about the source serializer.
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/87eadd09b9a988bc1d9b30d9a501ef7e3fc6bb87
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/87eadd09b9a988bc1d9b30d9a501ef7e3fc6bb87
https://github.com/rails-api/active_model_serializers/commit/79a6e13e8f7fae2eb4f48e83a9633e74beb6739e
* Passing options[:hash] is not public API of include!. That was
removed.
removed.
https://github.com/rails-api/active_model_serializers/commit/5cbf9317051002a32c90c3f995b8b2f126f70d0c
* ActiveModel::Serializer::Associations::Config is now
@ -31,8 +31,8 @@
thing so shouldn't bother.
ActiveModel::Serializer::Associations::Has\* are now
ActiveModel::Serializer::Association::Has\* and inherit from
ActiveModel::Serializer::Association
https://github.com/rails-api/active_model_serializers/commit/f5de334ddf1f3b9764d914a717311532021785d2
ActiveModel::Serializer::Association
https://github.com/rails-api/active_model_serializers/commit/f5de334ddf1f3b9764d914a717311532021785d2
https://github.com/rails-api/active_model_serializers/commit/3dd422d99e8c57f113880da34f6abe583c4dadf9
* serialize\_ids call methods on the corresponding serializer if they
@ -42,6 +42,10 @@
* Array items are not wrapped anymore in root element.
* Remove support for ruby 1.8 versions.
* Require rails >= 3.2.
# VERSION 0.8.1
* 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
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
development.**
# Installing Serializers
# Installing
The easiest way to install `ActiveModel::Serializers` is to add it to your
`Gemfile`:
```ruby
gem "active_model_serializers", "~> 0.8.0"
gem "active_model_serializers"
```
Then, install it on the command line:
@ -28,6 +28,16 @@ Then, install it on the command line:
$ 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
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'
end
task :default => :test
task default: :test

View File

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

View File

@ -52,7 +52,7 @@ module ActiveModel
end
serializer ||= DefaultSerializer
serializable = serializer.new(item, options.merge(:root => nil))
serializable = serializer.new(item, options.merge(root: nil))
if serializable.respond_to?(: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,
# 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
# in for authorization purposes.
@ -30,7 +30,7 @@ module ActiveModel
#
# def attributes
# hash = super
# hash.merge!(:email => post.email) if author?
# hash.merge!(email: post.email) if author?
# hash
# end
#
@ -46,7 +46,7 @@ module ActiveModel
include ActiveModel::Serializer::Caching
INCLUDE_METHODS = {}
INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" }
INSTRUMENT = { serialize: :"serialize.serializer", associations: :"associations.serializer" }
class IncludeError < StandardError
attr_reader :source, :association
@ -86,7 +86,7 @@ module ActiveModel
attrs.each do |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
attribute attr
end
@ -172,20 +172,20 @@ module ActiveModel
#
# The +attributes+ hash looks like this:
#
# { :name => :string, :age => :integer }
# { name: :string, age: :integer }
#
# The +associations+ hash looks like this:
# { :posts => { :has_many => :posts } }
# { posts: { has_many: :posts } }
#
# If :key is used:
#
# class PostsSerializer < ActiveModel::Serializer
# has_many :posts, :key => :my_posts
# has_many :posts, key: :my_posts
# end
#
# 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,
# which is provided by +SerializerClass.model_class+.
@ -232,7 +232,7 @@ module ActiveModel
end
end
{ :attributes => attrs, :associations => associations }
{ attributes: attrs, associations: associations }
end
# The model class associated with this serializer.
@ -244,7 +244,7 @@ module ActiveModel
#
# embed :objects # Embed associations as full objects
# 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={})
self._embed = type
@ -323,7 +323,7 @@ module ActiveModel
# Returns a json representation of the serializable
# object including the root.
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
def serialize_object
@ -451,8 +451,8 @@ module ActiveModel
def default_embed_options
{
:embed => _embed,
:include => _root_embed
embed: _embed,
include: _root_embed
}
end
end

View File

@ -12,7 +12,7 @@ module ActiveModel
# embed: Define how associations should be embedded.
# - :objects # Embed associations as full objects.
# - :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.
#
@ -158,8 +158,8 @@ module ActiveModel
if polymorphic?
{
:type => polymorphic_key,
:id => id
type: polymorphic_key,
id: id
}
else
id

View File

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

View File

@ -2,20 +2,17 @@ module Rails
module Generators
class SerializerGenerator < NamedBase
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
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
end
private
def generate_id_method
RUBY_VERSION =~ /1\.8/
end
def attributes_names
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
@ -28,9 +25,6 @@ module Rails
def parent_class_name
if options[:parent]
options[:parent]
# Only works on 3.2
# elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
# "ApplicationSerializer"
elsif defined?(::ApplicationSerializer)
"ApplicationSerializer"
else

View File

@ -4,16 +4,5 @@ class <%= class_name %>Serializer < <%= parent_class_name %>
<% association_names.each do |attribute| -%>
has_one :<%= attribute %>
<% 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 -%>

View File

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

View File

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

View File

@ -36,18 +36,6 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
Object.send :remove_const, :ApplicationSerializer
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
# Object.const_set(:SerializerNamespace, Module.new)
# SerializerNamespace.const_set(:ApplicationSerializer, Class.new)

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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