Permalink
Browse files

Correct implementation for collection subroutes

  • Loading branch information...
1 parent cdd1caf commit aeb146c09fb215fff47b1fb15a7b49feab599412 @1602 committed Feb 14, 2013
Showing with 40 additions and 12 deletions.
  1. +19 −12 lib/railway_routes.js
  2. +21 −0 test/railway_routes_test.js
View
@@ -100,13 +100,6 @@ Map.prototype.root = function (handler, middleware, options) {
action = handler.split('#')[1];
}
- var path;
- if (typeof subpath === 'string') {
- path = this.globPath + subpath.replace(/^\/|\/$/, '');
- } else { // regex???
- path = subpath;
- }
-
// only accept functions in before filter when it's an array
if (middleware instanceof Array) {
var before_filter_functions = middleware.filter(function(filter) {
@@ -124,7 +117,20 @@ Map.prototype.root = function (handler, middleware, options) {
options = {};
}
- path = options.collection ? path.replace(/\/:[^\/\:]+_id(\/[^\:]+)$/, '$1') : path;
+ var path;
+ if (typeof subpath === 'string') {
+ var prefix = '';
+ if (options.collection) {
+ prefix = this.globPath.replace(/:[^\/]*_id\/$/, '');
+ } else {
+ prefix = this.globPath;
+ }
+ path = prefix + subpath.replace(/^\/|\/$/, '');
+ } else { // regex???
+ path = subpath;
+ }
+
+ // path = options.collection ? path.replace(/\/:[^\/\:]+_id(\/[^\:]+)$/, '$1') : path;
var args = [path];
if (middleware) {
@@ -269,10 +275,11 @@ Map.prototype.resources = function (name, params, actions) {
var activeRoutes = getActiveRoutes(params);
// but first, create subroutes
if (typeof actions == 'function') {
- if (params.singleton)
- this.subroutes(prefix, actions); // singletons don't need to specify an id
- else
- this.subroutes(prefix + '/:' + (singularize(name) || name) + '_id', actions);
+ if (params.singleton) {
+ this.subroutes(prefix, actions); // singletons don't need to specify an id
+ } else {
+ this.subroutes(prefix + '/:' + (singularize(name) || name) + '_id', actions);
+ }
}
// now let's walk through action routes
for (var action in activeRoutes) {
@@ -211,3 +211,24 @@ it('should allow to specify manual helper name for resources', function (test) {
]);
test.done();
});
+
+it('should only replace last collection_id when collection: true', function (test) {
+
+ var paths = [];
+ var map = new routes.Map(fakeApp(paths), fakeBridge());
+ map.resources('posts', function (post) {
+ post.resources('comments', function (comment) {
+ comment.get('report', 'comments#report');
+ comment.get('tag-with/:tag', 'comments#tag');
+ comment.get('reload', 'comments#reload', {collection: true});
+ comment.get('destroyAll/:filter', 'comments#destroyAll', {collection: true});
+ });
+ });
+ test.deepEqual(paths.slice(0, 4), [
+ [ 'GET', '/posts/:post_id/comments/:comment_id/report', 'comments#report' ],
+ [ 'GET', '/posts/:post_id/comments/:comment_id/tag-with/:tag', 'comments#tag' ],
+ [ 'GET', '/posts/:post_id/comments/reload', 'comments#reload' ],
+ [ 'GET', '/posts/:post_id/comments/destroyAll/:filter', 'comments#destroyAll' ]
+ ]);
+ test.done();
+});

0 comments on commit aeb146c

Please sign in to comment.