Skip to content
Browse files

Add onlyPath option to model-aware urlFor.

  • Loading branch information...
1 parent b2405c3 commit 0f7dc5a539429debb4c39b1b0e20a25ff52f9ffe @jaredhanson committed Apr 14, 2012
Showing with 14 additions and 2 deletions.
  1. +4 −1 lib/locomotive/helpers/dynamic/url.js
  2. +10 −1 test/helpers/dynamic/url-test.js
View
5 lib/locomotive/helpers/dynamic/url.js
@@ -53,10 +53,13 @@ function urlFor(req, res) {
// If the argument is a record from a datastore, find and call the
// corresponding named routing helper.
if (options.constructor.name !== 'Object') {
+ var opts = arguments[1] || {};
var recordof = app._recordOf(options);
if (!recordof) { throw new Error('Unable to determine record of ' + options); }
- // TODO: Should this respect the pathOnly option?
var helperName = utils.helperize(recordof, 'URL');
+ if (opts.onlyPath) {
+ helperName = utils.helperize(recordof, 'Path');
+ }
var helperFn = this[helperName];
if (!helperFn || (typeof helperFn !== 'function')) { throw new Error('No routing helper named ' + helperName); }
return helperFn.call(this, options);
View
11 test/helpers/dynamic/url-test.js
@@ -22,7 +22,10 @@ function MockLocomotive() {
function animalURL(obj) {
return this.urlFor({ controller: 'AnimalsController', action: 'show', id: obj.id });
}
- this.routingHelpers = { animalURL: animalURL };
+ function animalPath(obj) {
+ return this.urlFor({ controller: 'AnimalsController', action: 'show', id: obj.id, onlyPath: true });
+ }
+ this.routingHelpers = { animalURL: animalURL, animalPath: animalPath };
}
MockLocomotive.prototype._recordOf = function(obj) {
@@ -111,6 +114,12 @@ vows.describe('URLDynamicHelpers').addBatch({
animal.id = '123';
assert.equal(view.urlFor(animal), 'http://www.example.com/animals/123');
},
+ 'should invoke routing helper methods when given an object argument and onlyPath option': function (view) {
+ function Animal() {};
+ var animal = new Animal();
+ animal.id = '123';
+ assert.equal(view.urlFor(animal, { onlyPath: true }), '/animals/123');
+ },
'should throw error when unable to find routing helper for object': function (view) {
function Dog() {};
var dog = new Dog();

0 comments on commit 0f7dc5a

Please sign in to comment.
Something went wrong with that request. Please try again.