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

Commit 032a7a14 by Robert Falkén

The instance.update method keeps track of possible side effects caused by setter…

… methods and include affected fields in save.
1 parent ab8ca530
...@@ -754,13 +754,21 @@ module.exports = (function() { ...@@ -754,13 +754,21 @@ module.exports = (function() {
* @alias updateAttributes * @alias updateAttributes
*/ */
Instance.prototype.update = function(values, options) { Instance.prototype.update = function(values, options) {
var changedBefore = this.changed() || []
, sideEffects
, fields;
options = options || {}; options = options || {};
if (Array.isArray(options)) options = {fields: options}; if (Array.isArray(options)) options = {fields: options};
this.set(values, {attributes: options.fields}); 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) { if (!options.fields) {
options.fields = _.intersection(Object.keys(values), this.changed()); options.fields = _.intersection(fields, this.changed());
options.defaultFields = options.fields; options.defaultFields = options.fields;
} }
......
...@@ -36,6 +36,20 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -36,6 +36,20 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
allowNull: true, allowNull: true,
validate: {len: {msg: 'Length failed.', args: [1, 20]}} 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: { dateAllowNullTrue: {
type: DataTypes.DATE, type: DataTypes.DATE,
...@@ -141,6 +155,18 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -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 () { describe('hooks', function () {
it('should update attributes added in hooks when default fields are used', function () { it('should update attributes added in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), { 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!