diff --git a/README.md b/README.md index bf29e97..ce4db48 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 @@ -257,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 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 @@ -289,14 +308,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. diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index 97a3232..4fc3d06 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,61 +107,26 @@ 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 - # - 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 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 6517c38..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 + + @@ -63,7 +68,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..e749b21 100644 --- a/test_app/app/views/layouts/application.html.erb +++ b/test_app/app/views/layouts/application.html.erb @@ -19,12 +19,17 @@
  • <%= 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