module ContextExposer::BaseController

Public Instance Methods

build_page_obj() click to toggle source

TODO: cleanup!

# File lib/context_exposer/base_controller.rb, line 127
def build_page_obj
  return @page if @page
  @page = ContextExposer::Page.instance
  @page.clear!
  clazz = self.class

  # single or list resource ?
  @page.resource.type = calc_resource_type if calc_resource_type

  # also attempts to auto-caluclate resource.type if not set
  @page.resource.name = if clazz.respond_to?(:normalized_resource_name, true)
    clazz.normalized_resource_name 
  else
    clazz.resource_name if clazz.respond_to?(:resource_name, true)
  end

  @page.controller = self

  @page.name = if respond_to?(:page_name, true)
     page_name 
  else
    @page_name if @page_name
  end

  @page
end
calc_resource_type() click to toggle source
# File lib/context_exposer/base_controller.rb, line 154
def calc_resource_type
  return @resource_type if @resource_type
  clazz = self.class

  if clazz.respond_to?(:list_actions, true) && !clazz.list_actions.blank?
    resource_type = :list if clazz.list_actions[action_name.to_sym]
  end

  if !resource_type && clazz.respond_to?(:item_actions, true) && !clazz.item_actions.blank?
    resource_type = :item if clazz.item_actions[action_name.to_sym] 
  end
  @resource_type = resource_type
end
configure_exposed_context() click to toggle source

must be called after Controller is instantiated

# File lib/context_exposer/base_controller.rb, line 107
def configure_exposed_context
  return if configured_exposed_context?
  clazz = self.class
  exposed_methods = clazz.send(:_exposure_hash)[clazz.to_s] || []
  exposed_methods.each do |name, obj|
    options = obj[:options] || {}
    options[:cached] ? _add_cached_ctx_method(obj, name) : _add_ctx_method(obj, name)
  end
  @configured_exposed_context = true
end
configured_exposed_context?() click to toggle source
# File lib/context_exposer/base_controller.rb, line 168
def configured_exposed_context?
  @configured_exposed_context == true
end
ctx()
Alias for: view_ctx
page_obj() click to toggle source
# File lib/context_exposer/base_controller.rb, line 122
def page_obj
  @page ||= build_page_obj
end
save_exposed_context() click to toggle source
# File lib/context_exposer/base_controller.rb, line 118
def save_exposed_context
  ContextExposer::PageContext.instance.configure ctx, page_obj
end
view_ctx() click to toggle source
# File lib/context_exposer/base_controller.rb, line 16
def view_ctx
  @view_ctx ||= build_view_ctx
end
Also aliased as: ctx

Protected Instance Methods

_add_cached_ctx_method(obj, name) click to toggle source
# File lib/context_exposer/base_controller.rb, line 184
def _add_cached_ctx_method obj, name
  this = self
  options = obj[:options]
  proc = obj[:proc]
  inst_var_name = "@#{name}"

  view_ctx.send :define_singleton_method, name do
    old_val = instance_variable_get inst_var_name
    return old_val if old_val

    val = this.instance_eval(&proc)
    instance_variable_set inst_var_name, val
    val
  end
end
_add_ctx_method(obj, name) click to toggle source
# File lib/context_exposer/base_controller.rb, line 174
def _add_ctx_method obj, name
  this = self    
  proc = obj[:proc]
  inst_var_name = "@#{name}"

  view_ctx.send :define_singleton_method, name do
    this.instance_eval(&proc)
  end
end
build_view_ctx() click to toggle source

returns a ViewContext object view helpers can be exposed as singleton methods, dynamically be attached (see below)

# File lib/context_exposer/base_controller.rb, line 202
def build_view_ctx
  view_ctx_class.new self
end
view_ctx_class() click to toggle source
# File lib/context_exposer/base_controller.rb, line 206
def view_ctx_class
  @view_ctx_class ||= ContextExposer::ViewContext
end