Permalink
Browse files

When using the path parameter, nested subroutes should be prefixed by…

… the path, not the resource's name
  • Loading branch information...
1 parent 20a03ce commit 30bd0554bd85090130a4f2c3f5eccae7cfae6fb4 @olalonde committed Oct 8, 2012
Showing with 21 additions and 5 deletions.
  1. +1 −1 README.md
  2. +20 −4 lib/railway_routes.js
View
@@ -76,7 +76,7 @@ Will generate the following routes:
Singleton resources can also have nested resources. For example:
map.resource('account', function(account) {
- map.resources('posts');
+ account.resources('posts');
});
## Example app
View
@@ -100,13 +100,16 @@ Map.prototype.root = function (handler, middleware, options) {
action = handler.split('#')[1];
}
+ console.log('subpath: ' + subpath);
+ console.log('globPath: ' + this.globPath);
+
var path;
if (typeof subpath === 'string') {
path = this.globPath + subpath.replace(/^\/|\/$/, '');
} else { // regex???
path = subpath;
}
-
+ console.log('path: ' + path);
// only accept functions in before filter when it's an array
if (middleware instanceof Array) {
var before_filter_functions = middleware.filter(function(filter) {
@@ -224,14 +227,27 @@ Map.prototype.resources = function (name, params, actions) {
actions = params;
params = {};
}
+
+ // If resource uses the path param, it's subroutes should be
+ // prefixed by path, not the resource's name
+ // i.e.:
+ // map.resource('users', {path: ':username'}, function(user) {
+ // user.resources('posts);
+ // });
+ //
+ // /:username/posts.:format?
+ // /:username/posts/new.:format?
+ // etc.
+ var prefix = params.path ? params.path : name;
+
// we have bunch of actions here, will create routes for them
var activeRoutes = getActiveRoutes(params);
// but first, create subroutes
if (typeof actions == 'function') {
if (params.singleton)
- this.subroutes(name, actions); // singletons don't need to specify an id
+ this.subroutes(prefix, actions); // singletons don't need to specify an id
else
- this.subroutes(name + '/:' + (singularize(name) || name) + '_id', actions);
+ this.subroutes(prefix + '/:' + (singularize(name) || name) + '_id', actions);
}
// now let's walk through action routes
for (var action in activeRoutes) {
@@ -263,7 +279,7 @@ Map.prototype.resources = function (name, params, actions) {
// params.path setting allows to override common path component
var effectivePath = (params.path || name) + path;
- var controller = name;
+ var controller = params.controller || name;
// and call map.{get|post|update|delete}
// with the path, controller, middleware and options

0 comments on commit 30bd055

Please sign in to comment.