Module: Gon::Sinatra::Helpers

Defined in:
lib/gon/sinatra/helpers.rb

Instance Method Summary collapse

Instance Method Details

#include_gon(options = {}) ⇒ Object



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

def include_gon(options = {})
  return '' if gon.all_variables.empty?

  data = gon.all_variables
  namespace = options[:namespace] || 'gon'
  script = "<script>window.#{namespace} = {};"
  unless options[:camel_case]
    data.each do |key, val|
      script << "#{namespace}.#{key}=#{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
end