From f4e4bc5853e62a63ef64935944a3d844d9179bb6 Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Mon, 12 Mar 2012 20:24:39 -0700 Subject: [PATCH] Use the router to generate redirect urls, when possible. --- lib/app.js | 1 + lib/base_controller.js | 11 ++++++++++- lib/routers/regexp_router.js | 6 ++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/app.js b/lib/app.js index 2c715129..8a37a1fc 100644 --- a/lib/app.js +++ b/lib/app.js @@ -402,6 +402,7 @@ var App = function () { controller.accessTime = accessTime; if (typeof controller[params.action] == 'function') { + controller.app = self; controller.request = reqObj; controller.response = resp; controller.method = method; diff --git a/lib/base_controller.js b/lib/base_controller.js index ef5c69a4..adcb9f0d 100644 --- a/lib/base_controller.js +++ b/lib/base_controller.js @@ -595,7 +595,16 @@ controller.BaseController.prototype = new (function () { if (typeof target == 'string') { url = target; } - else { + else if (typeof this.app.router.url == 'function') { + if (this.name && !target.controller) + target.controller = this.name; + if (this.params.format && !target.format) + target.format = this.params.format; + + url = this.app.router.url(target); + } + + if (!url) { var contr = target.controller || this.name; var act = target.action; var ext = target.format || this.params.format; diff --git a/lib/routers/regexp_router.js b/lib/routers/regexp_router.js index 55098e0e..bbb8ba5a 100644 --- a/lib/routers/regexp_router.js +++ b/lib/routers/regexp_router.js @@ -199,6 +199,9 @@ var Router = function () { this.url = function (params) { var url = false; + // attempt the stringification with defaults mixed in + params = geddy.mixin({controller:'Application', action:'index' }, params); + // iterate through the existing routes until a suitable match is found for (var i in this.routes) { // do the controller & acton match? @@ -210,8 +213,7 @@ var Router = function () { this.routes[i].params.action != params.action) { continue; } - // attempt the stringification with defaults mixed in - params = geddy.mixin({controller:'Application', action:'index' }, params); + url = this.routes[i].stringify(params); if (url) { break;