Skip to content

Commit

Permalink
fix issue with using draper outside of controller/view context
Browse files Browse the repository at this point in the history
fixes #926

however the current PR also drops support for rails < 6.0
open to discussion around this if there's any engagement on this PR.
  • Loading branch information
timdiggins authored and Alexander-Senko committed Sep 1, 2024
1 parent 9daf3ac commit a93223e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 27 deletions.
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
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

0 comments on commit a93223e

Please sign in to comment.