From 12ebaf77f29c063b1fed8b2210612989d1798860 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Wed, 20 Aug 2014 15:58:45 +0200 Subject: [PATCH] Implemented hasOne destroy() --- lib/relation-definition.js | 10 ++++++++-- test/relations.test.js | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index f913cc6b..4674173b 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1431,8 +1431,14 @@ HasOne.prototype.update = function (targetModelData, cb) { }; HasOne.prototype.destroy = function (cb) { - - + this.fetch(function(err, inst) { + if (inst instanceof ModelBaseClass) { + inst.destroy(cb); + } else { + cb(new Error('HasOne relation ' + definition.name + + ' is empty')); + } + }); }; /** diff --git a/test/relations.test.js b/test/relations.test.js index b517e236..bba27a0e 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1269,6 +1269,29 @@ describe('relations', function () { }); }); + it('should destroy the related item on scope', function(done) { + Supplier.findById(supplierId, function(e, supplier) { + should.not.exist(e); + should.exist(supplier); + supplier.account.destroy(function(err) { + should.not.exist(e); + done(); + }); + }); + }); + + it('should get the related item on scope - verify', function(done) { + Supplier.findById(supplierId, function(e, supplier) { + should.not.exist(e); + should.exist(supplier); + supplier.account(function(err, act) { + should.not.exist(e); + should.not.exist(act); + done(); + }); + }); + }); + }); describe('hasAndBelongsToMany', function () {