paloma 3.0.2 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +8 -0
- data/README.md +151 -63
- data/app/views/paloma/_hook.html.erb +9 -11
- data/lib/paloma.rb +2 -0
- data/lib/paloma/action_controller_extension.rb +84 -15
- data/lib/paloma/controller.rb +38 -0
- data/lib/paloma/utilities.rb +23 -0
- data/paloma.gemspec +1 -1
- data/test_app/app/assets/javascripts/application.js +14 -61
- data/test_app/app/controllers/admin/foos_controller.rb +29 -0
- data/test_app/app/controllers/main_controller.rb +42 -0
- data/test_app/config/routes.rb +4 -6
- data/test_app/spec/integration/advanced_spec.rb +60 -0
- data/test_app/spec/integration/basic_spec.rb +122 -0
- data/test_app/spec/javascripts/router_spec.js +3 -72
- data/test_app/spec/spec_helper.rb +5 -0
- data/test_app/spec/units/controller_spec.rb +109 -0
- data/test_app/spec/units/utilities_spec.rb +114 -0
- data/vendor/assets/javascripts/paloma/engine.js +15 -45
- data/vendor/assets/javascripts/paloma/init.js +17 -1
- data/vendor/assets/javascripts/paloma/paloma.js +8 -6
- data/vendor/assets/javascripts/paloma/router.js +6 -47
- metadata +135 -103
- checksums.yaml +0 -15
- data/test_app/app/controllers/admin/bar_controller.rb +0 -7
- data/test_app/app/controllers/foo_controller.rb +0 -19
- data/test_app/app/controllers/multiple_names_controller.rb +0 -7
- data/test_app/spec/integration/main_spec.rb +0 -108
@@ -0,0 +1,38 @@
|
|
1
|
+
module Paloma
|
2
|
+
class Controller
|
3
|
+
|
4
|
+
attr_accessor :resource, :action, :params
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
self.clear_request
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def clear_request
|
14
|
+
self.resource = nil
|
15
|
+
self.action = nil
|
16
|
+
self.params = {}
|
17
|
+
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def request
|
23
|
+
{:resource => self.resource, :action => self.action, :params => self.params}
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def has_request?
|
28
|
+
self.resource.present? && self.action.present?
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def has_no_request?
|
33
|
+
!self.has_request?
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Paloma
|
2
|
+
class Utilities
|
3
|
+
|
4
|
+
def self.get_resource controller_path
|
5
|
+
controller_path.split('/').map(&:titleize).join('/').gsub(' ', '')
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
def self.interpret_route route_string = nil
|
10
|
+
raise 'Empty route cannot be interpreted' if route_string.blank?
|
11
|
+
|
12
|
+
parts = route_string.split '#'
|
13
|
+
|
14
|
+
resource = parts.first
|
15
|
+
resource = resource.blank? ? nil : resource
|
16
|
+
|
17
|
+
action = parts.length != 1 ? parts.last : nil
|
18
|
+
|
19
|
+
{:resource => resource, :action => action}
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/paloma.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'paloma'
|
3
|
-
s.version = '
|
3
|
+
s.version = '4.0.0'
|
4
4
|
s.summary = "Provides an easy way to execute page-specific javascript for Rails."
|
5
5
|
s.description = "Page-specific javascript for Rails done right"
|
6
6
|
s.authors = ['Karl Paragua']
|
@@ -16,78 +16,31 @@
|
|
16
16
|
//= require_tree .
|
17
17
|
|
18
18
|
|
19
|
-
// Will be manipulated by Paloma controllers.
|
20
|
-
window.called = [];
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
//
|
27
|
-
//
|
28
|
-
// Routes
|
29
|
-
//
|
30
|
-
//
|
31
|
-
var router = Paloma.router;
|
32
|
-
|
33
|
-
router.resource('Foo', {controller: 'MyFoo'});
|
34
|
-
router.redirect('Foo#new', {to: 'AnotherFoo#build'});
|
35
|
-
|
36
|
-
|
37
|
-
|
38
19
|
|
39
20
|
//
|
40
21
|
//
|
41
22
|
// Controllers
|
42
23
|
//
|
43
24
|
//
|
44
|
-
var Main = Paloma.controller('Main');
|
45
|
-
|
46
|
-
Main.prototype.index = function(){
|
47
|
-
window.called.push('Main#index');
|
48
|
-
};
|
49
25
|
|
26
|
+
var Main = Paloma.controller('Main');
|
27
|
+
Main.prototype.index = function(){};
|
28
|
+
Main.prototype.otherAction = function(){};
|
29
|
+
Main.prototype.prevent = function(){};
|
30
|
+
Main.prototype.basic_params = function(){};
|
50
31
|
|
51
32
|
|
52
|
-
var
|
53
|
-
|
54
|
-
|
55
|
-
window.called.push('MyFoo#index');
|
56
|
-
};
|
57
|
-
|
58
|
-
|
59
|
-
MyFoo.prototype.show = function(){
|
60
|
-
window.called.push('MyFoo#show');
|
61
|
-
window.parameter = this.params.parameter;
|
62
|
-
};
|
63
|
-
|
64
|
-
|
65
|
-
MyFoo.prototype.edit = function(){
|
66
|
-
window.called.push('MyFoo#edit');
|
67
|
-
};
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
var AnotherFoo = Paloma.controller('AnotherFoo');
|
72
|
-
|
73
|
-
AnotherFoo.prototype.build = function(){
|
74
|
-
window.called.push('AnotherFoo#build');
|
75
|
-
};
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
var Bar = Paloma.controller('Admin/Bar');
|
80
|
-
|
81
|
-
Bar.prototype.show = function(){
|
82
|
-
window.called.push('Admin/Bar#show');
|
83
|
-
};
|
84
|
-
|
33
|
+
var OtherMain = Paloma.controller('OtherMain');
|
34
|
+
OtherMain.prototype.show = function(){};
|
35
|
+
OtherMain.prototype.otherAction = function(){};
|
85
36
|
|
86
37
|
|
87
38
|
|
39
|
+
var Foos = Paloma.controller('Admin/Foos');
|
40
|
+
Foos.prototype.index = function(){};
|
41
|
+
Foos.prototype.otherAction = function(){};
|
88
42
|
|
89
|
-
var MultipleNames = Paloma.controller('MultipleNames');
|
90
43
|
|
91
|
-
|
92
|
-
|
93
|
-
};
|
44
|
+
var NotFoos = Paloma.controller('NotAdmin/Foos');
|
45
|
+
NotFoos.prototype.show = function(){};
|
46
|
+
NotFoos.prototype.otherAction = function(){};
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Admin::FoosController < ApplicationController
|
2
|
+
|
3
|
+
# Default behavior
|
4
|
+
def index
|
5
|
+
render :inline => 'Admin/Foos#index', :layout => 'application'
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
# Override controller
|
10
|
+
def show
|
11
|
+
js 'NotAdmin/Foos', :x => 99
|
12
|
+
render :inline => 'Admin/Foos#show', :layout => 'application'
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Override action
|
17
|
+
def new
|
18
|
+
js '#otherAction', :x => 99
|
19
|
+
render :inline => 'Admin/Foos#new', :layout => 'application'
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
# Override controller/action
|
24
|
+
def edit
|
25
|
+
js 'NotAdmin/Foos#otherAction', :x => 99
|
26
|
+
render :inline => 'Admin/Foos#edit', :layout => 'application'
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -1,10 +1,52 @@
|
|
1
1
|
class MainController < ApplicationController
|
2
2
|
|
3
|
+
# Default behavior
|
3
4
|
def index
|
4
5
|
render :inline => 'Main#index', :layout => 'application'
|
5
6
|
end
|
6
7
|
|
7
8
|
|
9
|
+
# Override controller
|
10
|
+
def show
|
11
|
+
js 'OtherMain', :x => 1
|
12
|
+
render :inline => 'Main#show', :layout => 'application'
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Override action
|
17
|
+
def new
|
18
|
+
js :otherAction, :x => 1
|
19
|
+
render :inline => 'Main#new', :layout => 'application'
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
# Override controller/action
|
24
|
+
def edit
|
25
|
+
js 'OtherMain#otherAction', :x => 1
|
26
|
+
render :inline => 'Main#edit', :layout => 'application'
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# Stop paloma from execution
|
31
|
+
def prevent
|
32
|
+
js false
|
33
|
+
render :inline => 'Main#prevent', :layout => 'application'
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def basic_params
|
38
|
+
js :x => 1, :y => 2
|
39
|
+
render :inline => 'Main#basic_params', :layout => 'application'
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
#
|
47
|
+
# Non-HTML response
|
48
|
+
#
|
49
|
+
|
8
50
|
def json_response
|
9
51
|
render :json => {:x => 1}
|
10
52
|
end
|
data/test_app/config/routes.rb
CHANGED
@@ -5,7 +5,9 @@ TestApp::Application.routes.draw do
|
|
5
5
|
root :to => 'main#index'
|
6
6
|
|
7
7
|
resources :main, :controller => 'Main' do
|
8
|
-
|
8
|
+
collection do
|
9
|
+
get :prevent
|
10
|
+
get :basic_params
|
9
11
|
get :json_response
|
10
12
|
get :js_response
|
11
13
|
get :xml_response
|
@@ -13,12 +15,8 @@ TestApp::Application.routes.draw do
|
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
|
-
resources :foo, :controller => 'Foo'
|
17
18
|
|
18
19
|
namespace :admin do
|
19
|
-
resources :
|
20
|
+
resources :foos
|
20
21
|
end
|
21
|
-
|
22
|
-
|
23
|
-
resources :multiple_names
|
24
22
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
#
|
4
|
+
#
|
5
|
+
# All examples are using namespaces
|
6
|
+
#
|
7
|
+
#
|
8
|
+
|
9
|
+
feature 'executing Paloma controller', :js => true do
|
10
|
+
|
11
|
+
|
12
|
+
context 'default behavior' do
|
13
|
+
it 'executes the same namespace/controller/action' do
|
14
|
+
visit admin_foos_path
|
15
|
+
|
16
|
+
expect(request).to eq({
|
17
|
+
'controller' => 'Admin/Foos',
|
18
|
+
'action' => 'index',
|
19
|
+
'params' => {}})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
context 'override default controller' do
|
25
|
+
it 'executes the specified controller' do
|
26
|
+
visit admin_foo_path(1)
|
27
|
+
|
28
|
+
expect(request).to eq({
|
29
|
+
'controller' => 'NotAdmin/Foos',
|
30
|
+
'action' => 'show',
|
31
|
+
'params' => {'x' => 99}})
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
context 'override default action' do
|
37
|
+
it 'executes the specified action' do
|
38
|
+
visit new_admin_foo_path
|
39
|
+
|
40
|
+
expect(request).to eq({
|
41
|
+
'controller' => 'Admin/Foos',
|
42
|
+
'action' => 'otherAction',
|
43
|
+
'params' => {'x' => 99}})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
context 'override default controller/action' do
|
49
|
+
it 'executes the specified controller/action' do
|
50
|
+
visit edit_admin_foo_path(1)
|
51
|
+
|
52
|
+
expect(request).to eq({
|
53
|
+
'controller' => 'NotAdmin/Foos',
|
54
|
+
'action' => 'otherAction',
|
55
|
+
'params' => {'x' => 99}})
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
#
|
4
|
+
#
|
5
|
+
# All examples are not using namespaces.
|
6
|
+
#
|
7
|
+
#
|
8
|
+
|
9
|
+
feature 'executing Paloma controller', :js => true do
|
10
|
+
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# Basic
|
14
|
+
# All except for basic_params and index action will pass :x => 1 parameter
|
15
|
+
#
|
16
|
+
#
|
17
|
+
|
18
|
+
context 'default behavior' do
|
19
|
+
it 'executes the same controller/action' do
|
20
|
+
visit main_index_path
|
21
|
+
|
22
|
+
expect(request).to eq({
|
23
|
+
'controller' => 'Main',
|
24
|
+
'action' => 'index',
|
25
|
+
'params' => {}})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
context 'override default controller' do
|
31
|
+
it 'executes the specified controller' do
|
32
|
+
visit main_path(1)
|
33
|
+
|
34
|
+
expect(request).to eq({
|
35
|
+
'controller' => 'OtherMain',
|
36
|
+
'action' => 'show',
|
37
|
+
'params' => {'x' => 1}})
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
context 'override default action' do
|
43
|
+
it 'executes the specified action' do
|
44
|
+
visit new_main_path
|
45
|
+
|
46
|
+
expect(request).to eq({
|
47
|
+
'controller' => 'Main',
|
48
|
+
'action' => 'otherAction',
|
49
|
+
'params' => {'x' => 1}})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
context 'override default controller/action' do
|
55
|
+
it 'executes the specified controller/action' do
|
56
|
+
visit edit_main_path(1)
|
57
|
+
|
58
|
+
expect(request).to eq({
|
59
|
+
'controller' => 'OtherMain',
|
60
|
+
'action' => 'otherAction',
|
61
|
+
'params' => {'x' => 1}})
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
context 'parameter passed' do
|
67
|
+
it 'passes the parameter' do
|
68
|
+
visit basic_params_main_index_path
|
69
|
+
|
70
|
+
expect(request['params']).to eq({'x' => 1, 'y' => 2})
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
#
|
79
|
+
#
|
80
|
+
# Prevent Paloma
|
81
|
+
#
|
82
|
+
#
|
83
|
+
|
84
|
+
shared_examples 'no paloma' do
|
85
|
+
it 'does not add paloma hook' do
|
86
|
+
expect(page.has_selector?('.js-paloma-hook')).to be_false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
context 'js(false)' do
|
92
|
+
before { visit prevent_main_index_path }
|
93
|
+
|
94
|
+
include_examples 'no paloma'
|
95
|
+
|
96
|
+
it 'prevents execution of Paloma controller' do
|
97
|
+
expect(request).to be_nil
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'json response' do
|
102
|
+
before { visit json_response_main_index_path }
|
103
|
+
include_examples 'no paloma'
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'js response' do
|
107
|
+
before { visit js_response_main_index_path }
|
108
|
+
include_examples 'no paloma'
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'xml response' do
|
112
|
+
before { visit xml_response_main_index_path }
|
113
|
+
include_examples 'no paloma'
|
114
|
+
end
|
115
|
+
|
116
|
+
context 'file response' do
|
117
|
+
before { visit file_response_main_index_path }
|
118
|
+
include_examples 'no paloma'
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
end
|