Module: Draper::ViewContext
- Defined in:
- lib/draper/view_context.rb,
lib/draper/view_context/build_strategy.rb
Class Method Summary collapse
-
.build ⇒ Object
Builds a new view context for usage in tests.
-
.build! ⇒ HelperProxy
Builds a new view context and sets it as the current view context.
-
.build_view_context ⇒ Object
deprecated
Deprecated.
Use ViewContext.build instead.
-
.clear! ⇒ Object
Clears the saved controller and view context.
-
.controller ⇒ Object
Returns the current controller.
-
.controller=(controller) ⇒ Object
Sets the current controller.
-
.current ⇒ HelperProxy
Returns the current view context, or builds one if none is saved.
-
.current=(view_context) ⇒ Object
Sets the current view context.
-
.current_controller ⇒ Object
deprecated
Deprecated.
Use ViewContext.controller instead.
-
.current_controller=(controller) ⇒ Object
deprecated
Deprecated.
Use ViewContext.controller= instead.
-
.test_strategy(name, &block) ⇒ Object
Configures the strategy used to build view contexts in tests, which defaults to
:full
iftest_strategy
has not been called.
Instance Method Summary collapse
-
#activate_draper ⇒ Object
Set the current controller.
-
#view_context ⇒ Object
Hooks into a controller or mailer to save the view context in ViewContext.current.
Class Method Details
.build ⇒ Object
Builds a new view context for usage in tests. See test_strategy for details of how the view context is built.
48 49 50 |
# File 'lib/draper/view_context.rb', line 48 def self.build build_strategy.call end |
.build! ⇒ HelperProxy
Builds a new view context and sets it as the current view context.
55 56 57 58 |
# File 'lib/draper/view_context.rb', line 55 def self.build! # send because we want to return the HelperProxy returned from #current= send :current=, build end |
.build_view_context ⇒ Object
Use build instead.
99 100 101 102 |
# File 'lib/draper/view_context.rb', line 99 def self.build_view_context ActiveSupport::Deprecation.warn("Draper::ViewContext.build_view_context is deprecated (use build instead)", caller) build end |
.clear! ⇒ Object
Clears the saved controller and view context.
41 42 43 44 |
# File 'lib/draper/view_context.rb', line 41 def self.clear! RequestStore.store.delete :current_controller RequestStore.store.delete :current_view_context end |
.controller ⇒ Object
Returns the current controller.
19 20 21 |
# File 'lib/draper/view_context.rb', line 19 def self.controller RequestStore.store[:current_controller] end |
.controller=(controller) ⇒ Object
Sets the current controller.
24 25 26 |
# File 'lib/draper/view_context.rb', line 24 def self.controller=(controller) RequestStore.store[:current_controller] = controller end |
.current ⇒ HelperProxy
Returns the current view context, or builds one if none is saved.
31 32 33 |
# File 'lib/draper/view_context.rb', line 31 def self.current RequestStore.store.fetch(:current_view_context) { build! } end |
.current=(view_context) ⇒ Object
Sets the current view context.
36 37 38 |
# File 'lib/draper/view_context.rb', line 36 def self.current=(view_context) RequestStore.store[:current_view_context] = Draper::HelperProxy.new(view_context) end |
.current_controller ⇒ Object
Use controller instead.
87 88 89 90 |
# File 'lib/draper/view_context.rb', line 87 def self.current_controller ActiveSupport::Deprecation.warn("Draper::ViewContext.current_controller is deprecated (use controller instead)", caller) self.controller || ApplicationController.new end |
.current_controller=(controller) ⇒ Object
Use controller= instead.
93 94 95 96 |
# File 'lib/draper/view_context.rb', line 93 def self.current_controller=(controller) ActiveSupport::Deprecation.warn("Draper::ViewContext.current_controller= is deprecated (use controller instead)", caller) self.controller = controller end |
.test_strategy(name, &block) ⇒ Object
Configures the strategy used to build view contexts in tests, which
defaults to :full
if test_strategy
has not been called. Evaluates
the block, if given, in the context of the view context's class.
77 78 79 |
# File 'lib/draper/view_context.rb', line 77 def self.test_strategy(name, &block) @build_strategy = Draper::ViewContext::BuildStrategy.new(name, &block) end |
Instance Method Details
#activate_draper ⇒ Object
Set the current controller
14 15 16 |
# File 'lib/draper/view_context.rb', line 14 def activate_draper Draper::ViewContext.controller = self end |
#view_context ⇒ Object
Hooks into a controller or mailer to save the view context in current.
7 8 9 10 11 |
# File 'lib/draper/view_context.rb', line 7 def view_context super.tap do |context| Draper::ViewContext.current = context end end |