diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1551b2cf7..cf0203dc7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,29 +14,26 @@ jobs: fail-fast: false matrix: include: - # Recent Rubies and Rails - # NOTE(BF): Rails >= 5.0 has test failures which need to be fixed - # - ruby-version: '2.6' - # rails-version: '6.1' - # - ruby-version: '2.7' - # rails-version: '6.0' - # - ruby-version: '2.6' - # rails-version: '5.2' - # - ruby-version: '2.5' - # rails-version: '5.1' - # bundler: '1' - # - ruby-version: '2.4' - # rails-version: '5.0' - # bundler: '1' - - ruby-version: '2.4' - rails-version: '4.2' - bundler: '1' - - ruby-version: '2.4' - rails-version: '4.1' - bundler: '1' - - ruby-version: '2.4' - rails-version: '4.0' - bundler: '1' + - ruby-version: '2.7' + rails-version: '5.0' + - ruby-version: '2.7' + rails-version: '5.1' + - ruby-version: '2.7' + rails-version: '5.2' + - ruby-version: '3.0' + rails-version: '6.0' + - ruby-version: '3.1' + rails-version: '6.1' + - ruby-version: '3.2' + rails-version: '7.0' + - ruby-version: '3.3' + rails-version: '7.1' + - ruby-version: '3.3' + rails-version: '7.2' + - ruby-version: '3.4' + rails-version: '8.0' + - ruby-version: '4.0' + rails-version: '8.1' continue-on-error: "${{ endsWith(matrix.ruby-version, 'head') }}" @@ -44,17 +41,16 @@ jobs: CI: "1" steps: - - name: "Checkout Code" - uses: "actions/checkout@v2" - timeout-minutes: 5 + - name: "Checkout Code" + uses: "actions/checkout@v2" + timeout-minutes: 5 with: - fetch-depth: 0 + fetch-depth: 0 - name: "Build Ruby" uses: ruby/setup-ruby@v1 with: - ruby-version: "${{ matrix.ruby-version }}" - bundler: "${{ matrix.bundler || 2 }}" + ruby-version: "${{ matrix.ruby-version }}" bundler-cache: true env: RAILS_VERSION: ${{ matrix.rails-version }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a391e3ddf..000000000 --- a/.travis.yml +++ /dev/null @@ -1,35 +0,0 @@ -language: ruby -sudo: false -rvm: - # - 1.8.7 - # - ree - # - jruby-18mode - # - 1.9.2 - # - 1.9.3 - - jruby-19mode - - 2.0.0 - - 2.1 - - 2.2 -install: bundle install --path=vendor/bundle --retry=3 --jobs=3 -cache: - directories: - - vendor/bundle -script: - - bundle exec rake ci -env: - # - "RAILS_VERSION=3.0" - # - "RAILS_VERSION=3.1" - # - "RAILS_VERSION=3.2" - - "RAILS_VERSION=4.0" - - "RAILS_VERSION=4.1" - - "RAILS_VERSION=4.2" - # - "RAILS_VERSION=master" -matrix: - exclude: - - rvm: jruby-19mode - env: "RAILS_VERSION=4.0" - # - rvm: 1.8.7 - # - ree - # - jruby-18mode - # - rvm: 1.9.2 - fast_finish: true diff --git a/Gemfile b/Gemfile index 384d7c261..cefff2346 100644 --- a/Gemfile +++ b/Gemfile @@ -1,40 +1,15 @@ source 'https://rubygems.org' -version = ENV['RAILS_VERSION'] || '4.1' - +version = ENV['RAILS_VERSION'] if version == 'master' - gem 'rack', github: 'rack/rack' - gem 'arel', github: 'rails/arel' gem 'rails', github: 'rails/rails' - git 'https://github.com/rails/rails.git' do - gem 'railties' - gem 'activesupport' - gem 'activemodel' - gem 'actionpack' - gem 'activerecord', group: :test - # Rails 5 - gem 'actionview' - end -else +elsif version gem_version = "~> #{version}.0" gem 'rails', gem_version - gem 'railties', gem_version - gem 'activesupport', gem_version - gem 'activemodel', gem_version - gem 'actionpack', gem_version - gem 'activerecord', gem_version, group: :test +else + gem 'rails' end -# Specify gem dependencies in active_model_serializers.gemspec gemspec -# https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54 -@windows_platforms = [:mswin, :mswin64, :mingw, :x64_mingw] - -# Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby]) - -# JRuby versions before 9.x report their version as "1.9.x" or lower, so lock these to an older version of mime-types -if defined?(JRUBY_VERSION) and Gem::ruby_version < Gem::Version.new("2.0.0") - gem 'mime-types', '< 3' -end +gem 'tzinfo-data', platforms: %i[mswin mswin64 mingw x64_mingw jruby] diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index c5724df99..c4c87d423 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -16,9 +16,10 @@ Gem::Specification.new do |gem| gem.name = "active_model_serializers" gem.require_paths = ["lib"] gem.version = ActiveModel::Serializer::VERSION + gem.required_ruby_version = '>= 2.7' - gem.add_dependency 'activemodel', '>= 3.0' - gem.add_development_dependency "rails", ">= 3.0" + gem.add_dependency 'activemodel', '>= 6.0' + gem.add_development_dependency "rails", ">= 6.0" gem.add_development_dependency "pry" gem.add_development_dependency "minitest" end diff --git a/bench/perf.rb b/bench/perf.rb index ea668d56e..836a0785a 100644 --- a/bench/perf.rb +++ b/bench/perf.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rubygems" require "bundler/setup" require "active_model_serializers" diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb index 4bcad5667..add6aa396 100644 --- a/lib/action_controller/serialization.rb +++ b/lib/action_controller/serialization.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ActionController # Action Controller Serialization # diff --git a/lib/active_model/array_serializer.rb b/lib/active_model/array_serializer.rb index fc3159ad7..1fbe488b8 100644 --- a/lib/active_model/array_serializer.rb +++ b/lib/active_model/array_serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "active_support/core_ext/class/attribute" require 'active_support/dependencies' require 'active_support/descendants_tracker' diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 694cf61f7..0cbc7409a 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "active_support/core_ext/class/attribute" require "active_support/core_ext/module/anonymous" require 'active_support/dependencies' @@ -455,7 +457,7 @@ def merge_association(hash, key, serializables, unique_values) def attributes _fast_attributes rescue NameError - method = "def _fast_attributes\n" + method = +"def _fast_attributes\n" method << " h = {}\n" diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 8606b930e..5ec65c511 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ActiveModel class Serializer module Associations #:nodoc: diff --git a/lib/active_model/serializers/version.rb b/lib/active_model/serializers/version.rb index 96a2681b3..08a46c449 100644 --- a/lib/active_model/serializers/version.rb +++ b/lib/active_model/serializers/version.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module ActiveModel class Serializer - VERSION = "0.8.4" + VERSION = "0.8.5" end end diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index 26a3353e7..499ff04c7 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "active_support" require "active_support/core_ext/string/inflections" require "active_support/notifications" diff --git a/lib/active_record/serializer_override.rb b/lib/active_record/serializer_override.rb index b6149b83a..1df04c046 100644 --- a/lib/active_record/serializer_override.rb +++ b/lib/active_record/serializer_override.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # We do not recommend that you use AM::S in this way, but if you must, here # is a mixin that overrides ActiveRecord::Base#to_json and #as_json. diff --git a/lib/generators/resource_override.rb b/lib/generators/resource_override.rb index 1b48a12e7..dd8867fea 100644 --- a/lib/generators/resource_override.rb +++ b/lib/generators/resource_override.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rails/generators" require "rails/generators/rails/resource/resource_generator" diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb index b7096f835..574f8191d 100644 --- a/lib/generators/serializer/serializer_generator.rb +++ b/lib/generators/serializer/serializer_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rails module Generators class SerializerGenerator < NamedBase diff --git a/lib/generators/serializer/templates/serializer.rb b/lib/generators/serializer/templates/serializer.rb index 6c25609d4..61bc9ada4 100644 --- a/lib/generators/serializer/templates/serializer.rb +++ b/lib/generators/serializer/templates/serializer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + <% module_namespacing do -%> class <%= class_name %>Serializer < <%= parent_class_name %> attributes <%= attributes_names.map(&:inspect).join(", ") %> diff --git a/test/array_serializer_test.rb b/test/array_serializer_test.rb index fd9ffbb5f..240aff2f0 100644 --- a/test/array_serializer_test.rb +++ b/test/array_serializer_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "test_helper" require "test_fakes" @@ -17,6 +19,7 @@ def test_array_items_do_not_have_root each_serializer = array.active_model_serializer.new(array, :root => "root", :each_serializer => BasicSerializer) default_json = default_serializer.as_json + default_json['root'].map!(&:symbolize_keys) each_json = each_serializer.as_json assert_equal(expected, default_json) diff --git a/test/association_test.rb b/test/association_test.rb index c4eb121af..5d1414154 100644 --- a/test/association_test.rb +++ b/test/association_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "test_helper" class AssociationTest < ActiveSupport::TestCase @@ -367,11 +369,9 @@ def test_embed_ids_include_true_does_not_serialize_multiple_times # Count how often the @comment record is serialized. serialized_times = 0 - @comment.class_eval do - define_method :read_attribute_for_serialization, lambda { |name| - serialized_times += 1 if name == :body - super(name) - } + @comment.define_singleton_method(:read_attribute_for_serialization) do |name| + serialized_times += 1 if name == :body + super(name) end include_bare! :comment @@ -386,14 +386,12 @@ def test_include_with_read_association_id_for_serialization_hook end association_name = nil - @post.class_eval do - define_method :read_attribute_for_serialization, lambda { |name| - association_name = name - send(name) - } - define_method :comment_id, lambda { - @attributes[:comment].id - } + @post.define_singleton_method(:read_attribute_for_serialization) do |name| + association_name = name + send(name) + end + @post.define_singleton_method(:comment_id) do + @attributes[:comment].id end include_bare! :comment @@ -409,14 +407,12 @@ def test_include_with_read_association_ids_for_serialization_hook end association_name = nil - @post.class_eval do - define_method :read_attribute_for_serialization, lambda { |name| - association_name = name - send(name) - } - define_method :comment_ids, lambda { - @attributes[:comments].map(&:id) - } + @post.define_singleton_method(:read_attribute_for_serialization) do |name| + association_name = name + send(name) + end + @post.define_singleton_method(:comment_ids) do + @attributes[:comments].map(&:id) end include_bare! :comments @@ -478,7 +474,7 @@ def test_mutual_relation_result end def test_mutual_relation_does_not_raise_error - assert_nothing_raised SystemStackError, 'stack level too deep' do + assert_nothing_raised do # no SystemStackError @serializer.as_json end end diff --git a/test/caching_test.rb b/test/caching_test.rb index d66f2e34e..b8f582ad8 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "test_helper" class CachingTest < ActiveSupport::TestCase diff --git a/test/generators_test.rb b/test/generators_test.rb index b1938cee8..4a3bda54b 100644 --- a/test/generators_test.rb +++ b/test/generators_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class Foo < Rails::Application diff --git a/test/no_serialization_scope_test.rb b/test/no_serialization_scope_test.rb index 719ce4bfa..65b6601e6 100644 --- a/test/no_serialization_scope_test.rb +++ b/test/no_serialization_scope_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "test_helper" class NoSerializationScopeTest < ActionController::TestCase diff --git a/test/serialization_scope_name_test.rb b/test/serialization_scope_name_test.rb index e419c6310..011540936 100644 --- a/test/serialization_scope_name_test.rb +++ b/test/serialization_scope_name_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' require 'pathname' diff --git a/test/serialization_test.rb b/test/serialization_test.rb index 1465585df..d46cf6494 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' require 'pathname' @@ -96,7 +98,7 @@ def render_json_nil end def render_json_render_to_string - render :text => render_to_string(:json => '[]') + render :plain => render_to_string(:json => '[]') end def render_json_hello_world @@ -133,28 +135,28 @@ def render_json_without_options end def render_json_with_serializer - @current_user = Struct.new(:as_json).new(:current_user => true) + @current_user = Struct.new(:as_json).new({:current_user => true}) render :json => JsonSerializable.new end def render_json_with_serializer_and_implicit_root - @current_user = Struct.new(:as_json).new(:current_user => true) + @current_user = Struct.new(:as_json).new({:current_user => true}) render :json => [JsonSerializable.new] end def render_json_with_serializer_and_options - @current_user = Struct.new(:as_json).new(:current_user => true) + @current_user = Struct.new(:as_json).new({:current_user => true}) render :json => JsonSerializable.new, :options => true 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) + @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) + @current_user = Struct.new(:as_json).new({:current_user => true}) render :json => JsonSerializable.new(true) end @@ -215,7 +217,7 @@ def setup def test_render_json_nil get :render_json_nil assert_equal 'null', @response.body - assert_equal 'application/json', @response.content_type + assert_match %r{application/json}, @response.content_type end def test_render_json_render_to_string @@ -231,7 +233,7 @@ def test_render_json_nil_with_custom_serializer def test_render_json get :render_json_hello_world assert_equal '{"hello":"world"}', @response.body - assert_equal 'application/json', @response.content_type + assert_match %r{application/json}, @response.content_type end def test_render_json_with_status @@ -244,29 +246,29 @@ def test_render_json_with_callback get :render_json_hello_world_with_callback if Rails::VERSION::MAJOR == 3 assert_equal 'alert({"hello":"world"})', @response.body - assert_match %r(application/json), @response.content_type.to_s + assert_match %r{application/json}, @response.content_type.to_s else assert_equal '/**/alert({"hello":"world"})', @response.body - assert_match %r(text/javascript), @response.content_type.to_s + assert_match %r{text/javascript}, @response.content_type.to_s end end def test_render_json_with_custom_content_type get :render_json_with_custom_content_type assert_equal '{"hello":"world"}', @response.body - assert_equal 'text/javascript', @response.content_type + assert_match %r{text/javascript}, @response.content_type end def test_render_symbol_json get :render_symbol_json assert_equal '{"hello":"world"}', @response.body - assert_equal 'application/json', @response.content_type + assert_match %r{application/json}, @response.content_type end def test_render_json_forwards_extra_options get :render_json_with_extra_options assert_equal '{"a":"b"}', @response.body - assert_equal 'application/json', @response.content_type + assert_match %r{application/json}, @response.content_type end def test_render_json_calls_to_json_from_object @@ -280,33 +282,11 @@ def test_render_json_with_serializer assert_match '"object":{"serializable_object":true}', @response.body end - def test_render_json_with_serializer_checking_defaults - 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 - 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 - assert_match '"scope":"current_admin"', @response.body - end - def test_render_json_with_serializer_and_implicit_root get :render_json_with_serializer_and_implicit_root assert_match '"test":[{"serializable_object":true}]', @response.body 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 - assert_match '"test":[{"rails":"rocks"}]', @response.body - end - def test_render_json_with_serializer_and_options get :render_json_with_serializer_and_options assert_match '"scope":{"current_user":true}', @response.body @@ -319,11 +299,6 @@ def test_render_json_with_serializer_and_scope_option assert_match '"scope":{"current_user":false}', @response.body 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 - assert_match '"scope":{"current_user":false}', @response.body - end - def test_render_json_with_serializer_api_but_without_serializer get :render_json_with_serializer_api_but_without_serializer assert_match '{"serializable_object":true}', @response.body @@ -334,11 +309,6 @@ def test_render_json_with_custom_serializer assert_match '{"hello":true}', @response.body end - def test_render_json_with_custom_serializer_checking_default_serailizer - get :render_json_with_custom_serializer, :check_default_serializer => true - assert_match '{"hello":true}', @response.body - end - def test_render_json_array_with_custom_serializer get :render_json_array_with_custom_serializer assert_match '{"test":[{"hello":true}]}', @response.body @@ -350,11 +320,6 @@ def test_render_json_array_with_wrong_option end 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 - assert_match '{"test":[{"hello":true}]}', @response.body - end - def test_render_json_with_links get :render_json_with_links assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body @@ -365,21 +330,11 @@ def test_render_json_array_with_no_root assert_equal '[]', @response.body end - def test_render_json_array_with_no_root_checking_default_root - get :render_json_array_with_no_root, :check_default_root => true - assert_equal '[]', @response.body - end - def test_render_json_empty_array get :render_json_empty_array assert_equal '{"test":[]}', @response.body end - def test_render_json_empty_array_checking_default_root - get :render_json_empty_array, :check_default_root => true - assert_equal '{"awesome":[]}', @response.body - end - def test_render_json_empty_array_with_array_serializer_root_false ActiveModel::ArraySerializer.root = false get :render_json_empty_array diff --git a/test/serializer_support_test.rb b/test/serializer_support_test.rb index f90fa45f2..d7ebcdc0c 100644 --- a/test/serializer_support_test.rb +++ b/test/serializer_support_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "test_helper" class RandomModel @@ -27,7 +29,7 @@ class Criteria class SerializerSupportTest < ActiveSupport::TestCase test "it returns nil if no serializer exists" do - assert_equal nil, RandomModel.new.active_model_serializer + assert_nil RandomModel.new.active_model_serializer end test "it returns a deducted serializer if it exists exists" do diff --git a/test/serializer_test.rb b/test/serializer_test.rb index d8695942b..55619427d 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "test_helper" require "test_fakes" @@ -295,7 +297,7 @@ def test_nil_root_object root false end - assert_equal(nil, serializer.new(blog, :scope => user).as_json) + assert_nil(serializer.new(blog, :scope => user).as_json) end def test_custom_root_with_nil_root_object @@ -442,10 +444,8 @@ def comments comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)] post.comments = comments - post.class_eval do - define_method :comment_ids, lambda { - self.comments.map { |c| c.read_attribute_for_serialization(:id) } - } + post.define_singleton_method(:comment_ids) do + self.comments.map { |c| c.read_attribute_for_serialization(:id) } end json = post_serializer.new(post).as_json assert_equal({ diff --git a/test/support/rails5_shims.rb b/test/support/rails5_shims.rb index 03a036da6..55de76e96 100644 --- a/test/support/rails5_shims.rb +++ b/test/support/rails5_shims.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rails5Shims module ControllerTests # https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb diff --git a/test/test_fakes.rb b/test/test_fakes.rb index 366790e02..077dffdc2 100644 --- a/test/test_fakes.rb +++ b/test/test_fakes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class BasicActiveModel include ActiveModel::Serializers::JSON def initialize(hash = {}) diff --git a/test/test_helper.rb b/test/test_helper.rb index 061f64fae..3790ab431 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rubygems" require "bundler/setup"