Module: PokeJs::ViewHelpers
- Defined in:
- lib/poke_js/view_helpers.rb
Instance Method Summary collapse
- #define_poke_js_params(template = @poke_js_template) ⇒ Object
- #head_poke ⇒ Object
- #poke(template = @poke_js_template, format = formats.first) ⇒ Object
- #poke_js_params(template = @poke_js_template) ⇒ Object
- #source_poke ⇒ Object
- #with_format(format, &block) ⇒ Object
- #with_template(template) ⇒ Object
Instance Method Details
#define_poke_js_params(template = @poke_js_template) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/poke_js/view_helpers.rb', line 25 def define_poke_js_params(template=@poke_js_template) controller, action = extract_template(template) params = { controller: controller, action: action, method: request.method, path: request.env['PATH_INFO'], format: request.format.symbol } if self.respond_to? :add_params params.reverse_merge! add_params end javascript = params.to_json if block_given? if yield javascript = "$.extend(#{yield}, #{javascript});" end end raw javascript end |
#head_poke ⇒ Object
41 42 43 |
# File 'lib/poke_js/view_helpers.rb', line 41 def head_poke raw "#{source_poke}\n#{poke}" end |
#poke(template = @poke_js_template, format = formats.first) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/poke_js/view_helpers.rb', line 45 def poke(template=@poke_js_template, format=formats.first) controller, action = extract_template(template) poke_lambda = -> do if format == :html with_format(:js) do javascript_tag do raw %Q/ POKE.create_namespace('#{poke_js_params}'); #{poke_js_params} = #{ define_poke_js_params do if lookup_context.template_exists? "#{controller}/#{action}_params" render(template: "#{controller}/#{action}_params") end end }; #{ if @source_poke %Q/$(function() { POKE.exec_all(#{poke_js_params}); });/ else %Q/ POKE.define('POKE', { params: #{poke_js_params}, init: function() { POKE.exec_all(POKE.params); }, }); $(POKE.init); / end } / end end elsif format == :js content_for :head do javascript_tag do raw "$(function(){#{render template: "#{controller}/#{action}", formats: [:js], layout: "layouts/application"}});" end end end end if template != @poke_js_template with_template(template) { poke_lambda.call } else poke_lambda.call end end |
#poke_js_params(template = @poke_js_template) ⇒ Object
20 21 22 23 |
# File 'lib/poke_js/view_helpers.rb', line 20 def poke_js_params(template=@poke_js_template) controller, action = extract_template(template) raw "APP.#{controller.gsub("/", ".")}.#{request.format.symbol}.#{action}_params" end |
#source_poke ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/poke_js/view_helpers.rb', line 90 def source_poke source_template = flash[:source_template] if source_template @source_poke = true result = poke source_template @source_poke = false result end end |
#with_format(format, &block) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/poke_js/view_helpers.rb', line 4 def with_format(format, &block) old_formats = formats self.formats = [format] result = block.call self.formats = old_formats result end |
#with_template(template) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/poke_js/view_helpers.rb', line 12 def with_template(template) old_template = @poke_js_template @poke_js_template = template result = yield @poke_js_template = old_template result end |