-
Notifications
You must be signed in to change notification settings - Fork 0
/
weld_handler.rb
75 lines (63 loc) · 1.88 KB
/
weld_handler.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
74
# A namespace for views
module Views
end
module Weld
class View
attr_reader :controller
def initialize(controller, assigns)
@controller = controller
assigns.each do |name, value|
instance_variable_set "@#{ name }", value
end
end
end
end
module Weld
module Compiler
def self.context
ExecJS.compile <<-JAVASCRIPT
var run = function(html, data, config) {
var jsdom = require('jsdom');
var weldlib = require('weld');
var a;
jsdom.env(html, function(error, window) {
var element = window.document;
weldlib.weld(element, data, config);
a = element.innerHTML;
});
return a;
};
JAVASCRIPT
end
end
end
module ActionView
module Template::Handlers
class Weld
# Default format used by Weld.
class_attribute :default_format
self.default_format = Mime::HTML
def self.call(template)
<<-RUBY
# raise self.controller.inspect
# raise @virtual_path.inspect
# raise self.assigns.inspect
# HACK:
begin
load(Rails.root.join('app/views', @virtual_path + ".rb"))
class_name = self.controller.class.to_s.gsub(/Controller$/, '')
view = Views.const_get(class_name).new(self.controller, self.assigns)
data = view.send self.controller.action_name
config_method = self.controller.action_name + "_config"
config = view.send config_method if view.respond_to? config_method
rescue LoadError # , NameError
# Fall back to assigns
data = @data
end
Weld::Compiler.context.call('run', %q{#{ template.source }}, data, config || {})
RUBY
end
end
end
end
ActionView::Template.register_template_handler(:weld, ActionView::Template::Handlers::Weld)