Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get tests passing on Rails 5 #1324

Merged
merged 7 commits into from
Nov 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ sudo: false
rvm:
- 1.9.3
- 2.0.0
- 2.2
- 2.1
- 2.2.2
- ruby-head
- rbx-2

Expand All @@ -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
7 changes: 3 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions lib/active_model/serializer/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions test/action_controller/json_api/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/action_controller/serialization_scope_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/adapter/fragment_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/support/rails5_shims.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 7 additions & 3 deletions test/support/rails_app.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/support/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def Minitest.after_run(&block)

require 'support/serialization_testing'

require 'support/rails5_shims'

require 'fixtures/active_record'

require 'fixtures/poro'
Expand Down