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
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
describe('Paloma.Router', function(){
|
3
|
-
var delimiter =
|
3
|
+
var delimiter = '/',
|
4
4
|
router = new Paloma.Router(delimiter);
|
5
5
|
|
6
6
|
describe('#parse(path)', function(){
|
@@ -9,7 +9,7 @@ describe('Paloma.Router', function(){
|
|
9
9
|
var result;
|
10
10
|
|
11
11
|
beforeEach(function(){
|
12
|
-
result = router.parse('Foo/Bar/Baz/Controller
|
12
|
+
result = router.parse('Foo/Bar/Baz/Controller');
|
13
13
|
});
|
14
14
|
|
15
15
|
it('returns the array of the namespaces', function(){
|
@@ -20,10 +20,6 @@ describe('Paloma.Router', function(){
|
|
20
20
|
expect(result.controller).toEqual('Controller');
|
21
21
|
});
|
22
22
|
|
23
|
-
it('returns the action', function(){
|
24
|
-
expect(result.action).toEqual('action');
|
25
|
-
});
|
26
|
-
|
27
23
|
it('returns the controllerPath', function(){
|
28
24
|
expect(result.controllerPath).toEqual(['Foo', 'Bar', 'Baz', 'Controller']);
|
29
25
|
});
|
@@ -35,7 +31,7 @@ describe('Paloma.Router', function(){
|
|
35
31
|
var result;
|
36
32
|
|
37
33
|
beforeEach(function(){
|
38
|
-
result = router.parse('Controller
|
34
|
+
result = router.parse('Controller');
|
39
35
|
});
|
40
36
|
|
41
37
|
it('returns an empty array of namespaces', function(){
|
@@ -46,10 +42,6 @@ describe('Paloma.Router', function(){
|
|
46
42
|
expect(result.controller).toEqual('Controller');
|
47
43
|
});
|
48
44
|
|
49
|
-
it('returns the action', function(){
|
50
|
-
expect(result.action).toEqual('action');
|
51
|
-
});
|
52
|
-
|
53
45
|
it('returns the controllerPath', function(){
|
54
46
|
expect(result.controllerPath).toEqual(['Controller']);
|
55
47
|
});
|
@@ -57,65 +49,4 @@ describe('Paloma.Router', function(){
|
|
57
49
|
|
58
50
|
});
|
59
51
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
describe('#controllerFor(resource)', function(){
|
65
|
-
|
66
|
-
var resource = 'MySuperResource';
|
67
|
-
|
68
|
-
describe('when no route is found', function(){
|
69
|
-
it('returns the resource', function(){
|
70
|
-
var controller = router.controllerFor(resource);
|
71
|
-
expect(controller).toEqual(resource);
|
72
|
-
});
|
73
|
-
});
|
74
|
-
|
75
|
-
|
76
|
-
describe('when route is found', function(){
|
77
|
-
it('returns the set controller', function(){
|
78
|
-
router.resource(resource, {controller: 'MyController'});
|
79
|
-
|
80
|
-
var controller = router.controllerFor(resource);
|
81
|
-
expect(controller).toEqual('MyController');
|
82
|
-
});
|
83
|
-
});
|
84
|
-
});
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
describe('#redirectFor(resource, action)', function(){
|
91
|
-
var router = new Paloma.Router(delimiter),
|
92
|
-
result;
|
93
|
-
|
94
|
-
describe('when has a redirect', function(){
|
95
|
-
beforeEach(function(){
|
96
|
-
router.redirect('Foo#edit', {to: 'Bar#revise'});
|
97
|
-
result = router.redirectFor('Foo', 'edit');
|
98
|
-
});
|
99
|
-
|
100
|
-
it('returns controller of the redirect', function(){
|
101
|
-
expect(result.controller).toEqual('Bar');
|
102
|
-
});
|
103
|
-
|
104
|
-
it('returns action of the redirect', function(){
|
105
|
-
expect(result.action).toEqual('revise');
|
106
|
-
});
|
107
|
-
});
|
108
|
-
|
109
|
-
|
110
|
-
describe('when has no redirect', function(){
|
111
|
-
var router = new Paloma.Router(delimiter),
|
112
|
-
result = router.redirectFor('Foo', 'edit');
|
113
|
-
|
114
|
-
it('returns null', function(){
|
115
|
-
expect(result).toBeNull();
|
116
|
-
});
|
117
|
-
});
|
118
|
-
|
119
|
-
});
|
120
|
-
|
121
52
|
});
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Paloma::Controller do
|
5
|
+
|
6
|
+
let(:controller){ Paloma::Controller.new }
|
7
|
+
|
8
|
+
|
9
|
+
before do
|
10
|
+
controller.resource = 'Test'
|
11
|
+
controller.action = 'new'
|
12
|
+
controller.params = {:x => 1, :y => 2}
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
shared_examples 'request is cleared' do
|
18
|
+
it 'assigns nil to resource' do
|
19
|
+
expect(controller.resource).to be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
it 'assigns nil to action' do
|
24
|
+
expect(controller.action).to be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
it 'assigns an empty hash to params' do
|
29
|
+
expect(controller.params).to be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
it 'returns true' do
|
34
|
+
expect(@return).to be_true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
shared_examples 'request is not cleared' do
|
41
|
+
it 'returns false' do
|
42
|
+
expect(@return).to be_false
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
it 'has a request' do
|
47
|
+
expect(controller.has_request?).to be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
describe '#clear_request' do
|
56
|
+
before { @return = controller.clear_request }
|
57
|
+
it_behaves_like 'request is cleared'
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
describe '#request' do
|
63
|
+
it 'returns a hash object' do
|
64
|
+
expect(controller.request).to be_an_instance_of Hash
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
it 'returns the current value of resource' do
|
69
|
+
expect(controller.request[:resource]).to eq 'Test'
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
it 'returns the current value of action' do
|
74
|
+
expect(controller.request[:action]).to eq 'new'
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
it 'returns the current value of params' do
|
79
|
+
expect(controller.request[:params]).to eq ({:x => 1, :y => 2})
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
describe '#has_request?' do
|
86
|
+
context 'when resource is nil' do
|
87
|
+
it 'returns false' do
|
88
|
+
controller.resource = nil
|
89
|
+
expect(controller.has_request?).to be_false
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
context 'when action is nil' do
|
95
|
+
it 'returns false' do
|
96
|
+
controller.action = nil
|
97
|
+
expect(controller.has_request?).to be_false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
context 'when resource and action is present' do
|
103
|
+
it 'returns true' do
|
104
|
+
expect(controller.has_request?).to be_true
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Paloma::Utilities do
|
5
|
+
|
6
|
+
let(:utilities){ Paloma::Utilities }
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
describe '.get_resource(path)' do
|
11
|
+
context 'when path is only 1 word' do
|
12
|
+
it 'returns the controller resource name' do
|
13
|
+
expect(utilities.get_resource('test')).to eq 'Test'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
context 'when path is more than 1 word' do
|
19
|
+
it 'returns the controller resource name' do
|
20
|
+
expect(utilities.get_resource('my_super_resources')).to eq 'MySuperResources'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
context 'when path has namespace' do
|
26
|
+
it 'returns "Namespace/Resource"' do
|
27
|
+
expect(utilities.get_resource('admin/my_mega_test')).to eq 'Admin/MyMegaTest'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
context 'when path has 2 or more namespace' do
|
33
|
+
it 'returns "Namespace1/NamespaceN/Resource' do
|
34
|
+
expect(utilities.get_resource('admin/test/my_resources')).to eq 'Admin/Test/MyResources'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
shared_examples 'resource is passed' do |resource|
|
44
|
+
it 'returns its returned as the resource' do
|
45
|
+
expect(route[:resource]).to eq resource
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
shared_examples 'action is passed' do |action|
|
51
|
+
it 'returns the action' do
|
52
|
+
expect(route[:action]).to eq action
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
shared_examples 'resource is not passed' do
|
58
|
+
it 'returns a nil resource' do
|
59
|
+
expect(route[:resource]).to be_nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
shared_examples 'action is not passed' do
|
65
|
+
it 'returns a nil action' do
|
66
|
+
expect(route[:action]).to be_nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
describe '.interpret_route(route_string)' do
|
75
|
+
context 'when route_string is empty' do
|
76
|
+
it 'raises an error' do
|
77
|
+
expect { utilities.interpret_route }.to raise_error 'Empty route cannot be interpreted'
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
context 'when route_string has a word' do
|
83
|
+
let(:route){ utilities.interpret_route 'MyResources' }
|
84
|
+
|
85
|
+
it_behaves_like 'resource is passed', 'MyResources'
|
86
|
+
it_behaves_like 'action is not passed'
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
context 'when route_string has a namespace' do
|
91
|
+
let(:route){ utilities.interpret_route 'Namespace/MyResource' }
|
92
|
+
|
93
|
+
it_behaves_like 'resource is passed', 'Namespace/MyResource'
|
94
|
+
it_behaves_like 'action is not passed'
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
context 'when route_string has an action' do
|
99
|
+
let(:route){ utilities.interpret_route 'Namespace/MyResources#action' }
|
100
|
+
|
101
|
+
it_behaves_like 'resource is passed', 'Namespace/MyResources'
|
102
|
+
it_behaves_like 'action is passed', 'action'
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
context 'when route_string has action only' do
|
107
|
+
let(:route){ utilities.interpret_route '#edit' }
|
108
|
+
|
109
|
+
it_behaves_like 'resource is not passed'
|
110
|
+
it_behaves_like 'action is passed', 'edit'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
@@ -1,62 +1,32 @@
|
|
1
1
|
(function(Paloma){
|
2
2
|
|
3
3
|
var Engine = function(config){
|
4
|
-
this.router = config.router;
|
5
4
|
this.factory = config.factory;
|
6
|
-
this.
|
5
|
+
this.lastRequest = null;
|
7
6
|
};
|
8
7
|
|
9
8
|
|
10
|
-
Engine.prototype.
|
11
|
-
|
12
|
-
var request = this.requests[i];
|
13
|
-
|
14
|
-
console.log('Processing request with params:');
|
15
|
-
console.log(request.params);
|
16
|
-
|
17
|
-
var controllerName = this.router.controllerFor(request.resource),
|
18
|
-
action = request.action,
|
19
|
-
redirect = this.router.redirectFor(request.resource, action);
|
20
|
-
|
21
|
-
if (redirect){
|
22
|
-
controllerName = redirect['controller'];
|
23
|
-
action = redirect['action'];
|
24
|
-
}
|
25
|
-
|
26
|
-
console.log('mapping <' + request.resource + '> to controller <' + controllerName + '>');
|
27
|
-
console.log('mapping action <' + request.action + '> to controller action <' + action + '>');
|
28
|
-
|
29
|
-
var Controller = this.factory.get(controllerName);
|
30
|
-
|
31
|
-
if (!Controller){
|
32
|
-
console.warn('Paloma: undefined controller -> ' + controllerName);
|
33
|
-
continue;
|
34
|
-
}
|
35
|
-
|
36
|
-
var controller = new Controller(request.params);
|
9
|
+
Engine.prototype.request = function(resource, action, params){
|
10
|
+
this.lastRequest = null;
|
37
11
|
|
38
|
-
|
39
|
-
console.warn('Paloma: undefined action <' + action +
|
40
|
-
'> for <' + controllerName + '> controller');
|
41
|
-
continue;
|
42
|
-
}
|
12
|
+
var Controller = this.factory.get(resource);
|
43
13
|
|
44
|
-
|
14
|
+
if (!Controller){
|
15
|
+
return Paloma.warn('Paloma: undefined controller -> ' + resource);
|
45
16
|
}
|
46
17
|
|
47
|
-
|
48
|
-
};
|
49
|
-
|
18
|
+
var controller = new Controller(params);
|
50
19
|
|
51
|
-
|
52
|
-
|
53
|
-
|
20
|
+
if (!controller[action]){
|
21
|
+
return Paloma.warn('Paloma: undefined action <' + action +
|
22
|
+
'> for <' + resource + '> controller');
|
23
|
+
}
|
54
24
|
|
25
|
+
Paloma.log('Paloma: Execute ' + resource + '#' + action + ' with');
|
26
|
+
Paloma.log(params);
|
55
27
|
|
56
|
-
|
57
|
-
this.
|
58
|
-
action: action,
|
59
|
-
params: params});
|
28
|
+
controller[action]();
|
29
|
+
this.lastRequest = {controller: resource, action: action, params: params};
|
60
30
|
};
|
61
31
|
|
62
32
|
|
@@ -1 +1,17 @@
|
|
1
|
-
window.Paloma = window.Paloma || {};
|
1
|
+
window.Paloma = window.Paloma || {};
|
2
|
+
|
3
|
+
//
|
4
|
+
// Do nothing if there is no available console.
|
5
|
+
//
|
6
|
+
if (!window['console']){ Paloma.log = Paloma.warn = function(msg){}; }
|
7
|
+
else {
|
8
|
+
Paloma.warn = function(msg){
|
9
|
+
if(Paloma.env != 'development') return;
|
10
|
+
console.warn(msg);
|
11
|
+
};
|
12
|
+
|
13
|
+
Paloma.log = function(msg){
|
14
|
+
if(Paloma.env != 'development') return;
|
15
|
+
console.log(msg);
|
16
|
+
};
|
17
|
+
}
|
@@ -1,17 +1,19 @@
|
|
1
1
|
(function(Paloma){
|
2
2
|
|
3
|
+
Paloma._router = new Paloma.Router('/');
|
4
|
+
Paloma._controllerFactory = new Paloma.ControllerFactory(Paloma._router);
|
3
5
|
|
4
|
-
|
5
|
-
Paloma
|
6
|
-
|
7
|
-
|
6
|
+
//
|
7
|
+
// Declare Paloma controllers using this method.
|
8
|
+
// Will return a new constructor if the no controller with the passed name
|
9
|
+
// is found, else it will just return the current constructor.
|
10
|
+
//
|
8
11
|
Paloma.controller = function(name){
|
9
12
|
return Paloma._controllerFactory.get(name) ||
|
10
13
|
Paloma._controllerFactory.make(name);
|
11
14
|
};
|
12
15
|
|
13
16
|
|
14
|
-
Paloma.engine = new Paloma.Engine({
|
15
|
-
factory: Paloma._controllerFactory});
|
17
|
+
Paloma.engine = new Paloma.Engine({factory: Paloma._controllerFactory});
|
16
18
|
|
17
19
|
})(window.Paloma);
|