Implemented hasOne update()

This commit is contained in:
Fabien Franzen 2014-08-20 15:54:47 +02:00
parent 5dc7a6350c
commit 123b2558a1
2 changed files with 47 additions and 1 deletions

View File

@ -1353,6 +1353,8 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
var relationMethod = relation.related.bind(relation) var relationMethod = relation.related.bind(relation)
relationMethod.create = relation.create.bind(relation); relationMethod.create = relation.create.bind(relation);
relationMethod.build = relation.build.bind(relation); relationMethod.build = relation.build.bind(relation);
relationMethod.update = relation.update.bind(relation);
relationMethod.destroy = relation.destroy.bind(relation);
relationMethod._targetClass = definition.modelTo.modelName; relationMethod._targetClass = definition.modelTo.modelName;
return relationMethod; return relationMethod;
} }
@ -1416,6 +1418,23 @@ HasOne.prototype.create = function (targetModelData, cb) {
}); });
}; };
HasOne.prototype.update = function (targetModelData, cb) {
var definition = this.definition;
this.fetch(function(err, inst) {
if (inst instanceof ModelBaseClass) {
inst.updateAttributes(targetModelData, cb);
} else {
cb(new Error('HasOne relation ' + definition.name
+ ' is empty'));
}
});
};
HasOne.prototype.destroy = function (cb) {
};
/** /**
* Create a target model instance * Create a target model instance
* @param {Object} targetModelData The target model data * @param {Object} targetModelData The target model data

View File

@ -1206,6 +1206,7 @@ describe('relations', function () {
describe('hasOne', function () { describe('hasOne', function () {
var Supplier, Account; var Supplier, Account;
var supplierId;
before(function () { before(function () {
db = getSchema(); db = getSchema();
@ -1220,9 +1221,9 @@ describe('relations', function () {
}); });
it('can be used to query data', function (done) { it('can be used to query data', function (done) {
// Supplier.hasOne(Account);
db.automigrate(function () { db.automigrate(function () {
Supplier.create({name: 'Supplier 1'}, function (e, supplier) { Supplier.create({name: 'Supplier 1'}, function (e, supplier) {
supplierId = supplier.id;
should.not.exist(e); should.not.exist(e);
should.exist(supplier); should.exist(supplier);
supplier.account.create({accountNo: 'a01'}, function (err, account) { supplier.account.create({accountNo: 'a01'}, function (err, account) {
@ -1242,6 +1243,32 @@ describe('relations', function () {
it('should set targetClass on scope property', function() { it('should set targetClass on scope property', function() {
should.equal(Supplier.prototype.account._targetClass, 'Account'); should.equal(Supplier.prototype.account._targetClass, 'Account');
}); });
it('should update the related item on scope', function(done) {
Supplier.findById(supplierId, function(e, supplier) {
should.not.exist(e);
should.exist(supplier);
supplier.account.update({supplierName: 'Supplier A'}, function(err, act) {
should.not.exist(e);
act.supplierName.should.equal('Supplier A');
done();
});
});
});
it('should get the related item on scope', 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.exist(act);
act.supplierName.should.equal('Supplier A');
done();
});
});
});
}); });
describe('hasAndBelongsToMany', function () { describe('hasAndBelongsToMany', function () {