-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_with.rb
52 lines (44 loc) · 1.24 KB
/
render_with.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
class Renderer
include ActionController::UrlWriter
attr_reader :page
def initialize(page=nil, assigns=nil, context=nil)
@context = context
if assigns
assigns.each_pair do |key,value|
instance_variable_set "@#{key}", value
end
end
if page
@page = page
elsif context
@page = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(context){}
else
raise ArgumentError, "can't construct a page without a context"
end
end
def to_s
@page.to_s
end
private
def method_missing(*args, &block)
@context.send *args, &block
end
end
class ActionView::Base
def render_with(renderer_name, &block)
page = eval("page", block.binding)
renderer = "#{renderer_name}_renderer".classify.constantize.new(page, @template.assigns, @template)
yield renderer
end
end
# Alternative way to use render_with. Doesn't rely on eval voodoo.
# example use:
# page.render_with :foo do |foo|
# foo.do_something_cool
# end
module ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods
def render_with(renderer_name, &block)
renderer = "#{renderer_name}_renderer".classify.constantize.new(self, @context.assigns, @context)
yield renderer
end
end