不要怂,就是干,撸起袖子干!

Commit 50beb169 by Mick Hansen

Merge pull request #3393 from robertfalken/update-tracks-side-effects

.update include attributes changed by setter methods
2 parents 7c0aa4dc 032a7a14
......@@ -754,13 +754,21 @@ module.exports = (function() {
* @alias updateAttributes
*/
Instance.prototype.update = function(values, options) {
var changedBefore = this.changed() || []
, sideEffects
, fields;
options = options || {};
if (Array.isArray(options)) options = {fields: options};
this.set(values, {attributes: options.fields});
// Now we need to figure out which fields were actually affected by the setter.
sideEffects = _.without.apply(this, [this.changed() || []].concat(changedBefore));
fields = _.union(Object.keys(values), sideEffects);
if (!options.fields) {
options.fields = _.intersection(Object.keys(values), this.changed());
options.fields = _.intersection(fields, this.changed());
options.defaultFields = options.fields;
}
......
......@@ -36,6 +36,20 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
allowNull: true,
validate: {len: {msg: 'Length failed.', args: [1, 20]}}
},
validateSideEffect: {
type: DataTypes.VIRTUAL,
allowNull: true,
validate: {isInt: true},
set: function (val) {
this.setDataValue('validateSideEffect', val);
this.setDataValue('validateSideAffected', val*2);
}
},
validateSideAffected: {
type: DataTypes.INTEGER,
allowNull: true,
validate: {isInt: true}
},
dateAllowNullTrue: {
type: DataTypes.DATE,
......@@ -141,6 +155,18 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
it('should save attributes affected by setters', function () {
var user = this.User.build();
return user.update({validateSideEffect: 5}).then(function () {
expect(user.validateSideEffect).to.be.equal(5);
}).then(function () {
return user.reload();
}).then(function () {
expect(user.validateSideAffected).to.be.equal(10);
expect(user.validateSideEffect).not.to.be.ok;
});
});
describe('hooks', function () {
it('should update attributes added in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!