Module: Gon::Base
- Defined in:
- lib/gon/base.rb
Class Method Summary collapse
- .get_controller(options) ⇒ Object
- .get_template_path(options, extension) ⇒ Object
- .render_data(options) ⇒ Object
Class Method Details
.get_controller(options) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/gon/base.rb', line 32 def get_controller() [: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
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gon/base.rb', line 40 def get_template_path(, extension) if [:template] if right_extension?(extension, [:template]) [:template] else [[:template], extension].join('.') end else controller = get_controller().controller_path action = get_controller().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 27 28 29 30 |
# File 'lib/gon/base.rb', line 5 def render_data() data = Gon.all_variables || {} if Gon.global.all_variables.present? data[:global] = Gon.global.all_variables end namespace = [:namespace] || 'gon' need_tag = [:need_tag].nil? || [:need_tag] start = "#{need_tag ? '<script>' : ''}window.#{namespace} = {};" script = '' if data.present? if [: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 end script = start + Gon::Escaper.escape(script) script << '</script>' if need_tag script.html_safe end |