Abstracting Thread.current implementation detail
This commit is contained in:
parent
fa6883e12a
commit
f50a82795c
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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)
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue