Skip to content

Commit bdf531a

Browse files
committed
Allow HasMany.setAccessor to take an empty array
1 parent 01c33d8 commit bdf531a

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed

lib/Associations/Many.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,19 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan
232232
});
233233
Object.defineProperty(Instance, association.setAccessor, {
234234
value: function () {
235-
var Instances = Array.prototype.slice.apply(arguments);
236-
var cb = (Instances.length &&
237-
typeof Instances[Instances.length - 1] == "function" ? Instances.pop() : noOperation);
238-
239-
if (Instances.length === 0) {
240-
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISMATCH, "No associations defined", { model: Model.name });
241-
}
242-
243-
if (Array.isArray(Instances[0])) {
244-
// clone is used or else Instances will be just a reference
245-
// to the array and the Instances.push(cb) a few lines ahead
246-
// would actually change the user Array passed to the function
247-
Instances = _.clone(Instances[0]);
248-
}
235+
var items = _.flatten(arguments);
236+
var cb = _.last(items) instanceof Function ? items.pop() : noOperation;
249237

250238
Instance[association.delAccessor](function (err) {
251-
if (err) {
252-
return cb(err);
239+
if (err) return cb(err);
240+
241+
if (items.length) {
242+
Instance[association.addAccessor](items, cb);
243+
} else {
244+
cb(null);
253245
}
254-
Instances.push(cb);
255-
Instance[association.addAccessor].apply(Instance, Instances);
256246
});
247+
257248
return this;
258249
},
259250
enumerable: false

test/integration/association-hasmany.js

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -367,59 +367,28 @@ describe("hasMany", function () {
367367
});
368368
});
369369
});
370-
});
371-
372-
describe("addAccessor", function () {
373-
before(setup());
374370

375371
it("should accept array as list of associations", function (done) {
376372
Pet.find(function (err, pets) {
377-
Person.find({ name: "Justin" }).first(function (err, Justin) {
378-
should.equal(err, null);
379-
380-
Justin.addPets(pets, function (err) {
381-
should.equal(err, null);
382-
383-
Justin.getPets(function (err, all_pets) {
384-
should.equal(err, null);
385-
386-
should(Array.isArray(all_pets));
387-
all_pets.length.should.equal(pets.length);
388-
389-
return done();
390-
});
391-
});
392-
});
393-
});
394-
});
395-
});
373+
var petCount = pets.length;
396374

397-
describe("setAccessor", function () {
398-
before(setup());
399-
400-
it("clears current associations", function (done) {
401-
Pet.find({ name: "Deco" }, function (err, pets) {
402-
var Deco = pets[0];
403-
404-
Person.find({ name: "Jane" }).first(function (err, Jane) {
375+
Person.find({ name: "Justin" }).first(function (err, Justin) {
405376
should.equal(err, null);
406377

407-
Jane.getPets(function (err, pets) {
378+
Justin.getPets(function (err, justinsPets) {
408379
should.equal(err, null);
409380

410-
should(Array.isArray(pets));
411-
pets.length.should.equal(1);
412-
pets[0].name.should.equal("Mutt");
381+
should.equal(justinsPets.length, 2);
413382

414-
Jane.setPets(Deco, function (err) {
383+
Justin.addPets(pets, function (err) {
415384
should.equal(err, null);
416385

417-
Jane.getPets(function (err, pets) {
386+
Justin.getPets(function (err, justinsPets) {
418387
should.equal(err, null);
419388

420-
should(Array.isArray(pets));
421-
pets.length.should.equal(1);
422-
pets[0].name.should.equal(Deco.name);
389+
should(Array.isArray(justinsPets));
390+
// We're not checking uniqueness.
391+
should.equal(justinsPets.length, petCount + 2);
423392

424393
return done();
425394
});
@@ -428,6 +397,18 @@ describe("hasMany", function () {
428397
});
429398
});
430399
});
400+
401+
it("should throw if no items passed", function (done) {
402+
Person.one(function (err, person) {
403+
should.equal(err, null);
404+
405+
(function () {
406+
person.addPets(function () {});
407+
}).should.throw();
408+
409+
return done();
410+
});
411+
});
431412
});
432413

433414
describe("setAccessor", function () {
@@ -475,15 +456,47 @@ describe("hasMany", function () {
475456
});
476457
});
477458

478-
it("should throw if no items passed", function (done) {
479-
Person.one(function (err, person) {
459+
it("should remove all associations if an empty array is passed", function (done) {
460+
Person.find({ name: "Justin" }).first(function (err, Justin) {
480461
should.equal(err, null);
481462

482-
(function () {
483-
person.addPets(function () {});
484-
}).should.throw();
463+
Justin.setPets([], function (err) {
464+
should.equal(err, null);
485465

486-
return done();
466+
return done();
467+
});
468+
});
469+
});
470+
471+
it("clears current associations", function (done) {
472+
Pet.find({ name: "Deco" }, function (err, pets) {
473+
var Deco = pets[0];
474+
475+
Person.find({ name: "Jane" }).first(function (err, Jane) {
476+
should.equal(err, null);
477+
478+
Jane.getPets(function (err, pets) {
479+
should.equal(err, null);
480+
481+
should(Array.isArray(pets));
482+
pets.length.should.equal(1);
483+
pets[0].name.should.equal("Mutt");
484+
485+
Jane.setPets(Deco, function (err) {
486+
should.equal(err, null);
487+
488+
Jane.getPets(function (err, pets) {
489+
should.equal(err, null);
490+
491+
should(Array.isArray(pets));
492+
pets.length.should.equal(1);
493+
pets[0].name.should.equal(Deco.name);
494+
495+
return done();
496+
});
497+
});
498+
});
499+
});
487500
});
488501
});
489502
});

0 commit comments

Comments
 (0)