Skip to content

Commit

Permalink
Separate ViewContext get/create context behavior
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
reinh committed Aug 29, 2012
1 parent 8b00dea commit 91d0b67
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/draper/view_context.rb
@@ -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)
Expand All @@ -19,5 +13,15 @@ def view_context
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

0 comments on commit 91d0b67

Please sign in to comment.