Skip to content

Commit 41bda0b

Browse files
committed
Merge pull request #17 from CleverStack/feature/auto-routing
feature(autoRouting): Added ability to have auto routing and middleware when using autoRouting
2 parents 3e7a5c8 + 4ab2b34 commit 41bda0b

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

controller.js

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,78 @@
11
/* jshint node: true */
22
'use strict';
33

4-
var NoActionException = require('./exceptions/NoAction'),
5-
Class = require('uberclass');
4+
var NoActionException = require('./exceptions/NoAction')
5+
, Class = require( 'uberclass' )
6+
, path = require( 'path' );
67

78
module.exports = Class.extend(
89
/* @Static */
910
{
10-
actionsEnabled: true,
11+
route: false,
1112

12-
httpMethodsEnabled: true,
13+
autoRouting: true,
14+
15+
actionRouting: true,
16+
17+
restfulRouting: true,
1318

1419
attach: function() {
15-
return this.callback('newInstance');
20+
return this.callback( 'newInstance' );
21+
},
22+
23+
setup: function() {
24+
var self = this;
25+
if ( typeof injector !== 'undefined' && this.autoRouting !== false && this.route !== false ) {
26+
injector.inject( function( app ) {
27+
self.autoRoute( app );
28+
});
29+
}
30+
},
31+
32+
autoRoute: function( app ) {
33+
var middleware = [];
34+
35+
if ( this.autoRouting instanceof Array ) {
36+
this.autoRouting.forEach(function( mw ) {
37+
middleware.push( typeof mw === 'string' ? this.callback( mw ) : mw );
38+
}.bind( this ));
39+
}
40+
41+
middleware.push( this.attach() );
42+
43+
app.all.apply( app, [ [ this.route, ':action', ':id?' ].join( '/' ) ].concat( middleware ) ); // /example/:action/:id?
44+
app.all.apply( app, [ [ this.route, ':action?' ].join( '/' ) ].concat( middleware ) ); // /example/?:action?
45+
},
46+
47+
extend: function() {
48+
var extendingArgs = [].slice.call( arguments )
49+
, stack = new Error().stack.split( '\n' )
50+
, stack = stack.splice( 1, stack.length - 1)
51+
, extendingFilePath = false
52+
, extendingFileName = false
53+
, route = null;
54+
55+
while( stack.length > 0 && extendingFilePath === false ) {
56+
var file = stack.shift();
57+
if ( !/clever-controller/ig.test( file ) && !/uberclass/ig.test( file ) ) {
58+
if ( /\(([^\[\:]+).*\)/ig.test( file ) ) {
59+
extendingFilePath = RegExp.$1;
60+
extendingFileName = path.basename( extendingFilePath ).replace( /(controller)?.js/ig, '' ).toLowerCase();
61+
}
62+
}
63+
}
64+
65+
if ( [ '', 'controller' ].indexOf( extendingFileName ) === -1 && this.route === false ) {
66+
route = [ '/', extendingFileName ].join('');
67+
// debug( 'Binding automatic route name??' )
68+
if ( extendingArgs.length === 2 ) {
69+
extendingArgs[ 0 ].route = route;
70+
} else {
71+
extendingArgs.unshift({ route: route });
72+
}
73+
}
74+
75+
return this._super.apply( this, extendingArgs );
1676
}
1777
},
1878
/* @Prototype */
@@ -40,7 +100,7 @@ module.exports = Class.extend(
40100
this.res = res;
41101

42102
// Override routes where you attach specifically to a single route
43-
if (this.Class.actionsEnabled && /\//.test(this.req.url)) {
103+
if (this.Class.actionRouting && /\//.test(this.req.url)) {
44104
var parts = this.req.url.split('/');
45105
funcName = parts[parts.length-1];
46106

@@ -57,7 +117,7 @@ module.exports = Class.extend(
57117
}
58118

59119
// Route based on an action first if we can
60-
if (this.Class.actionsEnabled && typeof this.req.params !== 'undefined' && typeof this.req.params.action !== 'undefined') {
120+
if (this.Class.actionRouting && typeof this.req.params !== 'undefined' && typeof this.req.params.action !== 'undefined') {
61121
// Action Defined Routing
62122
if (isNaN(this.req.params.action)) {
63123
funcName = this.req.params.action + 'Action';
@@ -83,7 +143,7 @@ module.exports = Class.extend(
83143
}
84144

85145
// Route based on the HTTP Method, otherwise throw an exception
86-
if (this.Class.httpMethodsEnabled) {
146+
if (this.Class.restfulRouting) {
87147
if (this.isGet() && (this.req.params === undefined || this.req.params.id === undefined) && typeof this.listAction === 'function') {
88148
method = 'listAction';
89149
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "clever-controller",
33
"description": "Lightning-fast flexible controller prototype",
4-
"version": "1.0.4",
4+
"version": "1.1.0",
55
"private": false,
66
"repository":
77
{

test/controller.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ describe('Controller', function () {
131131
c.action.should.equal('removeAction');
132132
});
133133

134-
it('should not call action by req.params.action if .actionsEnabled is false', function () {
134+
it('should not call action by req.params.action if .actionRouting is false', function () {
135135
var Ctrl = Controller.extend({
136-
actionsEnabled: false
136+
actionRouting: false
137137
}, {
138138
getAction: null,
139139
removeAction: sinon.spy()
@@ -152,9 +152,9 @@ describe('Controller', function () {
152152
next.called.should.be.true;
153153
});
154154

155-
it('should not call action by HTTP method if .httpMethodsEnabled is false', function () {
155+
it('should not call action by HTTP method if .restfulRouting is false', function () {
156156
var Ctrl = Controller.extend({
157-
httpMethodsEnabled: false
157+
restfulRouting: false
158158
}, {
159159
getAction: sinon.spy()
160160
});
@@ -177,7 +177,7 @@ describe('Controller', function () {
177177

178178
it('should call .action if parses URL with query string', function(){
179179
var Ctrl = Controller.extend({
180-
actionsEnabled: true
180+
actionRouting: true
181181
},{
182182
profileAction: sinon.spy()
183183
});

0 commit comments

Comments
 (0)