From f50a82795c1e883e29058ee422fabaecfa744fb1 Mon Sep 17 00:00:00 2001 From: Jeff Casimir Date: Thu, 20 Oct 2011 03:32:26 -0400 Subject: [PATCH] Abstracting Thread.current implementation detail --- lib/draper/base.rb | 2 +- lib/draper/system.rb | 2 +- lib/draper/view_context.rb | 12 +++++++++++- spec/view_context_spec.rb | 6 +++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index e535396..88899a7 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -111,7 +111,7 @@ module Draper # @return [Object] proxy class << self def helpers - Thread.current[:current_view_context] + Draper::ViewContext.current end alias :h :helpers end diff --git a/lib/draper/system.rb b/lib/draper/system.rb index 26ced3a..29cf957 100644 --- a/lib/draper/system.rb +++ b/lib/draper/system.rb @@ -1,7 +1,7 @@ module Draper class System def self.setup - ActionController::Base.send(:include, Draper::ViewContext) if defined?(ActionController::Base) + ActionController::Base.send(:include, Draper::ViewContextFilter) if defined?(ActionController::Base) end end end \ No newline at end of file diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb index 249876a..6b5ab21 100644 --- a/lib/draper/view_context.rb +++ b/lib/draper/view_context.rb @@ -1,7 +1,17 @@ module Draper module ViewContext + def self.current + Thread.current[:current_view_context] + end + + def self.current=(input) + Thread.current[:current_view_context] = input + end + end + + module ViewContextFilter def set_current_view_context - Thread.current[:current_view_context] = self.view_context + Draper::ViewContext.current = self.view_context end def self.included(source) diff --git a/spec/view_context_spec.rb b/spec/view_context_spec.rb index a221a97..a692215 100644 --- a/spec/view_context_spec.rb +++ b/spec/view_context_spec.rb @@ -19,17 +19,17 @@ describe Draper::ViewContext do end it "raises an exception if the view_context is fetched without being set" do - Thread.current[:current_view_context] = nil + Draper::ViewContext.current = nil expect {app_controller.current_view_context}.should raise_exception(Exception) end it "sets view_context every time" do app_controller_instance.stub(:view_context) { 'first' } app_controller_instance.set_current_view_context - Thread.current[:current_view_context].should == 'first' + Draper::ViewContext.current.should == 'first' app_controller_instance.stub(:view_context) { 'second' } app_controller_instance.set_current_view_context - Thread.current[:current_view_context].should == 'second' + Draper::ViewContext.current.should == 'second' end end \ No newline at end of file