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

fix issue with using draper outside of controller/view context #927

Merged
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
10 changes: 1 addition & 9 deletions lib/draper/view_context/build_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,9 @@ def call
def controller
Draper::ViewContext.controller ||= Draper.default_controller.new
Draper::ViewContext.controller.tap do |controller|
controller.request ||= new_test_request controller if defined?(ActionController::TestRequest)
controller.request ||= ActionDispatch::TestRequest.create
end
end

def new_test_request(controller)
is_above_rails_5_1 ? ActionController::TestRequest.create(controller) : ActionController::TestRequest.create
Alexander-Senko marked this conversation as resolved.
Show resolved Hide resolved
end

def is_above_rails_5_1
ActionController::TestRequest.method(:create).parameters.first == [:req, :controller_class]
end
end
end
end
Expand Down
19 changes: 1 addition & 18 deletions spec/draper/view_context/build_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,14 @@ module Draper

expect(controller.request).to be_nil
strategy.call
expect(controller.request).to be_an ActionController::TestRequest
expect(controller.request).to be_an ActionDispatch::TestRequest
expect(controller.params).to be_empty

# sanity checks
expect(controller.view_context.request).to be controller.request
expect(controller.view_context.params).to be controller.params
end

it "compatible with rails 5.1 change on ActionController::TestRequest.create method" do
ActionController::TestRequest.class_eval do
if ActionController::TestRequest.method(:create).parameters.first == []
def create controller_class
create
end
end
end
controller = Class.new(ActionController::Base).new
allow(ViewContext).to receive_messages controller: controller
strategy = ViewContext::BuildStrategy::Full.new

expect(controller.request).to be_nil
strategy.call
expect(controller.request).to be_an ActionController::TestRequest
end

it "adds methods to the view context from the constructor block" do
allow(ViewContext).to receive(:controller).and_return(fake_controller)
strategy = ViewContext::BuildStrategy::Full.new do
Expand Down