From 229986250069d1065b459d2e3a6a2d337a050b3e Mon Sep 17 00:00:00 2001 From: kbparauga Date: Fri, 28 Aug 2015 14:13:13 +0800 Subject: [PATCH 1/5] Handle js(true) and raise error on invalid argument --- lib/paloma/action_controller_extension.rb | 30 ++++++++++++++++++--- test_app/app/controllers/main_controller.rb | 3 ++- test_app/spec/units/controller_spec.rb | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index da919e4..c405d24 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -67,6 +67,9 @@ module Paloma # # def js path_or_options, params = {} + puts '-----------------------------------------------------------------' + puts "js #{path_or_options.inspect}, #{params.inspect}" + return self.paloma.clear_request if !path_or_options self.paloma.params.merge! params || {} @@ -80,8 +83,8 @@ module Paloma # if path_or_options.is_a? String route = ::Paloma::Utilities.interpret_route path_or_options - self.paloma.resource = route[:resource] unless route[:resource].blank? - self.paloma.action = route[:action] unless route[:action].blank? + self.paloma.resource = route[:resource] || self.default_resource + self.paloma.action = route[:action] || self.default_action # :action elsif path_or_options.is_a? Symbol @@ -91,7 +94,16 @@ 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 + raise "Paloma: Invalid argument (#{path_or_options}) for js method" end + + puts "Paloma.request = #{self.paloma.request.inspect}" + puts "Paloma has request? #{self.paloma.has_request?}" end @@ -101,8 +113,8 @@ module Paloma # Keeps track of what Rails controller/action is executed. # def track_paloma_request - self.paloma.resource ||= ::Paloma::Utilities.get_resource controller_path - self.paloma.action ||= self.action_name + self.paloma.resource ||= self.default_resource + self.paloma.action ||= self.default_action end @@ -134,6 +146,16 @@ module Paloma @paloma ||= ::Paloma::Controller.new end + + def default_resource + ::Paloma::Utilities.get_resource self.controller_path + end + + + def default_action + self.action_name + end + end diff --git a/test_app/app/controllers/main_controller.rb b/test_app/app/controllers/main_controller.rb index cc74637..529fec7 100644 --- a/test_app/app/controllers/main_controller.rb +++ b/test_app/app/controllers/main_controller.rb @@ -2,13 +2,14 @@ class MainController < ApplicationController # Default behavior def index + js false + js 11231242 render :inline => 'Main#index', :layout => 'application' end # Override controller def show - js false js 'OtherMain', :x => 1 render :inline => 'Main#show', :layout => 'application' end diff --git a/test_app/spec/units/controller_spec.rb b/test_app/spec/units/controller_spec.rb index be15d12..f93bc8a 100644 --- a/test_app/spec/units/controller_spec.rb +++ b/test_app/spec/units/controller_spec.rb @@ -106,4 +106,4 @@ describe Paloma::Controller do end end -end \ No newline at end of file +end From fff34c386f911a508a5d99acc59239acf1e4c471 Mon Sep 17 00:00:00 2001 From: kbparauga Date: Fri, 28 Aug 2015 14:15:58 +0800 Subject: [PATCH 2/5] Remove comments --- lib/paloma/action_controller_extension.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index c405d24..7a0bde6 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -67,9 +67,6 @@ module Paloma # # def js path_or_options, params = {} - puts '-----------------------------------------------------------------' - puts "js #{path_or_options.inspect}, #{params.inspect}" - return self.paloma.clear_request if !path_or_options self.paloma.params.merge! params || {} @@ -101,9 +98,6 @@ module Paloma else raise "Paloma: Invalid argument (#{path_or_options}) for js method" end - - puts "Paloma.request = #{self.paloma.request.inspect}" - puts "Paloma has request? #{self.paloma.has_request?}" end From 8d29f1abb4cd41ac9da3578d436dd491c20acba0 Mon Sep 17 00:00:00 2001 From: kbparauga Date: Fri, 28 Aug 2015 14:18:59 +0800 Subject: [PATCH 3/5] add comment --- lib/paloma/action_controller_extension.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index 7a0bde6..0b93397 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -64,6 +64,7 @@ module Paloma # js '#action', {params} # js :action, {params} # js :param_1 => 1, :param_2 => 2 + # js true # # def js path_or_options, params = {} From ead698874f0f8743d4e2397015e4a6ef3139c2df Mon Sep 17 00:00:00 2001 From: kbparauga Date: Fri, 28 Aug 2015 14:19:42 +0800 Subject: [PATCH 4/5] another comment --- lib/paloma/action_controller_extension.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index 0b93397..0a509e3 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -65,6 +65,7 @@ module Paloma # js :action, {params} # js :param_1 => 1, :param_2 => 2 # js true + # js false # # def js path_or_options, params = {} From 7543d985df07312bd9be15b49f48b4abe779c26f Mon Sep 17 00:00:00 2001 From: kbparauga Date: Fri, 28 Aug 2015 15:13:28 +0800 Subject: [PATCH 5/5] 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