diff --git a/.travis.yml b/.travis.yml index 82c47f19..a001825b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 40f7e00a..b4c2913d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Gemfile b/Gemfile index 71303738..79ab2b93 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/README.md b/README.md index 908bf304..a5386778 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Rakefile b/Rakefile index 23f17c4a..8c5bd75a 100644 --- a/Rakefile +++ b/Rakefile @@ -15,4 +15,4 @@ task :bench do load 'bench/perf.rb' end -task :default => :test +task default: :test diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index 0971a26e..9b551fad 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -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" diff --git a/lib/active_model/array_serializer.rb b/lib/active_model/array_serializer.rb index a1b92c4c..e752c812 100644 --- a/lib/active_model/array_serializer.rb +++ b/lib/active_model/array_serializer.rb @@ -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 diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index c3768f75..e2a6228b 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -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 diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 888b94fd..1f2b0b53 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -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 diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index a708585c..4ae2d743 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -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 diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb index b7096f83..8212d62c 100644 --- a/lib/generators/serializer/serializer_generator.rb +++ b/lib/generators/serializer/serializer_generator.rb @@ -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 diff --git a/lib/generators/serializer/templates/serializer.rb b/lib/generators/serializer/templates/serializer.rb index 6c25609d..4ebb004e 100644 --- a/lib/generators/serializer/templates/serializer.rb +++ b/lib/generators/serializer/templates/serializer.rb @@ -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 -%> diff --git a/test/array_serializer_test.rb b/test/array_serializer_test.rb index f3353829..d48e8027 100644 --- a/test/array_serializer_test.rb +++ b/test/array_serializer_test.rb @@ -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 diff --git a/test/association_test.rb b/test/association_test.rb index 2cfbd961..3ae225a0 100644 --- a/test/association_test.rb +++ b/test/association_test.rb @@ -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) diff --git a/test/generators_test.rb b/test/generators_test.rb index b1938cee..b1a05b3a 100644 --- a/test/generators_test.rb +++ b/test/generators_test.rb @@ -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) diff --git a/test/no_serialization_scope_test.rb b/test/no_serialization_scope_test.rb index 719ce4bf..31ba475f 100644 --- a/test/no_serialization_scope_test.rb +++ b/test/no_serialization_scope_test.rb @@ -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 diff --git a/test/serialization_scope_name_test.rb b/test/serialization_scope_name_test.rb index bc9c87bb..a5e164c4 100644 --- a/test/serialization_scope_name_test.rb +++ b/test/serialization_scope_name_test.rb @@ -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 diff --git a/test/serialization_test.rb b/test/serialization_test.rb index c5e1bbe3..6fe5075c 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -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 diff --git a/test/serializer_test.rb b/test/serializer_test.rb index 588da1c7..5da28d8a 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -3,7 +3,7 @@ require "test_fakes" class SerializerTest < ActiveModel::TestCase def test_scope_works_correct - serializer = ActiveModel::Serializer.new :foo, :scope => :bar + serializer = ActiveModel::Serializer.new :foo, scope: :bar assert_equal serializer.scope, :bar end @@ -14,51 +14,51 @@ class SerializerTest < ActiveModel::TestCase hash = user_serializer.as_json assert_equal({ - :default_user => { :first_name => "Jose", :last_name => "Valim" } + default_user: { first_name: "Jose", last_name: "Valim" } }, hash) end def test_attributes_method user = User.new - user_serializer = UserSerializer.new(user, :scope => {}) + user_serializer = UserSerializer.new(user, scope: {}) hash = user_serializer.as_json assert_equal({ - :user => { :first_name => "Jose", :last_name => "Valim", :ok => true } + user: { first_name: "Jose", last_name: "Valim", ok: true } }, hash) end def test_attributes_method_specifying_keys user = User.new - user_serializer = UserAttributesWithKeySerializer.new(user, :scope => {}) + user_serializer = UserAttributesWithKeySerializer.new(user, scope: {}) hash = user_serializer.as_json assert_equal({ - :user_attributes_with_key => { :f_name => "Jose", :l_name => "Valim", :ok => true } + user_attributes_with_key: { f_name: "Jose", l_name: "Valim", ok: true } }, hash) end def test_attributes_method_specifying_some_keys user = User.new - user_serializer = UserAttributesWithSomeKeySerializer.new(user, :scope => {}) + user_serializer = UserAttributesWithSomeKeySerializer.new(user, scope: {}) hash = user_serializer.as_json assert_equal({ - :user_attributes_with_some_key => { :first_name => "Jose", :l_name => "Valim", :ok => true } + user_attributes_with_some_key: { first_name: "Jose", l_name: "Valim", ok: true } }, hash) end def test_attributes_method_with_unsymbolizable_key user = User.new - user_serializer = UserAttributesWithUnsymbolizableKeySerializer.new(user, :scope => {}) + user_serializer = UserAttributesWithUnsymbolizableKeySerializer.new(user, scope: {}) hash = user_serializer.as_json assert_equal({ - :user_attributes_with_unsymbolizable_key => { :first_name => "Jose", :"last-name" => "Valim", :ok => true } + user_attributes_with_unsymbolizable_key: { first_name: "Jose", :"last-name" => "Valim", ok: true } }, hash) end @@ -69,30 +69,30 @@ class SerializerTest < ActiveModel::TestCase hash = object_serializer.as_json assert_equal({ - :some => { :some => "something" } + some: { some: "something" } }, hash) end def test_serializer_receives_scope user = User.new - user_serializer = UserSerializer.new(user, :scope => {:scope => true}) + user_serializer = UserSerializer.new(user, scope: { scope: true }) hash = user_serializer.as_json assert_equal({ - :user => { - :first_name => "Jose", - :last_name => "Valim", - :ok => true, - :scope => true + user: { + first_name: "Jose", + last_name: "Valim", + ok: true, + scope: true } }, hash) end def test_serializer_receives_url_options user = User.new - user_serializer = UserSerializer.new(user, :url_options => { :host => "test.local" }) - assert_equal({ :host => "test.local" }, user_serializer.url_options) + user_serializer = UserSerializer.new(user, url_options: { host: "test.local" }) + assert_equal({ host: "test.local" }, user_serializer.url_options) end def test_serializer_returns_empty_hash_without_url_options @@ -109,8 +109,8 @@ class SerializerTest < ActiveModel::TestCase hash = user_serializer.as_json assert_equal({ - :my_user => { - :first_name => "Jose", :last_name => "Valim", :super_user => true + my_user: { + first_name: "Jose", last_name: "Valim", super_user: true } }, hash) end @@ -118,19 +118,19 @@ class SerializerTest < ActiveModel::TestCase def test_has_many user = User.new - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")] + post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1"), Comment.new(title: "Comment2")] post.comments = comments - post_serializer = PostSerializer.new(post, :scope => user) + post_serializer = PostSerializer.new(post, scope: user) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + post: { + title: "New Post", + body: "Body of new post", + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ] } }, post_serializer.as_json) @@ -139,21 +139,21 @@ class SerializerTest < ActiveModel::TestCase def test_conditionally_included_associations user = User.new - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")] + post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1"), Comment.new(title: "Comment2")] post.comments = comments - post_serializer = PostWithConditionalCommentsSerializer.new(post, :scope => user) + post_serializer = PostWithConditionalCommentsSerializer.new(post, scope: user) # comments enabled post.comments_disabled = false assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + post: { + title: "New Post", + body: "Body of new post", + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ] } }, post_serializer.as_json) @@ -161,9 +161,9 @@ class SerializerTest < ActiveModel::TestCase # comments disabled post.comments_disabled = true assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post" + post: { + title: "New Post", + body: "Body of new post" } }, post_serializer.as_json) end @@ -171,21 +171,21 @@ class SerializerTest < ActiveModel::TestCase def test_conditionally_included_associations_and_attributes user = User.new - post = Post.new(:title => "New Post", :body => "Body of new post", :author => 'Sausage King', :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")] + post = Post.new(title: "New Post", body: "Body of new post", author: 'Sausage King', email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1"), Comment.new(title: "Comment2")] post.comments = comments - post_serializer = PostWithMultipleConditionalsSerializer.new(post, :scope => user) + post_serializer = PostWithMultipleConditionalsSerializer.new(post, scope: user) # comments enabled post.comments_disabled = false assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + post: { + title: "New Post", + body: "Body of new post", + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ] } }, post_serializer.as_json) @@ -193,19 +193,19 @@ class SerializerTest < ActiveModel::TestCase # comments disabled post.comments_disabled = true assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post" + post: { + title: "New Post", + body: "Body of new post" } }, post_serializer.as_json) # superuser - should see author user.superuser = true assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :author => "Sausage King" + post: { + title: "New Post", + body: "Body of new post", + author: "Sausage King" } }, post_serializer.as_json) end @@ -215,12 +215,12 @@ class SerializerTest < ActiveModel::TestCase blog = Blog.new blog.author = user - json = BlogSerializer.new(blog, :scope => user).as_json + json = BlogSerializer.new(blog, scope: user).as_json assert_equal({ - :blog => { - :author => { - :first_name => "Jose", - :last_name => "Valim" + blog: { + author: { + first_name: "Jose", + last_name: "Valim" } } }, json) @@ -236,17 +236,17 @@ class SerializerTest < ActiveModel::TestCase object.author end - has_one :person, :serializer => author_serializer + has_one :person, serializer: author_serializer end user = User.new blog = Blog.new blog.author = user - json = blog_serializer.new(blog, :scope => user).as_json + json = blog_serializer.new(blog, scope: user).as_json assert_equal({ - :person => { - :first_name => "Jose" + person: { + first_name: "Jose" } }, json) end @@ -254,8 +254,8 @@ class SerializerTest < ActiveModel::TestCase def post_serializer Class.new(ActiveModel::Serializer) do attributes :title, :body - has_many :comments, :serializer => CommentSerializer - has_one :author, :serializer => DefaultUserSerializer + has_many :comments, serializer: CommentSerializer + has_one :author, serializer: DefaultUserSerializer end end @@ -263,17 +263,17 @@ class SerializerTest < ActiveModel::TestCase user = User.new blog = Blog.new - json = BlogSerializer.new(blog, :scope => user).as_json + json = BlogSerializer.new(blog, scope: user).as_json assert_equal({ - :blog => { :author => nil } + blog: { author: nil } }, json) serializer = Class.new(BlogSerializer) do root :blog end - json = serializer.new(blog, :scope => user).as_json - assert_equal({ :blog => { :author => nil } }, json) + json = serializer.new(blog, scope: user).as_json + assert_equal({ blog: { author: nil } }, json) end def test_custom_root @@ -284,7 +284,7 @@ class SerializerTest < ActiveModel::TestCase root :my_blog end - assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json) + assert_equal({ my_blog: { author: nil } }, serializer.new(blog, scope: user).as_json) end def test_nil_root_object @@ -295,7 +295,7 @@ class SerializerTest < ActiveModel::TestCase root false end - assert_equal(nil, serializer.new(blog, :scope => user).as_json) + assert_equal(nil, serializer.new(blog, scope: user).as_json) end def test_custom_root_with_nil_root_object @@ -306,7 +306,7 @@ class SerializerTest < ActiveModel::TestCase root :my_blog end - assert_equal({ :my_blog => nil }, serializer.new(blog, :scope => user).as_json) + assert_equal({ my_blog: nil }, serializer.new(blog, scope: user).as_json) end def test_false_root @@ -321,20 +321,20 @@ class SerializerTest < ActiveModel::TestCase self.root = false end - assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json) - assert_equal({ :author => nil }, another_serializer.new(blog, :scope => user).as_json) + assert_equal({ author: nil }, serializer.new(blog, scope: user).as_json) + assert_equal({ author: nil }, another_serializer.new(blog, scope: user).as_json) # test inherited false root serializer = Class.new(serializer) - assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json) + assert_equal({ author: nil }, serializer.new(blog, scope: user).as_json) end def test_true_root blog = Blog.new assert_equal({ - :blog_with_root => { - :author => nil, + blog_with_root: { + author: nil, } }, BlogWithRootSerializer.new(blog).as_json) end @@ -348,7 +348,7 @@ class SerializerTest < ActiveModel::TestCase blog = Blog.new serializer = BlogSerializer.new(blog) - assert_equal({ :author => nil }, serializer.as_json) + assert_equal({ author: nil }, serializer.as_json) ensure ActiveSupport.on_load(:active_model_serializers) do self.root = nil @@ -364,18 +364,18 @@ class SerializerTest < ActiveModel::TestCase embed :ids end - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)] post.comments = comments serializer = serializer.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comment_ids => [1, 2], - :author_id => nil + post: { + title: "New Post", + body: "Body of new post", + comment_ids: [1, 2], + author_id: nil } }, serializer.as_json) end @@ -385,45 +385,45 @@ class SerializerTest < ActiveModel::TestCase serializer_class.class_eval do root :post - embed :ids, :include => true + embed :ids, include: true end - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)] post.comments = comments serializer = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comment_ids => [1, 2], - :author_id => nil + post: { + title: "New Post", + body: "Body of new post", + comment_ids: [1, 2], + author_id: nil }, - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ], - :authors => [] + authors: [] }, serializer.as_json) - post.author = User.new(:id => 1) + post.author = User.new(id: 1) serializer = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comment_ids => [1, 2], - :author_id => 1 + post: { + title: "New Post", + body: "Body of new post", + comment_ids: [1, 2], + author_id: 1 }, - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ], - :authors => [{ :first_name => "Jose", :last_name => "Valim" }] + authors: [{ first_name: "Jose", last_name: "Valim" }] }, serializer.as_json) end @@ -438,8 +438,8 @@ class SerializerTest < ActiveModel::TestCase end end - post = Post.new(:title => "My Post") - comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post = Post.new(title: "My Post") + comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)] post.comments = comments post.class_eval do @@ -449,8 +449,8 @@ class SerializerTest < ActiveModel::TestCase end json = post_serializer.new(post).as_json assert_equal({ - :title => "My Post", - :comment_ids => [1] + title: "My Post", + comment_ids: [1] }, json) end @@ -463,7 +463,7 @@ class SerializerTest < ActiveModel::TestCase post_serializer = Class.new(ActiveModel::Serializer) do attributes :title - has_many :comments, :serializer => comment_serializer + has_many :comments, serializer: comment_serializer embed :ids def comments @@ -471,8 +471,8 @@ class SerializerTest < ActiveModel::TestCase end end - post = Post.new(:title => "My Post") - comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post = Post.new(title: "My Post") + comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)] post.comments = comments post.class_eval do @@ -482,8 +482,8 @@ class SerializerTest < ActiveModel::TestCase end json = post_serializer.new(post).as_json assert_equal({ - :title => "My Post", - :comment_ids => ["OMG"] + title: "My Post", + comment_ids: ["OMG"] }, json) end @@ -495,45 +495,45 @@ class SerializerTest < ActiveModel::TestCase embed :objects end - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)] post.comments = comments serializer = serializer.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :author => nil, - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + post: { + title: "New Post", + body: "Body of new post", + author: nil, + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ] } }, serializer.as_json) end def test_sets_can_be_serialized - 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) set = Set.new set << post1 set << post2 - serializer = set.active_model_serializer.new set, :each_serializer => CustomPostSerializer + serializer = set.active_model_serializer.new set, each_serializer: CustomPostSerializer as_json = serializer.as_json assert_equal 2, as_json.size - assert as_json.include?({ :title => "Post1" }) - assert as_json.include?({ :title => "Post2" }) + assert as_json.include?({ title: "Post1" }) + assert as_json.include?({ title: "Post2" }) end def test_associations_with_as posts = [ - Post.new(:title => 'First Post', :body => 'text'), - Post.new(:title => 'Second Post', :body => 'text') + Post.new(title: 'First Post', body: 'text'), + Post.new(title: 'Second Post', body: 'text') ] user = User.new @@ -541,18 +541,18 @@ class SerializerTest < ActiveModel::TestCase custom_blog.public_posts = posts custom_blog.public_user = user - serializer = CustomBlogSerializer.new(custom_blog, :scope => { :scope => true }) + serializer = CustomBlogSerializer.new(custom_blog, scope: { scope: true }) assert_equal({ - :custom_blog => { - :posts => [ - {:title => 'First Post', :body => 'text', :comments => []}, - {:title => 'Second Post', :body => 'text', :comments => []} + custom_blog: { + posts: [ + {title: 'First Post', body: 'text', comments: []}, + {title: 'Second Post', body: 'text', comments: []} ], - :user => { - :first_name => "Jose", - :last_name => "Valim", :ok => true, - :scope => true + user: { + first_name: "Jose", + last_name: "Valim", ok: true, + scope: true } } }, serializer.as_json) @@ -564,13 +564,13 @@ class SerializerTest < ActiveModel::TestCase const_set(:UserSerializer, UserSerializer) const_set(:PostSerializer, PostSerializer) - has_many :public_posts, :key => :posts - has_one :public_user, :key => :user + has_many :public_posts, key: :posts + has_one :public_user, key: :user end posts = [ - Post.new(:title => 'First Post', :body => 'text', :comments => []), - Post.new(:title => 'Second Post', :body => 'text', :comments => []) + Post.new(title: 'First Post', body: 'text', comments: []), + Post.new(title: 'Second Post', body: 'text', comments: []) ] user = User.new @@ -578,18 +578,18 @@ class SerializerTest < ActiveModel::TestCase custom_blog.public_posts = posts custom_blog.public_user = user - serializer = implicit_serializer.new(custom_blog, :scope => { :scope => true }) + serializer = implicit_serializer.new(custom_blog, scope: { scope: true }) assert_equal({ - :custom_blog => { - :posts => [ - {:title => 'First Post', :body => 'text', :comments => []}, - {:title => 'Second Post', :body => 'text', :comments => []} + custom_blog: { + posts: [ + {title: 'First Post', body: 'text', comments: []}, + {title: 'Second Post', body: 'text', comments: []} ], - :user => { - :first_name => "Jose", - :last_name => "Valim", :ok => true, - :scope => true + user: { + first_name: "Jose", + last_name: "Valim", ok: true, + scope: true } } }, serializer.as_json) @@ -599,18 +599,18 @@ class SerializerTest < ActiveModel::TestCase serializer_class = Class.new(ActiveModel::Serializer) do root :user - attribute :first_name, :key => :firstName - attribute :last_name, :key => :lastName + attribute :first_name, key: :firstName + attribute :last_name, key: :lastName attribute :password end serializer = serializer_class.new(User.new) assert_equal({ - :user => { - :firstName => "Jose", - :lastName => "Valim", - :password => "oh noes yugive my password" + user: { + firstName: "Jose", + lastName: "Valim", + password: "oh noes yugive my password" } }, serializer.as_json) end @@ -647,18 +647,18 @@ class SerializerTest < ActiveModel::TestCase def can_view; end def drafts; end - attributes :name, :age, {:can_edit => :boolean}, :can_view - has_many :posts, :serializer => Class.new - has_many :drafts, :serializer => Class.new - has_one :parent, :serializer => Class.new + attributes :name, :age, { can_edit: :boolean }, :can_view + has_many :posts, serializer: Class.new + has_many :drafts, serializer: Class.new + has_one :parent, serializer: Class.new end assert_equal serializer.schema, { - :attributes => { :name => :string, :age => :integer, :can_edit => :boolean, :can_view => nil }, - :associations => { - :posts => { :has_many => :posts }, - :drafts => nil, - :parent => { :belongs_to => :parent } + attributes: { name: :string, age: :integer, can_edit: :boolean, can_view: nil }, + associations: { + posts: { has_many: :posts }, + drafts: nil, + parent: { belongs_to: :parent } } } end @@ -672,15 +672,15 @@ class SerializerTest < ActiveModel::TestCase end attributes :name, :age - has_many :posts, :key => :my_posts, :serializer => Class.new - has_one :parent, :key => :my_parent, :serializer => Class.new + has_many :posts, key: :my_posts, serializer: Class.new + has_one :parent, key: :my_parent, serializer: Class.new end assert_equal serializer.schema, { - :attributes => { :name => :string, :age => :integer }, - :associations => { - :my_posts => { :has_many => :posts }, - :my_parent => { :belongs_to => :parent } + attributes: { name: :string, age: :integer }, + associations: { + my_posts: { has_many: :posts }, + my_parent: { belongs_to: :parent } } } end @@ -693,7 +693,7 @@ class SerializerTest < ActiveModel::TestCase root :post attributes :title, :body - has_one :author, :serializer => author_serializer + has_one :author, serializer: author_serializer end post_class = Class.new(Model) do @@ -702,17 +702,17 @@ class SerializerTest < ActiveModel::TestCase author_class = Class.new(Model) - post = post_class.new(:title => "New Post", :body => "It's a new post!") - author = author_class.new(:id => 5) + post = post_class.new(title: "New Post", body: "It's a new post!") + author = author_class.new(id: 5) post.author = author hash = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "It's a new post!", - :author_id => 5 + post: { + title: "New Post", + body: "It's a new post!", + author_id: 5 } }, hash.as_json) end @@ -729,7 +729,7 @@ class SerializerTest < ActiveModel::TestCase root :post attributes :title, :body - has_one :author, :serializer => author_serializer + has_one :author, serializer: author_serializer end post_class = Class.new(Model) do @@ -738,17 +738,17 @@ class SerializerTest < ActiveModel::TestCase author_class = Class.new(Model) - post = post_class.new(:title => "New Post", :body => "It's a new post!") - author = author_class.new(:id => 5) + post = post_class.new(title: "New Post", body: "It's a new post!") + author = author_class.new(id: 5) post.author = author hash = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "It's a new post!", - :author_id => "OMG" + post: { + title: "New Post", + body: "It's a new post!", + author_id: "OMG" } }, hash.as_json) end @@ -762,7 +762,7 @@ class SerializerTest < ActiveModel::TestCase root :post attributes :title, :body - has_one :author, :serializer => author_serializer + has_one :author, serializer: author_serializer end post_class = Class.new(Model) do @@ -771,17 +771,17 @@ class SerializerTest < ActiveModel::TestCase author_class = Class.new(Model) - post = post_class.new(:title => "New Post", :body => "It's a new post!") - author = author_class.new(:id => 5, :name => "Tom Dale") + post = post_class.new(title: "New Post", body: "It's a new post!") + author = author_class.new(id: 5, name: "Tom Dale") post.author = author hash = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "It's a new post!", - :author => { :id => 5, :name => "Tom Dale" } + post: { + title: "New Post", + body: "It's a new post!", + author: { id: 5, name: "Tom Dale" } } }, hash.as_json) end @@ -795,7 +795,7 @@ class SerializerTest < ActiveModel::TestCase root :post attributes :title, :body - has_one :author, :serializer => author_serializer + has_one :author, serializer: author_serializer end post_class = Class.new(Model) do @@ -804,37 +804,37 @@ class SerializerTest < ActiveModel::TestCase author_class = Class.new(Model) - post = post_class.new(:title => "New Post", :body => "It's a new post!") - author = author_class.new(:id => 5, :name => "Tom Dale") + post = post_class.new(title: "New Post", body: "It's a new post!") + author = author_class.new(id: 5, name: "Tom Dale") post.author = author assert_equal({ - :blog_post => { - :title => "New Post", - :body => "It's a new post!", - :author => { :id => 5, :name => "Tom Dale" } + blog_post: { + title: "New Post", + body: "It's a new post!", + author: { id: 5, name: "Tom Dale" } } - }, serializer_class.new(post, :root => :blog_post).as_json) + }, serializer_class.new(post, root: :blog_post).as_json) assert_equal({ - :title => "New Post", - :body => "It's a new post!", - :author => { :id => 5, :name => "Tom Dale" } - }, serializer_class.new(post, :root => false).as_json) + title: "New Post", + body: "It's a new post!", + author: { id: 5, name: "Tom Dale" } + }, serializer_class.new(post, root: false).as_json) assert_equal({ - :blog_post => { - :title => "New Post", - :body => "It's a new post!", - :author => { :id => 5, :name => "Tom Dale" } + blog_post: { + title: "New Post", + body: "It's a new post!", + author: { id: 5, name: "Tom Dale" } } - }, serializer_class.new(post).as_json(:root => :blog_post)) + }, serializer_class.new(post).as_json(root: :blog_post)) assert_equal({ - :title => "New Post", - :body => "It's a new post!", - :author => { :id => 5, :name => "Tom Dale" } - }, serializer_class.new(post).as_json(:root => false)) + title: "New Post", + body: "It's a new post!", + author: { id: 5, name: "Tom Dale" } + }, serializer_class.new(post).as_json(root: false)) end def test_serializer_has_access_to_root_object @@ -853,7 +853,7 @@ class SerializerTest < ActiveModel::TestCase root :post attributes :title, :body - has_one :author, :serializer => author_serializer + has_one :author, serializer: author_serializer end post_class = Class.new(Model) do @@ -862,8 +862,8 @@ class SerializerTest < ActiveModel::TestCase author_class = Class.new(Model) - post = post_class.new(:title => "New Post", :body => "It's a new post!") - author = author_class.new(:id => 5, :name => "Tom Dale") + post = post_class.new(title: "New Post", body: "It's a new post!") + author = author_class.new(id: 5, name: "Tom Dale") post.author = author expected = serializer_class.new(post).as_json @@ -875,47 +875,47 @@ class SerializerTest < ActiveModel::TestCase serializer_class.class_eval do root :post - embed :ids, :include => true - has_many :comments, :key => :comment_ids, :root => :comments - has_one :author, :serializer => DefaultUserSerializer, :key => :author_id, :root => :author + embed :ids, include: true + has_many :comments, key: :comment_ids, root: :comments + has_one :author, serializer: DefaultUserSerializer, key: :author_id, root: :author end - post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com") - comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] + post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com") + comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)] post.comments = comments serializer = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comment_ids => [1, 2], - :author_id => nil + post: { + title: "New Post", + body: "Body of new post", + comment_ids: [1, 2], + author_id: nil }, - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ], - :author => [] + author: [] }, serializer.as_json) - post.author = User.new(:id => 1) + post.author = User.new(id: 1) serializer = serializer_class.new(post) assert_equal({ - :post => { - :title => "New Post", - :body => "Body of new post", - :comment_ids => [1, 2], - :author_id => 1 + post: { + title: "New Post", + body: "Body of new post", + comment_ids: [1, 2], + author_id: 1 }, - :comments => [ - { :title => "Comment1" }, - { :title => "Comment2" } + comments: [ + { title: "Comment1" }, + { title: "Comment2" } ], - :author => [{ :first_name => "Jose", :last_name => "Valim" }] + author: [{ first_name: "Jose", last_name: "Valim" }] }, serializer.as_json) end @@ -927,15 +927,15 @@ class SerializerTest < ActiveModel::TestCase end comment_serializer = Class.new(ActiveModel::Serializer) do - embed :ids, :include => true + embed :ids, include: true attributes :id, :body - has_many :tags, :serializer => tag_serializer + has_many :tags, serializer: tag_serializer end post_serializer = Class.new(ActiveModel::Serializer) do - embed :ids, :include => true + embed :ids, include: true attributes :id, :title, :body - has_many :comments, :serializer => comment_serializer + has_many :comments, serializer: comment_serializer end post_class = Class.new(Model) do @@ -952,32 +952,32 @@ class SerializerTest < ActiveModel::TestCase tag_class = Class.new(Model) - post = post_class.new(:title => "New Post", :body => "NEW POST", :id => 1) - comment1 = comment_class.new(:body => "EWOT", :id => 1) - comment2 = comment_class.new(:body => "YARLY", :id => 2) - tag1 = tag_class.new(:name => "lolcat", :id => 1) - tag2 = tag_class.new(:name => "nyancat", :id => 2) - tag3 = tag_class.new(:name => "violetcat", :id => 3) + post = post_class.new(title: "New Post", body: "NEW POST", id: 1) + comment1 = comment_class.new(body: "EWOT", id: 1) + comment2 = comment_class.new(body: "YARLY", id: 2) + tag1 = tag_class.new(name: "lolcat", id: 1) + tag2 = tag_class.new(name: "nyancat", id: 2) + tag3 = tag_class.new(name: "violetcat", id: 3) post.comments = [comment1, comment2] comment1.tags = [tag1, tag3] comment2.tags = [tag1, tag2] - actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json + actual = ActiveModel::ArraySerializer.new([post], root: :posts).as_json assert_equal({ - :posts => [ - { :title => "New Post", :body => "NEW POST", :id => 1, :comment_ids => [1,2] } + posts: [ + { title: "New Post", body: "NEW POST", id: 1, comment_ids: [1,2] } ], - :comments => [ - { :body => "EWOT", :id => 1, :tag_ids => [1,3] }, - { :body => "YARLY", :id => 2, :tag_ids => [1,2] } + comments: [ + { body: "EWOT", id: 1, tag_ids: [1,3] }, + { body: "YARLY", id: 2, tag_ids: [1,2] } ], - :tags => [ - { :name => "lolcat", :id => 1 }, - { :name => "violetcat", :id => 3 }, - { :name => "nyancat", :id => 2 } + tags: [ + { name: "lolcat", id: 1 }, + { name: "violetcat", id: 3 }, + { name: "nyancat", id: 2 } ] }, actual) end @@ -993,7 +993,7 @@ class SerializerTest < ActiveModel::TestCase klass = Class.new do def read_attribute_for_serialization(name) - { :title => "New post!", :body => "First post body" }[name] + { title: "New post!", body: "First post body" }[name] end def title @@ -1007,12 +1007,12 @@ class SerializerTest < ActiveModel::TestCase object = klass.new - actual = serializer.new(object, :root => :post).as_json + actual = serializer.new(object, root: :post).as_json assert_equal({ - :post => { - :title => "NEW POST!", - :body => "First post body" + post: { + title: "NEW POST!", + body: "First post body" } }, actual) end @@ -1022,16 +1022,16 @@ class SerializerTest < ActiveModel::TestCase attributes :title, :body def read_attribute_for_serialization(name) - { :title => "New post!", :body => "First post body" }[name] + { title: "New post!", body: "First post body" }[name] end end - actual = serializer.new(Object.new, :root => :post).as_json + actual = serializer.new(Object.new, root: :post).as_json assert_equal({ - :post => { - :title => "New post!", - :body => "First post body" + post: { + title: "New post!", + body: "First post body" } }, actual) end @@ -1062,7 +1062,7 @@ class SerializerTest < ActiveModel::TestCase actual = serializer.new(todo.new).as_json assert_equal({ - :overdue => true + overdue: true }, actual) end @@ -1078,13 +1078,13 @@ class SerializerTest < ActiveModel::TestCase end serializer = Class.new(ActiveModel::Serializer) do - attribute :overdue?, :key => :foo + attribute :overdue?, key: :foo end actual = serializer.new(todo.new).as_json assert_equal({ - :foo => true + foo: true }, actual) end @@ -1105,21 +1105,21 @@ class SerializerTest < ActiveModel::TestCase attachment_serializer = Class.new(ActiveModel::Serializer) do attributes :name, :url - has_one :attachable, :polymorphic => true + has_one :attachable, polymorphic: true end - email = email_class.new :subject => 'foo', :body => 'bar' + email = email_class.new subject: 'foo', body: 'bar' - attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email + attachment = Attachment.new name: 'logo.png', url: 'http://example.com/logo.png', attachable: email actual = attachment_serializer.new(attachment, {}).as_json assert_equal({ - :name => 'logo.png', - :url => 'http://example.com/logo.png', - :attachable => { - :type => :email, - :email => { :subject => 'foo', :body => 'bar' } + name: 'logo.png', + url: 'http://example.com/logo.png', + attachable: { + type: :email, + email: { subject: 'foo', body: 'bar' } } }, actual) end @@ -1142,21 +1142,21 @@ class SerializerTest < ActiveModel::TestCase attachment_serializer = Class.new(ActiveModel::Serializer) do embed :ids attributes :name, :url - has_one :attachable, :polymorphic => true + has_one :attachable, polymorphic: true end - email = email_class.new :id => 1 + email = email_class.new id: 1 - attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email + attachment = Attachment.new name: 'logo.png', url: 'http://example.com/logo.png', attachable: email actual = attachment_serializer.new(attachment, {}).as_json assert_equal({ - :name => 'logo.png', - :url => 'http://example.com/logo.png', - :attachable => { - :type => :email, - :id => 1 + name: 'logo.png', + url: 'http://example.com/logo.png', + attachable: { + type: :email, + id: 1 } }, actual) end @@ -1178,29 +1178,29 @@ class SerializerTest < ActiveModel::TestCase attachment_serializer = Class.new(ActiveModel::Serializer) do root :attachment - embed :ids, :include => true + embed :ids, include: true attributes :name, :url - has_one :attachable, :polymorphic => true + has_one :attachable, polymorphic: true end - email = email_class.new :id => 1, :subject => "Hello", :body => "World" + email = email_class.new id: 1, subject: "Hello", body: "World" - attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email + attachment = Attachment.new name: 'logo.png', url: 'http://example.com/logo.png', attachable: email actual = attachment_serializer.new(attachment, {}).as_json assert_equal({ - :attachment => { - :name => 'logo.png', - :url => 'http://example.com/logo.png', - :attachable => { - :type => :email, - :id => 1 + attachment: { + name: 'logo.png', + url: 'http://example.com/logo.png', + attachable: { + type: :email, + id: 1 }}, - :emails => [{ - :id => 1, - :subject => "Hello", - :body => "World" + emails: [{ + id: 1, + subject: "Hello", + body: "World" }] }, actual) end @@ -1211,10 +1211,10 @@ class SerializerTest < ActiveModel::TestCase end orange_serializer = Class.new(ActiveModel::Serializer) do - embed :ids, :include => true + embed :ids, include: true attributes :plu, :id - has_one :readable, :polymorphic => true + has_one :readable, polymorphic: true end email_class = Class.new(Model) do @@ -1243,47 +1243,47 @@ class SerializerTest < ActiveModel::TestCase attachment_serializer = Class.new(ActiveModel::Serializer) do root :attachment - embed :ids, :include => true + embed :ids, include: true attributes :name, :url - has_one :attachable, :polymorphic => true - has_one :readable, :polymorphic => true - has_one :edible, :polymorphic => true + has_one :attachable, polymorphic: true + has_one :readable, polymorphic: true + has_one :edible, polymorphic: true end - email = email_class.new :id => 1, :subject => "Hello", :body => "World" - orange = orange_class.new :id => 1, :plu => "3027", :readable => email + email = email_class.new id: 1, subject: "Hello", body: "World" + orange = orange_class.new id: 1, plu: "3027", readable: email attachment = Attachment.new({ - :name => 'logo.png', - :url => 'http://example.com/logo.png', - :attachable => email, - :readable => email, - :edible => orange + name: 'logo.png', + url: 'http://example.com/logo.png', + attachable: email, + readable: email, + edible: orange }) actual = attachment_serializer.new(attachment, {}).as_json assert_equal({ - :emails => [{ - :subject => "Hello", - :body => "World", - :id => 1 + emails: [{ + subject: "Hello", + body: "World", + id: 1 }], - :oranges => [{ - :plu => "3027", - :id => 1, - :readable => { :type => :email, :id => 1 } + oranges: [{ + plu: "3027", + id: 1, + readable: { type: :email, id: 1 } }], - :attachment => { - :name => 'logo.png', - :url => 'http://example.com/logo.png', - :attachable => { :type => :email, :id => 1 }, - :readable => { :type => :email, :id => 1 }, - :edible => { :type => :orange, :id => 1 } + attachment: { + name: 'logo.png', + url: 'http://example.com/logo.png', + attachable: { type: :email, id: 1 }, + readable: { type: :email, id: 1 }, + edible: { type: :orange, id: 1 } } }, actual) end @@ -1294,8 +1294,8 @@ class SerializerTest < ActiveModel::TestCase end fruit_serializer = Class.new(ActiveModel::Serializer) do - embed :ids, :include => true - has_one :attachment, :serializer => attachment_serializer + embed :ids, include: true + has_one :attachment, serializer: attachment_serializer attribute :color end @@ -1337,24 +1337,24 @@ class SerializerTest < ActiveModel::TestCase smoothie_serializer = Class.new(ActiveModel::Serializer) do root false - embed :ids, :include => true + embed :ids, include: true - has_one :base, :polymorphic => true - has_one :flavor, :polymorphic => true + has_one :base, polymorphic: true + has_one :flavor, polymorphic: true end banana_attachment = Attachment.new({ - :name => 'banana_blending.md', - :id => 3, + name: 'banana_blending.md', + id: 3, }) strawberry_attachment = Attachment.new({ - :name => 'strawberry_cleaning.doc', - :id => 4 + name: 'strawberry_cleaning.doc', + id: 4 }) - banana = banana_class.new :color => "yellow", :id => 1, :attachment => banana_attachment - strawberry = strawberry_class.new :color => "red", :id => 2, :attachment => strawberry_attachment + banana = banana_class.new color: "yellow", id: 1, attachment: banana_attachment + strawberry = strawberry_class.new color: "red", id: 2, attachment: strawberry_attachment smoothie = smoothie_serializer.new(smoothie.new(banana, strawberry)) @@ -1366,19 +1366,19 @@ class SerializerTest < ActiveModel::TestCase def tests_includes_does_not_include_nil_polymoprhic_associations post_serializer = Class.new(ActiveModel::Serializer) do root :post - embed :ids, :include => true - has_one :author, :polymorphic => true + embed :ids, include: true + has_one :author, polymorphic: true attributes :title end - post = Post.new(:title => 'Foo') + post = Post.new(title: 'Foo') actual = post_serializer.new(post).as_json assert_equal({ - :post => { - :title => 'Foo', - :author => nil + post: { + title: 'Foo', + author: nil } }, actual) end @@ -1401,30 +1401,30 @@ class SerializerTest < ActiveModel::TestCase serializable_array = Class.new(Array) array = serializable_array.new - array << tag_class.new(:name => 'Rails') - array << tag_class.new(:name => 'Sinatra') + array << tag_class.new(name: 'Rails') + array << tag_class.new(name: 'Sinatra') - actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}).as_json + actual = array.active_model_serializer.new(array, root: :tags, meta: {total: 10}).as_json assert_equal({ - :meta => { - :total => 10, + meta: { + total: 10, }, - :tags => [ - { :name => "Rails" }, - { :name => "Sinatra" }, + tags: [ + { name: "Rails" }, + { name: "Sinatra" }, ] }, actual) - actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}, :meta_key => 'meta_object').as_json + actual = array.active_model_serializer.new(array, root: :tags, meta: {total: 10}, meta_key: 'meta_object').as_json assert_equal({ - :meta_object => { - :total => 10, + meta_object: { + total: 10, }, - :tags => [ - { :name => "Rails" }, - { :name => "Sinatra" }, + tags: [ + { name: "Rails" }, + { name: "Sinatra" }, ] }, actual) end @@ -1447,9 +1447,9 @@ class SerializerTest < ActiveModel::TestCase item.body = "body" 2.times do - assert_equal({:title => "title"}, + assert_equal({title: "title"}, parent.new(item).attributes) - assert_equal({:body => "body", :title => "title"}, + assert_equal({body: "body", title: "title"}, child.new(item).attributes) end @@ -1464,36 +1464,36 @@ class SerializerTest < ActiveModel::TestCase user = User.new user.superuser = true - post = Post.new(:title => 'Foo') + post = Post.new(title: 'Foo') - a_serializer = serializer.new(post, :scope => user, :scope_name => :current_user) + a_serializer = serializer.new(post, scope: user, scope_name: :current_user) assert a_serializer.has_permission? end def test_only_option_filters_attributes_and_associations - post = Post.new(:title => "New Post", :body => "Body of new post") - comments = [Comment.new(:title => "Comment1")] + post = Post.new(title: "New Post", body: "Body of new post") + comments = [Comment.new(title: "Comment1")] post.comments = comments - post_serializer = PostSerializer.new(post, :only => :title) + post_serializer = PostSerializer.new(post, only: :title) assert_equal({ - :post => { - :title => "New Post" + post: { + title: "New Post" } }, post_serializer.as_json) end def test_except_option_filters_attributes_and_associations - post = Post.new(:title => "New Post", :body => "Body of new post") - comments = [Comment.new(:title => "Comment1")] + post = Post.new(title: "New Post", body: "Body of new post") + comments = [Comment.new(title: "Comment1")] post.comments = comments - post_serializer = PostSerializer.new(post, :except => [:body, :comments]) + post_serializer = PostSerializer.new(post, except: [:body, :comments]) assert_equal({ - :post => { - :title => "New Post" + post: { + title: "New Post" } }, post_serializer.as_json) end @@ -1501,11 +1501,11 @@ class SerializerTest < ActiveModel::TestCase def test_only_option_takes_precedence_over_custom_defined_include_methods user = User.new - post = Post.new(:title => "New Post", :body => "Body of new post", :author => "Sausage King") - comments = [Comment.new(:title => "Comment")] + post = Post.new(title: "New Post", body: "Body of new post", author: "Sausage King") + comments = [Comment.new(title: "Comment")] post.comments = comments - post_serializer = PostWithMultipleConditionalsSerializer.new(post, :scope => user, :only => :title) + post_serializer = PostWithMultipleConditionalsSerializer.new(post, scope: user, only: :title) # comments enabled post.comments_disabled = false @@ -1513,8 +1513,8 @@ class SerializerTest < ActiveModel::TestCase user.superuser = true assert_equal({ - :post => { - :title => "New Post" + post: { + title: "New Post" } }, post_serializer.as_json) end diff --git a/test/test_fakes.rb b/test/test_fakes.rb index 30ce34be..a0a244c1 100644 --- a/test/test_fakes.rb +++ b/test/test_fakes.rb @@ -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