Skip to content

Commit 0ce2e43

Browse files
committed
Allow before* hooks to modify the instance
1 parent a37983d commit 0ce2e43

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

lib/Instance.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ function Instance(Model, opts) {
9393
}
9494
};
9595
var saveInstance = function (cb, saveOptions) {
96-
if (!opts.is_new && opts.changes.length === 0) {
97-
return saveInstanceExtra(cb);
98-
}
99-
10096
handleValidations(function (err) {
10197
if (err) {
10298
return saveError(cb, err);
@@ -116,6 +112,10 @@ function Instance(Model, opts) {
116112
return saveError(cb, err);
117113
}
118114

115+
if (opts.changes.length === 0) {
116+
return saveInstanceExtra(cb);
117+
}
118+
119119
return savePersisted(cb, saveOptions, getInstanceData());
120120
});
121121
}

test/integration/hook.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ describe("Hook", function() {
119119
});
120120
});
121121

122+
it("should allow modification of instance", function (done) {
123+
Person.beforeCreate(function (next) {
124+
this.name = "Hook Worked";
125+
next();
126+
});
127+
128+
Person.create([{ }], function (err, people) {
129+
should.not.exist(err);
130+
should.exist(people);
131+
should.equal(people.length, 1);
132+
should.equal(people[0].name, "Hook Worked");
133+
done();
134+
});
135+
});
136+
122137
describe("when setting properties", function () {
123138
before(setup({
124139
beforeCreate : function () {
@@ -217,6 +232,20 @@ describe("Hook", function() {
217232
return done();
218233
});
219234
});
235+
236+
it("should allow modification of instance", function (done) {
237+
Person.beforeSave(function () {
238+
this.name = "Hook Worked";
239+
});
240+
241+
Person.create([{ name: "John Doe" }], function (err, people) {
242+
should.not.exist(err);
243+
should.exist(people);
244+
should.equal(people.length, 1);
245+
should.equal(people[0].name, "Hook Worked");
246+
done();
247+
});
248+
});
220249

221250
describe("when setting properties", function () {
222251
before(setup({
@@ -339,6 +368,21 @@ describe("Hook", function() {
339368
});
340369
});
341370

371+
372+
it("should allow modification of instance", function (done) {
373+
Person.beforeValidation(function () {
374+
this.name = "Hook Worked";
375+
});
376+
377+
Person.create([{ name: "John Doe" }], function (err, people) {
378+
should.not.exist(err);
379+
should.exist(people);
380+
should.equal(people.length, 1);
381+
should.equal(people[0].name, "Hook Worked");
382+
done();
383+
});
384+
});
385+
342386
describe("if hook method has 1 argument", function () {
343387
var beforeValidation = false;
344388
this.timeout(500);
@@ -589,4 +633,31 @@ describe("Hook", function() {
589633
});
590634
});
591635
});
636+
637+
describe("instance modifications", function () {
638+
before(setup({
639+
beforeValidation: function () {
640+
should.equal(this.name, "John Doe");
641+
this.name = "beforeValidation";
642+
},
643+
beforeCreate: function () {
644+
should.equal(this.name, "beforeValidation");
645+
this.name = "beforeCreate";
646+
},
647+
beforeSave: function () {
648+
should.equal(this.name, "beforeCreate");
649+
this.name = "beforeSave";
650+
}
651+
}));
652+
653+
it("should propagate down hooks", function (done) {
654+
Person.create([{ name: "John Doe" }], function (err, people) {
655+
should.not.exist(err);
656+
should.exist(people);
657+
should.equal(people.length, 1);
658+
should.equal(people[0].name, "beforeSave");
659+
done();
660+
});
661+
});
662+
});
592663
});

0 commit comments

Comments
 (0)