Browse files

Added custom ID field configuration

  • Loading branch information...
1 parent b21decd commit 3fb30d2de6f7e23fd26822d726a41494a4f69d83 Paul committed Jul 12, 2011
Showing with 14 additions and 1 deletion.
  1. +3 −1 index.js
  2. +11 −0 test/resource.path.test.js
View
4 index.js
@@ -228,6 +228,8 @@ Resource.prototype.mapDefaultAction = function(key, fn){
Resource.prototype.createRouteHelper = function(mapPath, route) {
resourceAccess.path = resourceAccess.path || {};
+ resourceAccess.path.idField = resourceAccess.path.idField || 'id';
+
var methodName = this.name || "roots";
var argCount = 0;
@@ -266,7 +268,7 @@ Resource.prototype.createRouteHelper = function(mapPath, route) {
resourceAccess.path[methodName] = resourceAccess.path[methodName] || function() {
var localRoute = route;
Array.prototype.forEach.call(arguments, function(arg) {
- localRoute = localRoute.replace(/:\w+/, arg['id']);
+ localRoute = localRoute.replace(/:\w+/, arg[resourceAccess.path.idField]);
});
return localRoute;
};
View
11 test/resource.path.test.js
@@ -89,5 +89,16 @@ module.exports = {
assert.strictEqual(app.resource.path.user_forum_thread(userObj, forumObj, threadObj), '/users/1/forums/5/threads/50');
assert.strictEqual(app.resource.path.edit_user_forum_thread(userObj, forumObj, threadObj), '/users/1/forums/5/threads/50/edit');
},
+ 'test resource with custom id field': function(){
+ var app = express.createServer();
+ var ret = app.resource('forums', require('./fixtures/forum'));
+
+ // NOTE: because this is set across all resources, this test should be run last or it will need
+ // to be reset for each test. If this is too confusing, we could change to set in on a per-resource basis.
+ app.resource.path.idField = '_id';
+
+ assert.strictEqual(app.resource.path.forum({_id: 5}), '/forums/5');
+ assert.strictEqual(app.resource.path.edit_forum({_id: 5}), '/forums/5/edit');
+ },
};

0 comments on commit 3fb30d2

Please sign in to comment.