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
-
.included(base) ⇒ Object
Internal: Ensure that methods are not defined as actions.
Instance Method Summary collapse
-
#proffer(variables) ⇒ Object
Public: Make the given values available to the view as local variables.
-
#proffered ⇒ Object
Public: All proffered values and their given keys.
-
#render(*args, &blk) ⇒ Object
Internal: Override Action Controller’s render method to convert proffered variables to locals.
-
#view_assigns ⇒ Object
Internal: Override Action Controller’s view_assigns to no longer copy instance variables into the view.
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 |
#proffered ⇒ Object
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? = args. [:locals] ||= {} [:locals] = proffered.merge([:locals]) args << end super(*args, &blk) end |
#view_assigns ⇒ Object
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 |