Abstracting Thread.current implementation detail

This commit is contained in:
Jeff Casimir 2011-10-20 03:32:26 -04:00
parent fa6883e12a
commit f50a82795c
4 changed files with 16 additions and 6 deletions

View File

@ -111,7 +111,7 @@ module Draper
# @return [Object] proxy # @return [Object] proxy
class << self class << self
def helpers def helpers
Thread.current[:current_view_context] Draper::ViewContext.current
end end
alias :h :helpers alias :h :helpers
end end

View File

@ -1,7 +1,7 @@
module Draper module Draper
class System class System
def self.setup 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 end
end end

View File

@ -1,7 +1,17 @@
module Draper module Draper
module ViewContext 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 def set_current_view_context
Thread.current[:current_view_context] = self.view_context Draper::ViewContext.current = self.view_context
end end
def self.included(source) def self.included(source)

View File

@ -19,17 +19,17 @@ describe Draper::ViewContext do
end end
it "raises an exception if the view_context is fetched without being set" do 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) expect {app_controller.current_view_context}.should raise_exception(Exception)
end end
it "sets view_context every time" do it "sets view_context every time" do
app_controller_instance.stub(:view_context) { 'first' } app_controller_instance.stub(:view_context) { 'first' }
app_controller_instance.set_current_view_context 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.stub(:view_context) { 'second' }
app_controller_instance.set_current_view_context app_controller_instance.set_current_view_context
Thread.current[:current_view_context].should == 'second' Draper::ViewContext.current.should == 'second'
end end
end end