Skip to content

Commit

Permalink
If no controller is given with the id or action urlFor will attem…
Browse files Browse the repository at this point in the history
…pt to get the current controller
  • Loading branch information
larzconwell committed Jun 26, 2012
1 parent 3b29a7e commit 3489b5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/template/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ var utils = require('../../utils')
// Copy a data object so it can be used in helpers
exports.registerData = {
name: 'registerData',
action: function(data) { Data = data; }
action: function(data) {
Data = data;
helperUtils.registerData(data);
}
}

/*
Expand Down Expand Up @@ -178,10 +181,10 @@ exports.scriptLink = {
* Generates a style element pointing to `source` and includes all the given `htmlOptions`
*
* Examples:
* styleLink('/js/script.js')
* styleLink('/css/styles.css')
* => '<link href="/css/style.css" />'
*
* styleLink('/js/script.js', { type: 'text/javascript' })
* styleLink('/css/styles.css', { type: 'text/javascript' })
* => '<link href="/css/style.css" rel="stylesheet" />'
*/
exports.styleLink = {
Expand Down Expand Up @@ -275,7 +278,7 @@ exports.imageLink = {
}

/*
* truncate(string<String>, options<Object>, callback[Function])
* truncate(string<String>, options<Integer/Object>, callback[Function])
*
* Truncates a given `string` after a specified `length` if `string` is longer than
* `length`. The last characters will be replaced with an `omission` for a total length not
Expand Down
20 changes: 19 additions & 1 deletion lib/template/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var utils = require('../../utils')
, Data;

exports.registerData = function(data) { Data = data; };

/*
* Helper utilities for dealing with HTML tags, and other attributes
Expand Down Expand Up @@ -229,6 +232,13 @@ exports.urls = {
}
}
if(options.id) {
// If no controller is given attempt to get the current one
if(!options.controller && Data.params.controller) {
var controller = Data.params.controller[0].toLowerCase() +
Data.params.controller.slice(1, Data.params.controller.length);

path += '/' + controller;
}
path += '/' + options.id;

if(!options.action) options.action = 'show';
Expand All @@ -240,9 +250,17 @@ exports.urls = {
if(options.action) {
path += '/';

// If no controller is given attempt to get the current one
if(!options.controller && Data.params.controller) {
var controller = Data.params.controller[0].toLowerCase() +
Data.params.controller.slice(1, Data.params.controller.length);

path += controller + '/';
}

if(options.action === 'show') {
if(options.trailingSlash) path += '/';
return path
return path;
} else {
path += options.action;
if(options.trailingSlash) path += '/';
Expand Down

0 comments on commit 3489b5c

Please sign in to comment.