1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Separate ViewContext get/create context behavior

Separating these behaviors eliminates and entire class of bugs like forgetting to re-set the thread local, attempting to re-set the block-local conditionally, and so on. It also makes it more obvious that `self.current` is memoizing.
This commit is contained in:
Rein Henrichs 2012-08-29 11:50:49 -07:00
parent 8b00deae68
commit 91d0b67654

View file

@ -1,13 +1,7 @@
module Draper module Draper
module ViewContext module ViewContext
def self.current def self.current
context = Thread.current[:current_view_context] Thread.current[:current_view_context] ||= build_view_context
context ||= ApplicationController.new.view_context
context.controller.request ||= ActionController::TestRequest.new
context.request ||= context.controller.request
context.params ||= {}
Thread.current[:current_view_context] = context
context
end end
def self.current=(input) def self.current=(input)
@ -19,5 +13,15 @@ module Draper
Draper::ViewContext.current = context Draper::ViewContext.current = context
end end
end end
private
def build_view_context
ApplicationController.new.view_context.tap do |context|
context.controller.request ||= ActionController::TestRequest.new
context.request ||= context.controller.request
context.params ||= {}
end
end
end end
end end