diff --git a/.travis.yml b/.travis.yml index e39bb3e99..bfc8edfd0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,8 @@ sudo: false rvm: - 1.9.3 - 2.0.0 - - 2.2 + - 2.1 + - 2.2.2 - ruby-head - rbx-2 @@ -24,12 +25,18 @@ env: - "RAILS_VERSION=master" matrix: - include: + exclude: + - rvm: 1.9.3 + env: RAILS_VERSION=master + - rvm: 2.0.0 + env: RAILS_VERSION=master - rvm: 2.1 + env: RAILS_VERSION=master + include: + - rvm: 2.2 env: CAPTURE_STDERR=true - rvm: jruby-19mode env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false -Xcli.debug=true --debug' allow_failures: - rvm: ruby-head - - env: "RAILS_VERSION=master" fast_finish: true diff --git a/Gemfile b/Gemfile index 1d96ba05e..9a386356f 100644 --- a/Gemfile +++ b/Gemfile @@ -11,22 +11,23 @@ version = ENV['RAILS_VERSION'] || '4.2' if version == 'master' gem 'rack', github: 'rack/rack' + gem 'arel', github: 'rails/arel' 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 - # Rails 5 - gem 'rails-controller-testing', github: 'rails/rails-controller-testing' else gem_version = "~> #{version}.0" gem 'railties', gem_version gem 'activesupport', gem_version gem 'activemodel', gem_version gem 'actionpack', gem_version + gem 'activerecord', gem_version, group: :test end # https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54 @@ -36,11 +37,9 @@ end gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby]) group :test do - gem 'activerecord' gem 'sqlite3', platform: (@windows_platforms + [:ruby]) gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby - gem 'minitest-reporters', require: false, group: :development gem 'codeclimate-test-reporter', require: false gem 'simplecov', '~> 0.10', require: false, group: :development end diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index 1e438e2cb..3ccd9654e 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -41,10 +41,16 @@ Gem::Specification.new do |spec| # 'minitest' # 'thread_safe' + spec.add_development_dependency 'activerecord', rails_versions + # arel + # activesuport + # activemodel + # Soft dependency for pagination spec.add_development_dependency 'kaminari', ' ~> 0.16.3' spec.add_development_dependency 'will_paginate', '~> 3.0', '>= 3.0.7' spec.add_development_dependency 'bundler', '~> 1.6' spec.add_development_dependency 'timecop', '~> 0.7' + spec.add_development_dependency 'minitest-reporters' end diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index b513216fa..3975391c8 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -152,7 +152,7 @@ def self.fragmented(serializer) # @todo require less code comments. See # https://github.com/rails-api/active_model_serializers/pull/1249#issuecomment-146567837 def self.cache(options = {}) - self._cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching + self._cache = ActiveModelSerializers.config.cache_store if ActiveModelSerializers.config.perform_caching self._cache_key = options.delete(:key) self._cache_only = options.delete(:only) self._cache_except = options.delete(:except) diff --git a/lib/active_model/serializer/railtie.rb b/lib/active_model/serializer/railtie.rb index 533a191c3..18bb513c9 100644 --- a/lib/active_model/serializer/railtie.rb +++ b/lib/active_model/serializer/railtie.rb @@ -8,6 +8,13 @@ class Railtie < Rails::Railtie end end + initializer 'active_model_serializers.caching' do + ActiveSupport.on_load(:action_controller) do + ActiveModelSerializers.config.cache_store = ActionController::Base.cache_store + ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching + end + end + initializer 'generators' do |app| app.load_generators require 'generators/serializer/resource_override' diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb index c7a4d1d43..a3e2ff006 100644 --- a/lib/active_model_serializers.rb +++ b/lib/active_model_serializers.rb @@ -5,6 +5,10 @@ module ActiveModelSerializers mattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) } + def self.config + ActiveModel::Serializer.config + end + extend ActiveSupport::Autoload autoload :Model autoload :Callbacks diff --git a/test/action_controller/json_api/pagination_test.rb b/test/action_controller/json_api/pagination_test.rb index 4286ed886..7c8ebec26 100644 --- a/test/action_controller/json_api/pagination_test.rb +++ b/test/action_controller/json_api/pagination_test.rb @@ -53,7 +53,7 @@ def test_render_pagination_links_with_will_paginate 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" } - get :render_pagination_using_will_paginate, page: { number: 2, size: 1 } + get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end @@ -62,7 +62,7 @@ def test_render_only_last_and_next_pagination_links expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2", 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2", 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" } - get :render_pagination_using_will_paginate, page: { number: 1, size: 2 } + get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end @@ -73,7 +73,7 @@ def test_render_pagination_links_with_kaminari 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", 'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", 'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" } - get :render_pagination_using_kaminari, page: { number: 2, size: 1 } + get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end @@ -82,7 +82,7 @@ def test_render_only_prev_and_first_pagination_links expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1", 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1", 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" } - get :render_pagination_using_kaminari, page: { number: 3, size: 1 } + get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end @@ -91,7 +91,7 @@ def test_render_only_last_and_next_pagination_links_with_additional_params expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional", 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional", 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" } - get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional' + get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end @@ -100,13 +100,13 @@ def test_render_only_prev_and_first_pagination_links_with_additional_params expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional", 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional", 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" } - get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional' + get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' } response = JSON.parse(@response.body) assert_equal expected_links, response['links'] end def test_array_without_pagination_links - get :render_array_without_pagination_links, page: { number: 2, size: 1 } + get :render_array_without_pagination_links, params: { page: { number: 2, size: 1 } } response = JSON.parse(@response.body) refute response.key? 'links' end diff --git a/test/action_controller/serialization_scope_name_test.rb b/test/action_controller/serialization_scope_name_test.rb index 75220be29..5969f5226 100644 --- a/test/action_controller/serialization_scope_name_test.rb +++ b/test/action_controller/serialization_scope_name_test.rb @@ -12,7 +12,7 @@ def admin? class UserTestController < ActionController::Base protect_from_forgery - before_filter { request.format = :json } + before_action { request.format = :json } def current_user User.new(id: 1, name: 'Pete', admin: false) @@ -43,7 +43,7 @@ class AdminUserTestController < ActionController::Base protect_from_forgery serialization_scope :current_admin - before_filter { request.format = :json } + before_action { request.format = :json } def current_admin User.new(id: 2, name: 'Bob', admin: true) diff --git a/test/adapter/fragment_cache_test.rb b/test/adapter/fragment_cache_test.rb index fc6c3feb4..cacc156c3 100644 --- a/test/adapter/fragment_cache_test.rb +++ b/test/adapter/fragment_cache_test.rb @@ -4,6 +4,7 @@ class Serializer module Adapter class FragmentCacheTest < Minitest::Test def setup + super @spam = Spam::UnrelatedLink.new(id: 'spam-id-1') @author = Author.new(name: 'Joao M. D. Moura') @role = Role.new(name: 'Great Author', description: nil) diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 28ee08d52..573b3fa8d 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -16,6 +16,12 @@ def method_missing(meth, *args) end end + # required for ActiveModel::AttributeAssignment#_assign_attribute + # in Rails 5 + def respond_to_missing?(method_name, _include_private = false) + attributes.key?(method_name.to_s.tr('=', '').to_sym) || super + end + def cache_key_with_digest "#{cache_key}/#{FILE_DIGEST}" end diff --git a/test/support/rails5_shims.rb b/test/support/rails5_shims.rb new file mode 100644 index 000000000..5677d1099 --- /dev/null +++ b/test/support/rails5_shims.rb @@ -0,0 +1,29 @@ +module Rails5Shims + module ControllerTests + # https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb + REQUEST_KWARGS = [:params, :session, :flash, :method, :body, :xhr] + + # Fold kwargs from test request into args + # Band-aid for DEPRECATION WARNING + def get(path, *args) + hash = args && args[0] + if hash.respond_to?(:key) + Rails5Shims::ControllerTests::REQUEST_KWARGS.each do |kwarg| + next unless hash.key?(kwarg) + hash.merge! hash.delete(kwarg) + end + end + super + end + + # Uncomment for debugging where the kwargs warnings come from + # def non_kwarg_request_warning + # super.tap do + # STDOUT.puts caller[2..3] + # end + # end + end +end +if Rails::VERSION::MAJOR < 5 + ActionController::TestCase.send :include, Rails5Shims::ControllerTests +end diff --git a/test/support/rails_app.rb b/test/support/rails_app.rb index c567de2db..ced830dc5 100644 --- a/test/support/rails_app.rb +++ b/test/support/rails_app.rb @@ -1,14 +1,18 @@ -class Foo < Rails::Application +class ActiveModelSerializers::RailsApplication < Rails::Application if Rails::VERSION::MAJOR >= 4 config.eager_load = false + config.secret_key_base = 'abc123' - config.action_controller.perform_caching = true + config.active_support.test_order = :random + config.logger = Logger.new(nil) + + config.action_controller.perform_caching = true ActionController::Base.cache_store = :memory_store end end -Foo.initialize! +ActiveModelSerializers::RailsApplication.initialize! module TestHelper Routes = ActionDispatch::Routing::RouteSet.new diff --git a/test/support/test_case.rb b/test/support/test_case.rb index 66e8648d2..8f1afe79a 100644 --- a/test/support/test_case.rb +++ b/test/support/test_case.rb @@ -2,4 +2,18 @@ def setup @routes = TestHelper::Routes end + + # For Rails5 + # https://github.com/rails/rails/commit/ca83436d1b3b6cedd1eca2259f65661e69b01909#diff-b9bbf56e85d3fe1999f16317f2751e76L17 + def assigns(key = nil) + assigns = {}.with_indifferent_access + @controller.view_assigns.each { |k, v| assigns.regular_writer(k, v) } + key.nil? ? assigns : assigns[key] + end + + # Rails5: Uncomment for debugging where the warnings come from + # def non_kwarg_request_warning + # super + # STDOUT.puts caller[2..3] + # end end diff --git a/test/test_helper.rb b/test/test_helper.rb index f494eba12..9d0698207 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -64,6 +64,8 @@ def Minitest.after_run(&block) require 'support/serialization_testing' +require 'support/rails5_shims' + require 'fixtures/active_record' require 'fixtures/poro'