module Draper::ViewContext

def self.build

details of how the view context is built.
Builds a new view context for usage in tests. See {test_strategy} for
def self.build
  build_strategy.call
end

def self.build!

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

def self.build_strategy

Other tags:
    Private: -
def self.build_strategy
  @build_strategy ||= Draper::ViewContext::BuildStrategy.new(:full)
end

def self.clear!

Clears the saved controller and view context.
def self.clear!
  RequestStore.store.delete :current_controller
  RequestStore.store.delete :current_view_context
end

def self.controller

Returns the current controller.
def self.controller
  RequestStore.store[:current_controller]
end

def self.controller=(controller)

different controller.
Sets the current controller. Clears view context when we are setting
def self.controller=(controller)
  clear! if RequestStore.store[:current_controller] != controller
  RequestStore.store[:current_controller] = controller
end

def self.current

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

def self.current=(view_context)

Sets the current view context.
def self.current=(view_context)
  RequestStore.store[:current_view_context] = Draper::HelperProxy.new(view_context)
end

def self.test_strategy(name, &block)

Parameters:
  • name (:full, :fast) --

Other tags:
    Example: Pass a block to add helper methods to the view context: -
def self.test_strategy(name, &block)
  @build_strategy = Draper::ViewContext::BuildStrategy.new(name, &block)
end

def activate_draper

Set the current controller
def activate_draper
  Draper::ViewContext.controller = self
end

def view_context

Hooks into a controller or mailer to save the view context in {current}.
def view_context
  super.tap do |context|
    Draper::ViewContext.current = context
  end
end