From 91d0b67654d3fc5007ae5c1ec52b0dd81a112523 Mon Sep 17 00:00:00 2001 From: Rein Henrichs Date: Wed, 29 Aug 2012 11:50:49 -0700 Subject: [PATCH] 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. --- lib/draper/view_context.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb index 59e38a4..cadd2e6 100644 --- a/lib/draper/view_context.rb +++ b/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) @@ -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