From 08cb1fb6c2ccca0389c636ffec78e1b8830498d4 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sun, 29 Jan 2012 20:17:04 +0400 Subject: [PATCH] Add fallback helper name for better support "as" option --- lib/railway_routes.js | 6 +++--- test/railway_routes_test.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/railway_routes.js b/lib/railway_routes.js index 3d7c0d6..9a2d457 100644 --- a/lib/railway_routes.js +++ b/lib/railway_routes.js @@ -103,20 +103,20 @@ Map.prototype.root = function (handler, middleware, options) { action: action }); - this.addPath(path, action); + this.addPath(path, action, options.as); this.app[method].apply(this.app, args); }; }); -Map.prototype.addPath = function (templatePath, action) { +Map.prototype.addPath = function (templatePath, action, fallbackHelperName) { if (templatePath instanceof RegExp) { // TODO: think about adding to `path_to` routes by reg ex return; } var paramsLength = templatePath.match(/\/:/g); 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 if (this.pathTo[helperName]) return; diff --git a/test/railway_routes_test.js b/test/railway_routes_test.js index 1ab255d..f7cd0d8 100644 --- a/test/railway_routes_test.js +++ b/test/railway_routes_test.js @@ -135,3 +135,14 @@ it('should handle root url', function (test) { 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(); +}); +