Module: Gon::Helpers::InstanceMethods

Defined in:
lib/gon/helpers.rb

Instance Method Summary collapse

Instance Method Details

#include_gon(options = {}) ⇒ Object



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

def include_gon(options = {})
  if Gon.request_env && Gon.all_variables.present?
    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