Skip to content
Browse files

[api] added "getChild" method

  • Loading branch information...
1 parent 26268e8 commit f262e9c8f95a9f69c4f1972d2d999f4fba638bb2 @nrw nrw committed
Showing with 81 additions and 0 deletions.
  1. +39 −0 lib/resourceful/resource.js
  2. +42 −0 test/relationship-test.js
View
39 lib/resourceful/resource.js
@@ -544,6 +544,45 @@ function relationship(factory, type, r, options) {
};
//
+ // Parent.getChild(parent, child_id, callback)
+ //
+ factory['get' + rstringc] = function (parent, child_id, callback) {
+ var key = factory.lowerResource + '_id',
+ id = parent.key || parent,
+ cid = child_id;
+
+ factory.get(id, function(err, p) {
+ if(err) {
+ if(callback) return callback(err);
+ }
+
+ var rstringi = rstring + '_ids';
+ p[rstringi] = p[rstringi] || [];
+
+ if (p[rstringi].indexOf(cid) < 0) {
+ // this parent does not have a child with the given id
+ return callback(null, null)
+ } else {
+ // parent has child
+ id_path = factory.lowerResource + '/' + id + '/' + cid
+
+ rfactory.get(id_path, function(err, child){
+ if(err) {
+ if(callback) return callback(err);
+ }
+ return callback(null, child);
+ });
+ }
+ });
+ }
+ //
+ // parent.getChild(child_id, callback)
+ //
+ factory.prototype['get' + rstringc] = function (child_id, callback) {
+ return this.constructor['get' + rstringc](this.key, child_id, callback);
+ };
+
+ //
// Parent.createChild(id, child, callback)
//
factory['create' + rstringc] = function (parent, child, callback) {
View
42 test/relationship-test.js
@@ -94,6 +94,48 @@ engines.forEach(function (e) {
assert.equal(obj[0].user_id, 'pavan');
assert.equal(obj[1].user_id, 'pavan');
}
+ },
+ "and when 'Parent.getChild()' is used": {
+ topic: function (obj) {
+ resources[e].User.getRepository(
+ 'pavan',
+ 'bullet',
+ this.callback
+ );
+ },
+ "should return one repository": function (err, obj) {
+ assert.isNull(err);
+ assert.isObject(obj);
+ assert.equal(obj.id, 'user/pavan/bullet');
+ assert.equal(obj.name, 'bullet');
+ },
+ "should be of proper resource type": function (err, obj) {
+ assert.isNull(err);
+ assert.equal(obj.resource, 'Repository');
+ },
+ "should have the user_id set correctly": function (err, obj) {
+ assert.isNull(err);
+ assert.equal(obj.user_id, 'pavan');
+ }
+ },
+ "and when 'Parent.prototype.getChild()' is used": {
+ topic: function (obj) {
+ obj.getRepository('bullet', this.callback);
+ },
+ "should return one repository": function (err, obj) {
+ assert.isNull(err);
+ assert.isObject(obj);
+ assert.equal(obj.id, 'user/pavan/bullet');
+ assert.equal(obj.name, 'bullet');
+ },
+ "should be of proper resource type": function (err, obj) {
+ assert.isNull(err);
+ assert.equal(obj.resource, 'Repository');
+ },
+ "should have the user_id set correctly": function (err, obj) {
+ assert.isNull(err);
+ assert.equal(obj.user_id, 'pavan');
+ }
}
}
}

0 comments on commit f262e9c

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