-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
helpers.rb
48 lines (44 loc) · 1.19 KB
/
helpers.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
module Gon
module Helpers
def self.included base
base.send(:include, InstanceMethods)
end
module InstanceMethods
def include_gon(options = {})
if Gon.request_env
data = Gon.all_variables
namespace = options[:namespace] || 'gon'
script = "<script>window." + namespace + " = {};"
unless options[:camel_case]
data.each do |key, val|
script += namespace + "." + key.to_s + '=' + val.to_json + ";"
end
else
data.each do |key, val|
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
end
end
script += "</script>"
script.html_safe
else
""
end
end
end
end
module GonHelpers
def self.included base
base.send(:include, InstanceMethods)
end
module InstanceMethods
def gon
if !Gon.request_env || Gon.request_env.object_id != request.env.object_id
Gon.request_env = request.env
end
Gon
end
end
end
end
ActionView::Base.send :include, Gon::Helpers
ActionController::Base.send :include, Gon::GonHelpers