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

Instance Method Summary collapse

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.



30
31
32
33
# File 'lib/proffer.rb', line 30

def proffer(variables)
  @_proffered_variables ||= {}
  @_proffered_variables.merge!(variables)
end

#render(*args, &blk) ⇒ Object

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/proffer.rb', line 37

def render(*args, &blk)
  if @_proffered_variables
    options = args.extract_options!
    options[:locals] ||= {}
    options[:locals] = @_proffered_variables.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.



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

def view_assigns
  {}
end