Module: Proffer

Defined in:
lib/proffer.rb

Overview

Public: Stops an Action Controller from automatically passing all instance variables to the view. Instead, variables must be explicitly defined by using proffer.

Examples

class PostsController < ApplicationController
  include Proffer

  # This will make a new Post object available as post in the view but
  # @heading will be nil.
  def new
    @heading = "New Post"
    proffer :post => Post.new
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Internal: Ensure that methods are not defined as actions.



20
21
22
# File 'lib/proffer.rb', line 20

def self.included(base)
  base.hide_action :proffer, :proffered
end

Instance Method Details

#proffer(variables) ⇒ Object

Public: Make the given values available to the view as local variables.

variables - The Hash of values keyed by the local variable name to be used

in the view.

Examples

proffer :model => Model.new
# => render ..., :locals => { :model => Model.new }

Returns nothing.



35
36
37
# File 'lib/proffer.rb', line 35

def proffer(variables)
  proffered.merge!(variables)
end

#profferedObject

Public: All proffered values and their given keys.

Examples

proffered
# => {}

proffer :foo => "bar"
proffered
# => { :foo => "bar" }

Returns a Hash of proffered keys and values.



51
52
53
# File 'lib/proffer.rb', line 51

def proffered
  @_proffered_variables ||= {}
end

#render(*args, &blk) ⇒ Object

Internal: Override Action Controller’s render method to convert proffered variables to locals.



57
58
59
60
61
62
63
64
65
66
# File 'lib/proffer.rb', line 57

def render(*args, &blk)
  unless proffered.empty?
    options = args.extract_options!
    options[:locals] ||= {}
    options[:locals] = proffered.merge(options[:locals])
    args << options
  end

  super(*args, &blk)
end

#view_assignsObject

Internal: Override Action Controller’s view_assigns to no longer copy instance variables into the view.



70
71
72
# File 'lib/proffer.rb', line 70

def view_assigns
  {}
end