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

Commit 73f1d098 by Mick Hansen

fix(instance): setDataValue should set changed

1 parent 34d7284d
...@@ -168,6 +168,11 @@ module.exports = (function() { ...@@ -168,6 +168,11 @@ module.exports = (function() {
* @param {any} value * @param {any} value
*/ */
Instance.prototype.setDataValue = function(key, 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; this.dataValues[key] = value;
}; };
...@@ -818,29 +823,27 @@ module.exports = (function() { ...@@ -818,29 +823,27 @@ module.exports = (function() {
force: false force: false
}, options || {}); }, 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 // 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 // Run before hook
if (options.hooks) { if (options.hooks) {
return self.Model.runHooks('beforeDestroy', self, options); return this.Model.runHooks('beforeDestroy', this, options);
} }
}).then(function() { }).then(function() {
var where; var where;
if (self.Model._timestampAttributes.deletedAt && options.force === false) { if (this.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date(); this.setDataValue(this.Model._timestampAttributes.deletedAt, new Date());
return self.save(_.extend(_.clone(options), {hooks : false})); return this.save(_.extend(_.clone(options), {hooks : false}));
} else { } else {
where = {}; where = {};
where[self.Model.rawAttributes[self.Model.primaryKeyAttribute].field] = self.get(self.Model.primaryKeyAttribute, {raw: true}); where[this.Model.rawAttributes[this.Model.primaryKeyAttribute].field] = this.get(this.Model.primaryKeyAttribute, {raw: true});
return self.QueryInterface.delete(self, self.Model.getTableName(options), where, _.defaults(options, { type: QueryTypes.DELETE,limit: null})); return this.QueryInterface.delete(this, this.Model.getTableName(options), where, _.defaults(options, { type: QueryTypes.DELETE,limit: null}));
} }
}).tap(function() { }).tap(function() {
// Run after hook // Run after hook
if (options.hooks) { if (options.hooks) {
return self.Model.runHooks('afterDestroy', self, options); return this.Model.runHooks('afterDestroy', this, options);
} }
}).then(function(result) { }).then(function(result) {
return result; return result;
...@@ -863,20 +866,18 @@ module.exports = (function() { ...@@ -863,20 +866,18 @@ module.exports = (function() {
force: false force: false
}, options || {}); }, options || {});
var self = this; return Promise.bind(this).then(function() {
return Promise.try(function() {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
return self.Model.runHooks('beforeRestore', self, options); return this.Model.runHooks('beforeRestore', this, options);
} }
}).then(function() { }).then(function() {
self.dataValues[self.Model._timestampAttributes.deletedAt] = null; this.setDataValue(this.Model._timestampAttributes.deletedAt, null);
return self.save(_.extend(_.clone(options), {hooks : false, omitNull : false})); return this.save(_.extend(_.clone(options), {hooks : false, omitNull : false}));
}).tap(function() { }).tap(function() {
// Run after hook // Run after hook
if (options.hooks) { 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() { ...@@ -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() { it('returns true for bulk changed attribute', function() {
return this.User.create({ username: 'user' }).then(function(user) { return this.User.create({ username: 'user' }).then(function(user) {
user.setAttributes({ user.setAttributes({
...@@ -1805,8 +1790,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1805,8 +1790,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}).then(function() { }).then(function() {
return ParanoidUser.find({where: {secretValue: '42'}}); return ParanoidUser.find({where: {secretValue: '42'}});
}).then(function(user) { }).then(function(user) {
return user.destroy() return user.destroy().then(function() {
.then(function() {
return user.restore(); return user.restore();
}); });
}).then(function() { }).then(function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!