module Draper::ViewContext

Public Class Methods

build() click to toggle source

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

# File lib/draper/view_context.rb, line 48
def self.build
  build_strategy.call
end
build!() click to toggle source

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

@return [HelperProxy]

# 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_strategy() click to toggle source

@private

# File lib/draper/view_context.rb, line 82
def self.build_strategy
  @build_strategy ||= Draper::ViewContext::BuildStrategy.new(:full)
end
build_view_context() click to toggle source

@deprecated Use {build} instead.

# 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!() click to toggle source

Clears the saved controller and view context.

# File lib/draper/view_context.rb, line 41
def self.clear!
  RequestStore.store.delete :current_controller
  RequestStore.store.delete :current_view_context
end
controller() click to toggle source

Returns the current controller.

# File lib/draper/view_context.rb, line 19
def self.controller
  RequestStore.store[:current_controller]
end
controller=(controller) click to toggle source

Sets the current controller.

# File lib/draper/view_context.rb, line 24
def self.controller=(controller)
  RequestStore.store[:current_controller] = controller
end
current() click to toggle source

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

@return [HelperProxy]

# File lib/draper/view_context.rb, line 31
def self.current
  RequestStore.store.fetch(:current_view_context) { build! }
end
current=(view_context) click to toggle source

Sets the current view context.

# 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() click to toggle source

@deprecated Use {controller} instead.

# 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) click to toggle source

@deprecated Use {controller=} instead.

# 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) click to toggle source

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.

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

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

@param [:full, :fast] name

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.
# File lib/draper/view_context.rb, line 77
def self.test_strategy(name, &block)
  @build_strategy = Draper::ViewContext::BuildStrategy.new(name, &block)
end

Public Instance Methods

activate_draper() click to toggle source

Set the current controller

# File lib/draper/view_context.rb, line 14
def activate_draper
  Draper::ViewContext.controller = self
end
view_context() click to toggle source

Hooks into a controller or mailer to save the view context in {current}.

Calls superclass method
# File lib/draper/view_context.rb, line 7
def view_context
  super.tap do |context|
    Draper::ViewContext.current = context
  end
end