Module: Gon::Base

Defined in:
lib/gon/base.rb

Class Method Summary collapse

Class Method Details

.get_controller(options = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/gon/base.rb', line 28

def get_controller(options = {})
  options[:controller] ||
    Gon::Request.env['action_controller.instance'] ||
    Gon::Request.env['action_controller.rescue.response'].
    instance_variable_get('@template').
    instance_variable_get('@controller')
end

.get_template_path(options, extension) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gon/base.rb', line 36

def get_template_path(options, extension)
  if options[:template]
    if right_extension?(extension, options[:template])
      options[:template]
    else
      [options[:template], extension].join('.')
    end
  else
    controller = get_controller(options).controller_path
    action = get_controller(options).action_name
    "app/views/#{controller}/#{action}.json.#{extension}"
  end
end

.render_data(options) ⇒ Object



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

def render_data(options)
  data = Gon.all_variables || {}
  if Gon.global.all_variables.present?
    data[:global] = Gon.global.all_variables
  end
  namespace, tag, cameled, watch = parse_options options
  start     = "#{tag if tag}window.#{namespace} = {};"
  script    = ''

  data.each do |key, val|
    if cameled
      script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
    else
      script << "#{namespace}.#{key.to_s}=#{val.to_json};"
    end
  end

  script = start + Gon::Escaper.escape(script)
  script << Gon.watch.render if watch and Gon::Watch.all_variables.present?
  script << '</script>' if tag
  script.html_safe
end