Skip to content
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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ gemfile:
- test/gemfiles/Gemfile-Rails-4-0
- test/gemfiles/Gemfile-Rails-4-1
- test/gemfiles/Gemfile-Rails-4-2
- test/gemfiles/Gemfile-Rails-5-0
matrix:
exclude:
- rvm: 2.1.9
gemfile: test/gemfiles/Gemfile-Rails-5-0
allow_failures:
- rvm: rbx-2.2.10
6 changes: 4 additions & 2 deletions lib/inherited_resources/base_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ def method_for_find
# Get resource ivar based on the current resource controller.
#
def get_resource_ivar #:nodoc:
instance_variable_get("@#{resource_instance_name}")
instance_variable_defined?(:"@#{resource_instance_name}") &&
instance_variable_get("@#{resource_instance_name}")
end

# Set resource ivar based on the current resource controller.
Expand All @@ -235,7 +236,8 @@ def set_resource_ivar(resource) #:nodoc:
# Get collection ivar based on the current resource controller.
#
def get_collection_ivar #:nodoc:
instance_variable_get("@#{resource_collection_name}")
instance_variable_defined?(:"@#{resource_collection_name}") &&
instance_variable_get("@#{resource_collection_name}")
end

# Set collection ivar based on the current resource controller.
Expand Down
11 changes: 9 additions & 2 deletions lib/inherited_resources/belongs_to_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ def parent_type
# association chain.
#
def evaluate_parent(parent_symbol, parent_config, chain = nil) #:nodoc:
instantiated_object = instance_variable_get("@#{parent_config[:instance_name]}")
return instantiated_object if instantiated_object
get_parent_ivar(parent_config[:instance_name]) ||
set_parent_instance(parent_config, chain)
end

def get_parent_ivar(instance_name)
instance_variable_defined?(:"@#{instance_name}") &&
instance_variable_get("@#{instance_name}")
end

