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
class << self
def helpers
Thread.current[:current_view_context]
Draper::ViewContext.current
end
alias :h :helpers
end

View File

@ -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

View File

@ -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)

View File

@ -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