Skip to content

Commit

Permalink
Add fallback helper name for better support "as" option
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Jan 29, 2012
1 parent c2fdf6b commit 08cb1fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/railway_routes.js
Expand Up @@ -103,20 +103,20 @@ Map.prototype.root = function (handler, middleware, options) {
action: action action: action
}); });


this.addPath(path, action); this.addPath(path, action, options.as);


this.app[method].apply(this.app, args); this.app[method].apply(this.app, args);
}; };
}); });


Map.prototype.addPath = function (templatePath, action) { Map.prototype.addPath = function (templatePath, action, fallbackHelperName) {
if (templatePath instanceof RegExp) { if (templatePath instanceof RegExp) {
// TODO: think about adding to `path_to` routes by reg ex // TODO: think about adding to `path_to` routes by reg ex
return; return;
} }
var paramsLength = templatePath.match(/\/:/g); var paramsLength = templatePath.match(/\/:/g);
paramsLength = paramsLength === null ? 0 : paramsLength.length; paramsLength = paramsLength === null ? 0 : paramsLength.length;
var helperName = this.urlHelperName(templatePath, action); var helperName = this.urlHelperName(templatePath, action) || fallbackHelperName;


// already defined? not need to redefine // already defined? not need to redefine
if (this.pathTo[helperName]) return; if (this.pathTo[helperName]) return;
Expand Down
11 changes: 11 additions & 0 deletions test/railway_routes_test.js
Expand Up @@ -135,3 +135,14 @@ it('should handle root url', function (test) {
test.done(); test.done();
}); });


it('should allow to specify url helper name', function (test) {
var paths = [];
var map = new routes.Map(fakeApp(paths), fakeBridge());
map.get('/p/:id', 'posts#show', {as: 'post'});
test.deepEqual(paths, [
[ 'GET', '/p/:id', 'posts#show' ]
]);
test.equal(map.pathTo.post(1), '/p/1');
test.done();
});

0 comments on commit 08cb1fb

Please sign in to comment.