Module: Draper::ViewContext

Defined in:
lib/draper/view_context.rb,
lib/draper/view_context/build_strategy.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.buildObject

Builds a new view context for usage in tests. See test_strategy for details of how the view context is built.



50
51
52
# File 'lib/draper/view_context.rb', line 50

def self.build
  build_strategy.call
end

.build!HelperProxy

Builds a new view context and sets it as the current view context.

Returns:



57
58
59
60
# File 'lib/draper/view_context.rb', line 57

def self.build!
  # send because we want to return the HelperProxy returned from #current=
  send :current=, build
end

.clear!Object

Clears the saved controller and view context.



43
44
45
46
# File 'lib/draper/view_context.rb', line 43

def self.clear!
  RequestStore.store.delete :current_controller
  RequestStore.store.delete :current_view_context
end

.controllerObject

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. Clears view context when we are setting different controller.



25
26
27
28
# File 'lib/draper/view_context.rb', line 25

def self.controller=(controller)
  clear! if RequestStore.store[:current_controller] != controller
  RequestStore.store[:current_controller] = controller
end

.currentHelperProxy

Returns the current view context, or builds one if none is saved.

Returns:



33
34
35
# File 'lib/draper/view_context.rb', line 33

def self.current
  RequestStore.store.fetch(:current_view_context) { build! }
end

.current=(view_context) ⇒ Object

Sets the current view context.



38
39
40
# File 'lib/draper/view_context.rb', line 38

def self.current=(view_context)
  RequestStore.store[:current_view_context] = Draper::HelperProxy.new(view_context)
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.

Examples:

Pass a block to add helper methods to the view context:

Draper::ViewContext.test_strategy :fast do
  include ApplicationHelper
end

Parameters:

  • name (:full, :fast)

    the strategy to use:

    :full - build a fully-working view context. Your Rails environment must be loaded, including your ApplicationController.

    :fast - build a minimal view context in tests, with no dependencies on other components of your application.



79
80
81
# File 'lib/draper/view_context.rb', line 79

def self.test_strategy(name, &block)
  @build_strategy = Draper::ViewContext::BuildStrategy.new(name, &block)
end

Instance Method Details

#activate_draperObject

Set the current controller



14
15
16
# File 'lib/draper/view_context.rb', line 14

def activate_draper
  Draper::ViewContext.controller = self
end

#view_contextObject

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