Skip to content

Commit

Permalink
Merge pull request geddy#103 from david-mitchell/master
Browse files Browse the repository at this point in the history
Use the router to generate redirect urls, when possible.
  • Loading branch information
mde committed Mar 13, 2012
2 parents c3b5119 + f4e4bc5 commit d65508c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/app.js
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion lib/base_controller.js
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions lib/routers/regexp_router.js
Expand Up @@ -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?
Expand All @@ -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;
Expand Down

0 comments on commit d65508c

Please sign in to comment.