Skip to content
Browse files

Make second param optional

  • Loading branch information...
1 parent 5fce61b commit b803acab0fcfd86bfff115a12ae11bb93aec0251 @1602 committed Feb 14, 2013
Showing with 39 additions and 3 deletions.
  1. +26 −3 lib/railway_routes.js
  2. +13 −0 test/railway_routes_test.js
View
29 lib/railway_routes.js
@@ -100,6 +100,23 @@ Map.prototype.root = function (handler, middleware, options) {
if (typeof handler === 'string') {
controller = handler.split('#')[0];
action = handler.split('#')[1];
+ } else if (handler instanceof Array) {
+ middleware = handler;
+ handler = null;
+ } else if (typeof handler === 'object') {
+ options = handler;
+ handler = null;
+ }
+
+ if (!controller && this.latestResource) {
+ controller = this.latestResource;
+ } else if (!controller) {
+ controller = 'controller';
+ }
+ if (!action && !subpath.match(/[:\/]/)) {
+ action = subpath;
+ } else if (!action) {
+ action = 'action';
}
// only accept functions in before filter when it's an array
@@ -132,16 +149,18 @@ Map.prototype.root = function (handler, middleware, options) {
path = subpath;
}
- // path = options.collection ? path.replace(/\/:[^\/\:]+_id(\/[^\:]+)$/, '$1') : path;
-
var args = [path];
if (middleware) {
args = args.concat(this.middlewareStack.concat(middleware));
}
args = args.concat(this.bridge(this.ns, controller, action, options));
+ var helperNameForDump = options.as || this.urlHelperName(path, action);
+ if (this.camelCaseHelperNames) {
+ helperNameForDump = camelize(helperNameForDump);
+ }
this.dump.push({
- helper: options.as || this.urlHelperName(path, action),
+ helper: helperNameForDump,
method: method,
path: path,
file: this.ns + controller,
@@ -283,11 +302,14 @@ Map.prototype.resources = function (name, params, actions) {
var activeRoutes = getActiveRoutes(params);
// but first, create subroutes
if (typeof actions == 'function') {
+ var oldResource = this.latestResource;
+ this.latestResource = name;
if (params.singleton) {
this.subroutes(prefix, actions); // singletons don't need to specify an id
} else {
this.subroutes(prefix + '/:' + (singularize(name) || name) + '_id', actions);
}
+ this.latestResource = oldResource;
}
// now let's walk through action routes
@@ -522,6 +544,7 @@ Map.prototype.collection = function (actions) {
};
function camelize(str) {
+ if (!str) return str;
return str.replace(/_+(.)/g, function (all, first) {
return first.toUpperCase();
});
View
13 test/railway_routes_test.js
@@ -266,3 +266,16 @@ it('should provide convenient api for collection-wide nested routes', function (
);
test.done();
});
+
+it('should be optional to specify controller#action', function (test) {
+ var paths = [];
+ var map = new routes.Map(fakeApp(paths), fakeBridge());
+ map.resources('posts', function (post) {
+ post.get('commentsCount');
+ post.get('destroyAll', {collection: true});
+ });
+ test.equal(paths[0][2], 'posts#commentsCount');
+ test.equal(paths[1][1], '/posts/destroyAll');
+ test.equal(paths[1][2], 'posts#destroyAll');
+ test.done();
+});

0 comments on commit b803aca

Please sign in to comment.
Something went wrong with that request. Please try again.