1
0
Fork 0
mirror of https://github.com/kbparagua/paloma synced 2023-03-27 23:21:17 -04:00

Merge pull request #81 from kbparagua/bugfix/multiple-js-call

Multiple JS calls
This commit is contained in:
Karl Bryan Paragua 2015-08-28 15:31:08 +08:00
commit b380289588
7 changed files with 150 additions and 7 deletions

View file

@ -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}
@ -64,6 +78,8 @@ module Paloma
# js '#action', {params}
# js :action, {params}
# js :param_1 => 1, :param_2 => 2
# js true
# js false
#
#
def js path_or_options, params = {}
@ -80,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] unless route[:resource].blank?
self.paloma.action = route[:action] unless route[:action].blank?
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
@ -91,7 +107,12 @@ module Paloma
elsif path_or_options.is_a? Hash
self.paloma.params.merge! path_or_options || {}
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
@ -101,8 +122,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 +155,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

View file

@ -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');

View file

@ -8,7 +8,6 @@ class MainController < ApplicationController
# Override controller
def show
js false
js 'OtherMain', :x => 1
render :inline => 'Main#show', :layout => 'application'
end
@ -28,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

View file

@ -17,6 +17,11 @@
<li><%= link_to 'Main#edit', edit_main_path(1) %></li>
<li><%= link_to 'Main#prevent', prevent_main_index_path %></li>
<li><%= link_to 'Main#basic_params', basic_params_main_index_path %></li>
<li><%= link_to 'Main#multiple_calls_1', multiple_calls_1_main_index_path %></li>
<li><%= link_to 'Main#multiple_calls_2', multiple_calls_2_main_index_path %></li>
<li><%= link_to 'Main#multiple_calls_3', multiple_calls_3_main_index_path %></li>
<li><%= link_to 'Main#multiple_calls_4', multiple_calls_4_main_index_path %></li>
<li><%= link_to 'Main#multiple_calls_5', multiple_calls_5_main_index_path %></li>
<li><%= link_to 'Main#xml_response', xml_response_main_index_path %></li>
<li><%= link_to 'Main#file_response', file_response_main_index_path %></li>
<li><%= link_to 'Main#ajax', ajax_main_index_path, :id => 'js-ajax-link' %></li>

View file

@ -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

View file

@ -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

View file

@ -106,4 +106,4 @@ describe Paloma::Controller do
end
end
end
end