Module: Gon::Helpers::InstanceMethods

Defined in:
lib/gon/helpers.rb

Instance Method Summary collapse

Instance Method Details

#include_gon(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gon/helpers.rb', line 10

def include_gon(options = {})
  if Gon.request_env && Gon.all_variables.present? && Gon.request == request.object_id
    data = Gon.all_variables
    namespace = options[:namespace] || 'gon'
    start = '<script>window.' + namespace + ' = {};'
    script = ''
    if options[:camel_case]
      data.each do |key, val|
        script << namespace + '.' + key.to_s.camelize(:lower) + '=' + val.to_json + ';'
      end
    else
      data.each do |key, val|
        script << namespace + '.' + key.to_s + '=' + val.to_json + ';'
      end
    end
    script = start + Gon::Escaper.escape(script) + '</script>'
    script.html_safe
  else
    ""
  end
end