def set_parent_instance(parent_config, chain)
if parent_config[:singleton]
parent = if chain
chain.send(parent_config[:instance_name])
Expand Down
2 changes: 2 additions & 0 deletions lib/inherited_resources/url_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,13 @@ def generate_url_and_path_helpers(prefix, name, resource_segments, resource_ivar

class_eval <<-URL_HELPERS, __FILE__, __LINE__
protected
undef :#{prefix}#{name}_path if method_defined? :#{prefix}#{name}_path
def #{prefix}#{name}_path(*given_args)
given_options = given_args.extract_options!
#{prefix}#{segments}_path(#{ivars})
end

undef :#{prefix}#{name}_url if method_defined? :#{prefix}#{name}_url
def #{prefix}#{name}_url(*given_args)
given_options = given_args.extract_options!
#{prefix}#{segments}_url(#{ivars})
Expand Down
16 changes: 14 additions & 2 deletions test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ module UserTestHelper
def setup
@controller_class = Class.new(UsersController)
@controller = @controller_class.new
@controller.request = @request = ActionController::TestRequest.new
@controller.response = @response = ActionController::TestResponse.new
@controller.request = @request = new_request
@controller.response = @response = new_response
@controller.stubs(:user_url).returns("/")
end

protected

def new_request
ActionPack::VERSION::MAJOR >= 5 ? # see Rails 3806eb7
ActionController::TestRequest.new({}, ActionController::TestSession.new) :
ActionController::TestRequest.new
end

def new_response
ActionPack::VERSION::MAJOR >= 5 ? # see Rails e26d11c
ActionDispatch::TestResponse.new :
ActionController::TestResponse.new
end

def mock_user(expectations={})
@mock_user ||= begin
user = mock(expectations.except(:errors))
Expand Down
5 changes: 5 additions & 0 deletions test/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class CommentsController < InheritedResources::Base
class BelongsToTest < ActionController::TestCase
tests CommentsController

def self.test_order
# version 5 defaults to random, which fails...
MiniTest::Unit::VERSION.to_i >= 5 ? :alpha : super
end

def setup
Post.expects(:find).with('37').returns(mock_post)
mock_post.expects(:comments).returns(Comment)
Expand Down
16 changes: 14 additions & 2 deletions test/customized_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ def destroy_resource(resource)
module CarTestHelper
def setup
@controller = CarsController.new
@controller.request = @request = ActionController::TestRequest.new
@controller.response = @response = ActionController::TestResponse.new
@controller.request = @request = new_request
@controller.response = @response = new_response
@controller.stubs(:car_url).returns("/")
end

protected
def new_request
ActionPack::VERSION::MAJOR >= 5 ? # see Rails 3806eb7
ActionController::TestRequest.new({}, ActionController::TestSession.new) :
ActionController::TestRequest.new
end

def new_response
ActionPack::VERSION::MAJOR >= 5 ? # see Rails e26d11c
ActionDispatch::TestResponse.new :
ActionController::TestResponse.new
end

def mock_car(expectations={})
@mock_car ||= begin
car = mock(expectations.except(:errors))
Expand Down
9 changes: 9 additions & 0 deletions test/gemfiles/Gemfile-Rails-5-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

gemspec :path => '../..'

gem 'rails', '~> 5.0.1'

gem 'mocha'
gem 'minitest-rg'
gem 'rails-controller-testing'
24 changes: 12 additions & 12 deletions test/optional_belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_expose_all_products_as_instance_variable_with_category
def test_expose_all_products_as_instance_variable_without_category
Product.expects(:scoped).returns([mock_product])
get :index
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal [mock_product], assigns(:products)
end

Expand All @@ -47,7 +47,7 @@ def test_expose_the_requested_product_with_category
def test_expose_the_requested_product_without_category
Product.expects(:find).with('42').returns(mock_product)
get :show, :id => '42'
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end

Expand All @@ -63,7 +63,7 @@ def test_expose_a_new_product_with_category
def test_expose_a_new_product_without_category
Product.expects(:new).returns(mock_product)
get :new
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end

Expand All @@ -79,7 +79,7 @@ def test_expose_the_requested_product_for_edition_with_category
def test_expose_the_requested_product_for_edition_without_category
Product.expects(:find).with('42').returns(mock_product)
get :edit, :id => '42'
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end

Expand All @@ -95,7 +95,7 @@ def test_expose_a_newly_create_product_with_category
def test_expose_a_newly_create_product_without_category
Product.expects(:new).with({'these' => 'params'}).returns(mock_product(:save => true))
post :create, :product => {:these => 'params'}
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end

Expand All @@ -115,7 +115,7 @@ def test_update_the_requested_object_without_category
mock_product.expects(:update_attributes).with({'these' => 'params'}).returns(true)

put :update, :id => '42', :product => {:these => 'params'}
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end

Expand All @@ -137,7 +137,7 @@ def test_the_requested_product_is_destroyed_without_category
@controller.expects(:collection_url).returns('/')

delete :destroy, :id => '42'
assert_equal nil, assigns(:category)
assert_nil assigns(:category)
assert_equal mock_product, assigns(:product)
end

Expand All @@ -146,11 +146,11 @@ def test_polymorphic_helpers
get :index

assert !@controller.send(:parent?)
assert_equal nil, assigns(:parent_type)
assert_equal nil, @controller.send(:parent_type)
assert_equal nil, @controller.send(:parent_class)
assert_equal nil, assigns(:category)
assert_equal nil, @controller.send(:parent)
assert_nil assigns(:parent_type)
assert_nil @controller.send(:parent_type)
assert_nil @controller.send(:parent_class)
assert_nil assigns(:category)
assert_nil @controller.send(:parent)
end

protected
Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
require "active_model"
require "action_controller"

if ActionPack::VERSION::MAJOR >= 5
require 'rails-controller-testing'
Rails::Controller::Testing.install
end

I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
I18n.reload!

Expand Down