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

Commit 73f1d098 by Mick Hansen

fix(instance): setDataValue should set changed

1 parent 34d7284d
......@@ -168,6 +168,11 @@ module.exports = (function() {
* @param {any} value
*/
Instance.prototype.setDataValue = function(key, value) {
var originalValue = this._previousDataValues[key];
if (primitives.indexOf(typeof value) === -1 || value !== originalValue) {
this.changed(key, true);
}
this.dataValues[key] = value;
};
......@@ -818,29 +823,27 @@ module.exports = (function() {
force: false
}, options || {});
var self = this;
// This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
return Promise.try(function() {
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
return self.Model.runHooks('beforeDestroy', self, options);
return this.Model.runHooks('beforeDestroy', this, options);
}
}).then(function() {
var where;
if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date();
return self.save(_.extend(_.clone(options), {hooks : false}));
if (this.Model._timestampAttributes.deletedAt && options.force === false) {
this.setDataValue(this.Model._timestampAttributes.deletedAt, new Date());
return this.save(_.extend(_.clone(options), {hooks : false}));
} else {
where = {};
where[self.Model.rawAttributes[self.Model.primaryKeyAttribute].field] = self.get(self.Model.primaryKeyAttribute, {raw: true});
return self.QueryInterface.delete(self, self.Model.getTableName(options), where, _.defaults(options, { type: QueryTypes.DELETE,limit: null}));
where[this.Model.rawAttributes[this.Model.primaryKeyAttribute].field] = this.get(this.Model.primaryKeyAttribute, {raw: true});
return this.QueryInterface.delete(this, this.Model.getTableName(options), where, _.defaults(options, { type: QueryTypes.DELETE,limit: null}));
}
}).tap(function() {
// Run after hook
if (options.hooks) {
return self.Model.runHooks('afterDestroy', self, options);
return this.Model.runHooks('afterDestroy', this, options);
}
}).then(function(result) {
return result;
......@@ -863,20 +866,18 @@ module.exports = (function() {
force: false
}, options || {});
var self = this;
return Promise.try(function() {
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
return self.Model.runHooks('beforeRestore', self, options);
return this.Model.runHooks('beforeRestore', this, options);
}
}).then(function() {
self.dataValues[self.Model._timestampAttributes.deletedAt] = null;
return self.save(_.extend(_.clone(options), {hooks : false, omitNull : false}));
this.setDataValue(this.Model._timestampAttributes.deletedAt, null);
return this.save(_.extend(_.clone(options), {hooks : false, omitNull : false}));
}).tap(function() {
// Run after hook
if (options.hooks) {
return self.Model.runHooks('afterRestore', self, options);
return this.Model.runHooks('afterRestore', this, options);
}
});
};
......
......@@ -141,21 +141,6 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
it('returns false for non-changed date attribute', function() {
return this.User.create({ aDate: new Date(2013, 6, 31, 14, 25, 21) }).then(function(user) {
user.aDate = '2013-07-31 14:25:21';
expect(user.isDirty).to.be.false;
});
});
// In my opinion this is bad logic, null is different from an empty string
it.skip('returns false for two empty attributes', function() {
return this.User.create({ username: null }).then(function(user) {
user.username = '';
expect(user.isDirty).to.be.false;
});
});
it('returns true for bulk changed attribute', function() {
return this.User.create({ username: 'user' }).then(function(user) {
user.setAttributes({
......@@ -1805,8 +1790,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}).then(function() {
return ParanoidUser.find({where: {secretValue: '42'}});
}).then(function(user) {
return user.destroy()
.then(function() {
return user.destroy().then(function() {
return user.restore();
});
}).then(function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!