-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.rb
51 lines (47 loc) · 1.42 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
49
50
51
module Gon
module Sinatra
module Helpers
def self.included base
base.send(:include, InstanceMethods)
end
module InstanceMethods
def include_gon(options = {})
if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present?
data = Gon::Sinatra.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_s + ";"
end
else
data.each do |key, val|
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_s + ";"
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::Sinatra.request_env || Gon::Sinatra.request != request.object_id
Gon::Sinatra.request = request.object_id
Gon::Sinatra.request_env = request.env
end
Gon::Sinatra
end
end
end
end
end
Sinatra::Application.send :include, Gon::Sinatra::Helpers
Sinatra::Base.send :include, Gon::Sinatra::GonHelpers