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:
parent
8b00deae68
commit
91d0b67654
1 changed files with 11 additions and 7 deletions
|
@ -1,13 +1,7 @@
|
|||
module Draper
|
||||
module ViewContext
|
||||
def self.current
|
||||
context = Thread.current[:current_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
|
||||
Thread.current[:current_view_context] ||= build_view_context
|
||||
end
|
||||
|
||||
def self.current=(input)
|
||||
|
@ -19,5 +13,15 @@ module Draper
|
|||
Draper::ViewContext.current = context
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue