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 Aug 30, 2024
1 parent 2836ea7 commit b08c2d7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
6 changes: 3 additions & 3 deletions draper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 2.2.2'

s.add_dependency 'activesupport', '>= 5.0'
s.add_dependency 'actionpack', '>= 5.0'
s.add_dependency 'activesupport', '>= 6.0'
s.add_dependency 'actionpack', '>= 6.0'
s.add_dependency 'request_store', '>= 1.0'
s.add_dependency 'activemodel', '>= 5.0'
s.add_dependency 'activemodel', '>= 6.0'
s.add_dependency 'activemodel-serializers-xml', '>= 1.0'
s.add_dependency 'ruby2_keywords'

Expand Down
8 changes: 2 additions & 6 deletions lib/draper/view_context/build_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ 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 ||= new_test_request controller
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]
ActionDispatch::TestRequest.create
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 b08c2d7

Please sign in to comment.