-
Notifications
You must be signed in to change notification settings - Fork 0
/
proffer.rb
74 lines (67 loc) · 1.75 KB
/
proffer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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
module Proffer
# Internal: Ensure that methods are not defined as actions.
def self.included(base)
base.hide_action :proffer, :proffered
end
# 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.
def proffer(variables)
proffered.merge!(variables)
end
# Public: All proffered values and their given keys.
#
# Examples
#
# proffered
# # => {}
#
# proffer :foo => "bar"
# proffered
# # => { :foo => "bar" }
#
# Returns a Hash of proffered keys and values.
def proffered
@_proffered_variables ||= {}
end
# Internal: Override Action Controller's render method to convert proffered
# variables to locals.
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
# Internal: Override Action Controller's view_assigns to no longer copy
# instance variables into the view.
def view_assigns
{}
end
end