diff --git a/lib/draper/railtie.rb b/lib/draper/railtie.rb old mode 100644 new mode 100755 index 598e44f..04d35fa --- a/lib/draper/railtie.rb +++ b/lib/draper/railtie.rb @@ -24,13 +24,13 @@ module Draper initializer "draper.extend_action_controller_base" do |app| ActiveSupport.on_load(:action_controller) do - Draper::System.setup(self) + Draper::System.setup_action_controller(self) end end initializer "draper.extend_action_mailer_base" do |app| ActiveSupport.on_load(:action_mailer) do - Draper::System.setup(self) + Draper::System.setup_action_mailer(self) end end diff --git a/lib/draper/system.rb b/lib/draper/system.rb old mode 100644 new mode 100755 index ff378cd..fac81c3 --- a/lib/draper/system.rb +++ b/lib/draper/system.rb @@ -1,10 +1,18 @@ module Draper class System - def self.setup(component) + def self.setup_action_controller(component) component.class_eval do include Draper::ViewContext - extend Draper::HelperSupport unless defined?(::ActionMailer) && self.is_a?(::ActionMailer::Base) + extend Draper::HelperSupport + before_filter lambda {|controller| + Draper::ViewContext.current = nil + Draper::ViewContext.current_controller = controller + } end end + + def self.setup_action_mailer(component) + include Draper::ViewContext + end end end diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb old mode 100644 new mode 100755 index 59e06f0..a7a1724 --- a/lib/draper/view_context.rb +++ b/lib/draper/view_context.rb @@ -1,11 +1,19 @@ module Draper module ViewContext + def self.current_controller + Thread.current[:current_controller] || ApplicationController.new + end + + def self.current_controller=(controller) + Thread.current[:current_controller] = controller + end + def self.current Thread.current[:current_view_context] ||= build_view_context end - def self.current=(input) - Thread.current[:current_view_context] = input + def self.current=(context) + Thread.current[:current_view_context] = context end def view_context @@ -17,10 +25,12 @@ module Draper private def self.build_view_context - ApplicationController.new.view_context.tap do |context| - context.controller.request ||= ActionController::TestRequest.new - context.request ||= context.controller.request - context.params ||= {} + current_controller.view_context.tap do |context| + if defined?(ActionController::TestRequest) + context.controller.request ||= ActionController::TestRequest.new + context.request ||= context.controller.request + context.params ||= {} + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fd1cb3a..cbc03ca 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,7 +32,7 @@ require 'action_controller/test_case' module ActionController class Base - Draper::System.setup(self) + Draper::System.setup_action_controller(self) end end