Skip to content

Commit b4e69aa

Browse files
author
Paul Covell
committed
Use object if object[idField] is undefined for path ID. Per discussion on Issue visionmedia#20.
1 parent b1674df commit b4e69aa

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ Resource.prototype.createRouteHelper = function(mapPath, route) {
271271
resourceAccess.path[methodName + '_path'] = resourceAccess.path[methodName] || function() {
272272
var localRoute = route;
273273
Array.prototype.forEach.call(arguments, function(arg) {
274-
localRoute = localRoute.replace(/:\w+/, arg[resourceAccess.path.idField]);
274+
var id = arg[resourceAccess.path.idField] || arg;
275+
localRoute = localRoute.replace(/:\w+/, id);
275276
});
276277
return localRoute;
277278
};

test/resource.path.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ module.exports = {
108108
assert.strictEqual(app.resource.path.design_forums_path(), '/forums/design');
109109
assert.strictEqual(app.resource.path.lock_forum_path({id: 5}), '/forums/5/lock');
110110
},
111+
'test resource with direct id value': function(){
112+
var app = express.createServer();
113+
var ret = app.resource('forums', require('./fixtures/forum'));
114+
115+
assert.strictEqual(app.resource.path.forum_path(5), '/forums/5');
116+
assert.strictEqual(app.resource.path.edit_forum_path(5), '/forums/5/edit');
117+
118+
assert.strictEqual(app.resource.path.forum_path("5"), '/forums/5');
119+
assert.strictEqual(app.resource.path.edit_forum_path("5"), '/forums/5/edit');
120+
},
111121
'test resource with custom id field': function(){
112122
var app = express.createServer();
113123
var ret = app.resource('forums', require('./fixtures/forum'));

0 commit comments

Comments
 (0)