From 40c3ce8694087f990335bb7749af5f89fe36cbdb Mon Sep 17 00:00:00 2001 From: kbparauga Date: Sat, 25 Apr 2015 15:45:41 +0800 Subject: [PATCH 1/7] Do not automatically insert paloma hook --- lib/paloma/action_controller_extension.rb | 37 +++++------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index 97a3232..aaa4674 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -12,7 +12,7 @@ module Paloma prepend_view_path "#{Paloma.root}/app/views/" before_filter :track_paloma_request - after_filter :append_paloma_hook, :if => :not_redirect? + helper_method :insert_paloma_hook end end @@ -107,47 +107,24 @@ module Paloma # - # Before rendering html reponses, - # this is exectued to append Paloma's html hook to the response. + # Call in your view to insert Paloma's html hook. # # The html hook contains the javascript code that # will execute the tracked Paloma requests. # - def append_paloma_hook + def insert_paloma_hook return true if self.paloma.has_no_request? - - # Render the partial if it is present, otherwise do nothing. - begin - hook = view_context.render( - :partial => 'paloma/hook', - :locals => {:request => self.paloma.request}) - rescue ActionView::MissingTemplate - return true - end - before_body_end_index = response_body[0].rindex('') - - # Append the hook after the body tag if it is present. - if before_body_end_index.present? - before_body = response_body[0][0, before_body_end_index].html_safe - after_body = response_body[0][before_body_end_index..-1].html_safe - - response.body = before_body + hook + after_body - else - # If body tag is not present, append hook in the response body - response.body += hook - end + hook = view_context.render( + :partial => 'paloma/hook', + :locals => {:request => self.paloma.request}) self.paloma.clear_request + hook end end - def not_redirect? - self.status != 302 - end - - # # Make sure not to execute paloma on the following response type # From 32e1455c58bc87631b65545d81446f7206af56de Mon Sep 17 00:00:00 2001 From: kbparauga Date: Sat, 25 Apr 2015 15:50:43 +0800 Subject: [PATCH 2/7] Remove render override --- lib/paloma/action_controller_extension.rb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index aaa4674..4fc3d06 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -125,19 +125,7 @@ module Paloma end - # - # Make sure not to execute paloma on the following response type - # - def render options = nil, extra_options = {}, &block - [:json, :js, :xml, :file].each do |format| - if options.has_key?(format) - self.paloma.clear_request - break - end - end if options.is_a?(Hash) - super - end protected From c971ba5de7f7fe6b9eba06c8eab1a31c2bbce0ee Mon Sep 17 00:00:00 2001 From: kbparauga Date: Sat, 25 Apr 2015 17:04:53 +0800 Subject: [PATCH 3/7] Fix tests --- test_app/app/controllers/main_controller.rb | 2 +- test_app/app/views/layouts/application.html.erb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test_app/app/controllers/main_controller.rb b/test_app/app/controllers/main_controller.rb index 6517c38..047d481 100644 --- a/test_app/app/controllers/main_controller.rb +++ b/test_app/app/controllers/main_controller.rb @@ -63,7 +63,7 @@ class MainController < ApplicationController def file_response - render :file => "#{Rails.root}/Gemfile" + render :file => "#{Rails.root}/Gemfile", :layout => false end end diff --git a/test_app/app/views/layouts/application.html.erb b/test_app/app/views/layouts/application.html.erb index 7bc31c2..cc6218b 100644 --- a/test_app/app/views/layouts/application.html.erb +++ b/test_app/app/views/layouts/application.html.erb @@ -25,6 +25,7 @@
<%= yield %> +<%= insert_paloma_hook %> From e212c79ca04d23d78e8d1e35a373e766267cec09 Mon Sep 17 00:00:00 2001 From: kbparauga Date: Sat, 25 Apr 2015 17:21:02 +0800 Subject: [PATCH 4/7] Add manual test to AJAX request --- test_app/app/assets/javascripts/application.js | 13 +++++++++++++ test_app/app/controllers/main_controller.rb | 5 +++++ test_app/app/views/layouts/application.html.erb | 4 ++++ test_app/app/views/main/ajax.html.erb | 2 ++ test_app/config/routes.rb | 1 + 5 files changed, 25 insertions(+) create mode 100644 test_app/app/views/main/ajax.html.erb diff --git a/test_app/app/assets/javascripts/application.js b/test_app/app/assets/javascripts/application.js index 79605af..224d3cc 100644 --- a/test_app/app/assets/javascripts/application.js +++ b/test_app/app/assets/javascripts/application.js @@ -52,3 +52,16 @@ Foos.prototype.otherAction = function(){}; var NotFoos = Paloma.controller('NotAdmin/Foos'); NotFoos.prototype.show = function(){}; NotFoos.prototype.otherAction = function(){}; + + +$(document).ready(function(){ + $('#js-ajax-link').on('click', function(e){ + e.preventDefault(); + + $.get($(this).prop('href'), function(response){ + $('#js-ajax-response').html(response); + Paloma.executeHook(); + Paloma.engine.start(); + }); + }); +}); diff --git a/test_app/app/controllers/main_controller.rb b/test_app/app/controllers/main_controller.rb index 047d481..0649a0b 100644 --- a/test_app/app/controllers/main_controller.rb +++ b/test_app/app/controllers/main_controller.rb @@ -40,6 +40,11 @@ class MainController < ApplicationController end + def ajax + render :ajax, :layout => false + end + + diff --git a/test_app/app/views/layouts/application.html.erb b/test_app/app/views/layouts/application.html.erb index cc6218b..e749b21 100644 --- a/test_app/app/views/layouts/application.html.erb +++ b/test_app/app/views/layouts/application.html.erb @@ -19,11 +19,15 @@
  • <%= link_to 'Main#basic_params', basic_params_main_index_path %>
  • <%= link_to 'Main#xml_response', xml_response_main_index_path %>
  • <%= link_to 'Main#file_response', file_response_main_index_path %>
  • +
  • <%= link_to 'Main#ajax', ajax_main_index_path, :id => 'js-ajax-link' %>

  • +
    +
    + <%= yield %> <%= insert_paloma_hook %> diff --git a/test_app/app/views/main/ajax.html.erb b/test_app/app/views/main/ajax.html.erb new file mode 100644 index 0000000..ede7752 --- /dev/null +++ b/test_app/app/views/main/ajax.html.erb @@ -0,0 +1,2 @@ +

    Main#AJAX

    +<%= insert_paloma_hook %> diff --git a/test_app/config/routes.rb b/test_app/config/routes.rb index d12e761..b2e76c8 100644 --- a/test_app/config/routes.rb +++ b/test_app/config/routes.rb @@ -12,6 +12,7 @@ TestApp::Application.routes.draw do get :js_response get :xml_response get :file_response + get :ajax end end From 17b4d8bb21860e73963b9243da2968934665a0c3 Mon Sep 17 00:00:00 2001 From: Karl Bryan Paragua Date: Sat, 25 Apr 2015 17:30:33 +0800 Subject: [PATCH 5/7] Update README.md --- README.md | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bf29e97..c978a3d 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,23 @@ That's it! Simply Sexy! ## Install -* Without bundler: `sudo gem install paloma`. -* With bundler, add this to your Gemfile: `gem 'paloma'` -* Require `paloma` in your `application.js`: `//= require paloma` +1. Without bundler: `sudo gem install paloma`. +1. With bundler, add this to your Gemfile: `gem 'paloma'` +1. Require `paloma` in your `application.js`: `//= require paloma` +1. In your layouts insert Paloma hook. + `application.html.erb` + ```html + + + + + + <%= yield %> + <%= insert_paloma_hook %> + + + ``` ## Controllers @@ -289,14 +302,7 @@ $(document).on('page:load', function(){ ## Gotchas -* Paloma will execute on all `render` calls, except for calls with the following formats: `js`, `json`, `xml`, and `file`. - - Example: - - ```ruby - render :json => {:x => 1} # Paloma will not execute` - render :partial => '/path/to/partial' # Paloma will execute - ``` +* Make sure that the rendered view has the paloma hook (*use `insert_paloma_hook`*) for Paloma to execute. * It will cause conflicts if you have a controller and a module that has the same name. From 0441700c085ef79b33b7b4396954bff802756247 Mon Sep 17 00:00:00 2001 From: Karl Bryan Paragua Date: Sat, 25 Apr 2015 17:52:53 +0800 Subject: [PATCH 6/7] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index c978a3d..17b5841 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,12 @@ class UsersController < ApplicationController end ``` +## Hook + +`insert_paloma_hook` is a helper method that you can use in your views to insert Paloma's HTML hook. +Inside this HTML hook is where the magic happens. This is the reason why Paloma can magically know what Javascript controller/action to execute. To further understand how Paloma works, you can inspect the HTML hook, by checking at the generated HTML (*inspect element*) and locate the `div` element that has the class `js-paloma-hook`. + +Ideally, you just need to call `insert_paloma_hook` in your layouts, since the layout will always be included in every rendered view. But if you are rendering a view without a layout, make sure to call `insert_paloma_hook` in that view. ## Turbolinks Support From 9bce6bc82f052899f83612730c89d71099afa87a Mon Sep 17 00:00:00 2001 From: Karl Bryan Paragua Date: Sat, 25 Apr 2015 17:56:35 +0800 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 17b5841..ce4db48 100644 --- a/README.md +++ b/README.md @@ -273,7 +273,7 @@ end ## Hook `insert_paloma_hook` is a helper method that you can use in your views to insert Paloma's HTML hook. -Inside this HTML hook is where the magic happens. This is the reason why Paloma can magically know what Javascript controller/action to execute. To further understand how Paloma works, you can inspect the HTML hook, by checking at the generated HTML (*inspect element*) and locate the `div` element that has the class `js-paloma-hook`. +Inside this HTML hook is where the magic happens. This is the reason why Paloma can magically know what Javascript controller/action to execute. To further understand how Paloma works, you can inspect the HTML hook, by checking the generated HTML (*inspect element*) and locate the `div` element that has the class `js-paloma-hook`. Ideally, you just need to call `insert_paloma_hook` in your layouts, since the layout will always be included in every rendered view. But if you are rendering a view without a layout, make sure to call `insert_paloma_hook` in that view.