From 7543d985df07312bd9be15b49f48b4abe779c26f Mon Sep 17 00:00:00 2001 From: kbparauga Date: Fri, 28 Aug 2015 15:13:28 +0800 Subject: [PATCH] Tests for multiple js calls --- lib/paloma/action_controller_extension.rb | 27 ++++++-- .../app/assets/javascripts/application.js | 3 +- test_app/app/controllers/main_controller.rb | 39 ++++++++++- .../app/views/layouts/application.html.erb | 5 ++ test_app/config/routes.rb | 5 ++ test_app/spec/integration/basic_spec.rb | 65 +++++++++++++++++++ 6 files changed, 134 insertions(+), 10 deletions(-) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index 0a509e3..f325309 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -55,6 +55,20 @@ module Paloma # Can call a different Controller or execute a different action, and # pass parameters. # + # NOTE: + # Calling this more than once in a single action will not clear + # the previous config from the previous call. + # + # Example: + # def new + # js 'MyController#new' + # js :edit + # js :x => 1, :y => 2 + # + # # Paloma will execute JS for + # # MyController#edit and will pass the parameters :x => 1, :y => 2 + # end + # # Usage: # # js 'Controller', {params} @@ -82,8 +96,8 @@ module Paloma # if path_or_options.is_a? String route = ::Paloma::Utilities.interpret_route path_or_options - self.paloma.resource = route[:resource] || self.default_resource - self.paloma.action = route[:action] || self.default_action + self.paloma.resource = route[:resource] if route[:resource].present? + self.paloma.action = route[:action] if route[:action].present? # :action elsif path_or_options.is_a? Symbol @@ -93,13 +107,12 @@ module Paloma elsif path_or_options.is_a? Hash self.paloma.params.merge! path_or_options || {} - elsif path_or_options == true - self.paloma.resource = self.default_resource - self.paloma.action = self.default_action - - else + elsif path_or_options != true raise "Paloma: Invalid argument (#{path_or_options}) for js method" end + + self.paloma.resource ||= self.default_resource + self.paloma.action ||= self.default_action end diff --git a/test_app/app/assets/javascripts/application.js b/test_app/app/assets/javascripts/application.js index 224d3cc..f6a7d06 100644 --- a/test_app/app/assets/javascripts/application.js +++ b/test_app/app/assets/javascripts/application.js @@ -36,12 +36,13 @@ Main.prototype.index = function(){}; Main.prototype.otherAction = function(){}; Main.prototype.prevent = function(){}; Main.prototype.basic_params = function(){}; +Main.prototype.multiple_calls_1 = function(){}; var OtherMain = Paloma.controller('OtherMain'); OtherMain.prototype.show = function(){}; OtherMain.prototype.otherAction = function(){}; - +OtherMain.prototype.multiple_calls_2 = function(){}; var Foos = Paloma.controller('Admin/Foos'); diff --git a/test_app/app/controllers/main_controller.rb b/test_app/app/controllers/main_controller.rb index 529fec7..e25d61b 100644 --- a/test_app/app/controllers/main_controller.rb +++ b/test_app/app/controllers/main_controller.rb @@ -2,8 +2,6 @@ class MainController < ApplicationController # Default behavior def index - js false - js 11231242 render :inline => 'Main#index', :layout => 'application' end @@ -29,6 +27,43 @@ class MainController < ApplicationController end + def multiple_calls_1 + js false + js :x => 70 + render :inline => 'Main#multiple_calls', :layout => 'application' + end + + + def multiple_calls_2 + js false + js 'OtherMain' + render :inline => 'Main#multiple_calls_2', :layout => 'application' + end + + + def multiple_calls_3 + js 'OtherMain' + js :show + render :inline => 'Main#multiple_calls_3', :layout => 'application' + end + + + def multiple_calls_4 + js 'OtherMain#show' + js false + render :inline => 'Main#multiple_calls_4', :layout => 'application' + end + + + def multiple_calls_5 + js false + js true + render :inline => 'Main#multiple_calls_5', :layout => 'application' + end + + + + # Stop paloma from execution def prevent js false diff --git a/test_app/app/views/layouts/application.html.erb b/test_app/app/views/layouts/application.html.erb index e749b21..8d2c635 100644 --- a/test_app/app/views/layouts/application.html.erb +++ b/test_app/app/views/layouts/application.html.erb @@ -17,6 +17,11 @@
  • <%= link_to 'Main#edit', edit_main_path(1) %>
  • <%= link_to 'Main#prevent', prevent_main_index_path %>
  • <%= link_to 'Main#basic_params', basic_params_main_index_path %>
  • +
  • <%= link_to 'Main#multiple_calls_1', multiple_calls_1_main_index_path %>
  • +
  • <%= link_to 'Main#multiple_calls_2', multiple_calls_2_main_index_path %>
  • +
  • <%= link_to 'Main#multiple_calls_3', multiple_calls_3_main_index_path %>
  • +
  • <%= link_to 'Main#multiple_calls_4', multiple_calls_4_main_index_path %>
  • +
  • <%= link_to 'Main#multiple_calls_5', multiple_calls_5_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' %>
  • diff --git a/test_app/config/routes.rb b/test_app/config/routes.rb index b2e76c8..2cf4fe4 100644 --- a/test_app/config/routes.rb +++ b/test_app/config/routes.rb @@ -13,6 +13,11 @@ TestApp::Application.routes.draw do get :xml_response get :file_response get :ajax + get :multiple_calls_1 + get :multiple_calls_2 + get :multiple_calls_3 + get :multiple_calls_4 + get :multiple_calls_5 end end diff --git a/test_app/spec/integration/basic_spec.rb b/test_app/spec/integration/basic_spec.rb index 36f0287..ccfe14e 100644 --- a/test_app/spec/integration/basic_spec.rb +++ b/test_app/spec/integration/basic_spec.rb @@ -79,6 +79,71 @@ feature 'executing Paloma controller', :js => true do + # + # + # Multiple Calls + # + # + + context 'false at first then pass a parameter' do + it 'executes default controller#action plus the parameter' do + visit multiple_calls_1_main_index_path + + expect( + request['controller'] == 'Main' && + request['action'] == 'multiple_calls_1' && + request['params'] == {'x' => 70} + ).to be_truthy + end + end + + + context 'false at first then pass a controller string' do + it 'executes passed controller and default action' do + visit multiple_calls_2_main_index_path + + expect( + request['controller'] == 'OtherMain' && + request['action'] == 'multiple_calls_2' + ).to be_truthy + end + end + + + context 'controller at first then action' do + it 'executes the controller and action' do + visit multiple_calls_3_main_index_path + + expect( + request['controller'] == 'OtherMain' && + request['action'] == 'show' + ).to be_truthy + end + end + + + context 'controller#action at first then false' do + it 'does not execute any js' do + visit multiple_calls_4_main_index_path + expect(request).to be_nil + end + end + + + context 'false at first then true' do + it 'executes default controller#action' do + visit multiple_calls_5_main_index_path + + expect( + request['controller'] == 'Main' && + request['action'] == 'multiple_calls_5' + ).to be_truthy + end + end + + + + # # # Prevent Paloma