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

Commit 2f17a525 by Michael Storgaard

Added helper method for checking change (incl. Date and empty values), fixed som…

…e small stuff and added more tests
1 parent d0bec74b
......@@ -87,7 +87,7 @@ module.exports = (function() {
DAO.prototype.get = DAO.prototype.getDataValue
DAO.prototype.setDataValue = function(name, value) {
if (this.dataValues[name] != value) {
if (Utils.areChanged(this.dataValues[name], value)) {
this.isDirty = true
}
this.dataValues[name] = value
......@@ -240,7 +240,7 @@ module.exports = (function() {
(self.attributes.indexOf(attr) > -1)
)
if (updateAllowed) {
if (self[attr] != value) {
if (Utils.areChanged(self[attr], value)) {
isDirty = true
}
self[attr] = value
......@@ -346,13 +346,13 @@ module.exports = (function() {
if (has !== true) {
this.__defineGetter__(attribute, has.get || function() { return this.dataValues[attribute]; });
this.__defineSetter__(attribute, has.set || function(v) {
if (this.dataValues[attribute] != v) {
if (Utils.areChanged(this.dataValues[attribute], v)) {
//Only dirty the object if the change is not due to id, touchedAt, createdAt or updatedAt being initiated
if (this.dataValues[attribute] || (attribute != 'id' && attribute != 'touchedAt' && attribute != 'createdAt' && attribute != 'updatedAt')) {
this.isDirty = true
}
}
this.dataValues[attribute] = v;
this.dataValues[attribute] = v
});
}
......
......@@ -287,7 +287,7 @@ module.exports = (function() {
var transformRowWithEagerLoadingIntoDao = function(result, dao) {
// let's build the actual dao instance first...
dao = dao || this.callee.build(result[this.callee.tableName], { isNewRecord: false })
dao = dao || this.callee.build(result[this.callee.tableName], { isNewRecord: false, isDirty: false })
// ... and afterwards the prefetched associations
for (var tableName in result) {
......@@ -323,7 +323,7 @@ module.exports = (function() {
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1)
associationData.forEach(function(data) {
var daoInstance = associatedDaoFactory.build(data, { isNewRecord: false })
var daoInstance = associatedDaoFactory.build(data, { isNewRecord: false, isDirty: false })
, isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers)
if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) {
......
......@@ -252,6 +252,20 @@ var Utils = module.exports = {
isHash: function(obj) {
return Utils._.isObject(obj) && !Array.isArray(obj);
},
areChanged: function(attrValue, value) {
//If attribute value is Date, check value as a date
if (Utils._.isDate(attrValue) && !Utils._.isDate(value)) {
value = new Date(value)
}
if (Utils._.isDate(attrValue)) {
return attrValue.valueOf() !== value.valueOf()
}
//If both are them are empty, don't set as changed
if ((attrValue === undefined || attrValue === null || attrValue === '') && (value === undefined || value === null || value === '')) {
return false
}
return attrValue !== value
},
argsArePrimaryKeys: function(args, primaryKeys) {
var result = (args.length == Object.keys(primaryKeys).length)
if (result) {
......
......@@ -19,6 +19,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER },
bNumber: { type: DataTypes.INTEGER },
aDate: { type: DataTypes.DATE },
validateTest: {
type: DataTypes.INTEGER,
......@@ -114,13 +115,13 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it('returns true for non-saved objects', function(done) {
var user = this.User.build({ username: 'user' })
expect(user.id).to.be.null
expect(user.isDirty).to.be.ok
expect(user.isDirty).to.be.true
done()
})
it("returns false for saved objects", function(done) {
this.User.build({ username: 'user' }).save().success(function(user) {
expect(user.isDirty).to.not.be.ok
expect(user.isDirty).to.be.false
done()
})
})
......@@ -128,7 +129,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it("returns true for changed attribute", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.username = 'new'
expect(user.isDirty).to.be.ok
expect(user.isDirty).to.be.true
done()
})
})
......@@ -136,7 +137,23 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it("returns false for non-changed attribute", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.username = 'user'
expect(user.isDirty).to.not.be.ok
expect(user.isDirty).to.be.false
done()
})
})
it("returns false for non-changed date attribute", function(done) {
this.User.create({ aDate: new Date(2013, 6, 31, 14, 25, 21) }).success(function(user) {
user.aDate = '2013-07-31 14:25:21'
expect(user.isDirty).to.be.false
done()
})
})
it("returns false for two empty attributes", function(done) {
this.User.create({ username: null }).success(function(user) {
user.username = ''
expect(user.isDirty).to.be.false
done()
})
})
......@@ -147,7 +164,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
username: 'new',
aNumber: 1
})
expect(user.isDirty).to.be.ok
expect(user.isDirty).to.be.true
done()
})
})
......@@ -157,18 +174,18 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
user.setAttributes({
username: 'user'
})
expect(user.isDirty).to.not.be.ok
expect(user.isDirty).to.be.false
done()
})
})
it("returns true for changed and bulk non-changed attribute", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.aNumber = 1
user.aNumber = 23
user.setAttributes({
username: 'user'
})
expect(user.isDirty).to.be.ok
expect(user.isDirty).to.be.true
done()
})
})
......@@ -176,9 +193,9 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it("returns true for changed attribute and false for saved object", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.username = 'new'
expect(user.isDirty).to.be.ok
expect(user.isDirty).to.be.true
user.save().success(function() {
expect(user.isDirty).to.not.be.ok
expect(user.isDirty).to.be.false
done()
})
})
......@@ -186,7 +203,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it("returns false for created objects", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
expect(user.isDirty).to.not.be.ok
expect(user.isDirty).to.be.false
done()
})
})
......@@ -195,7 +212,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
var self = this
this.User.create({ username: 'user' }).success(function(user) {
self.User.find(user.id).success(function(user) {
expect(user.isDirty).to.not.be.ok
expect(user.isDirty).to.be.false
done()
})
})
......@@ -212,7 +229,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
this.User.bulkCreate(users).success(function() {
self.User.findAll().success(function(users) {
users.forEach(function(u) {
expect(u.isDirty).to.not.be.ok
expect(u.isDirty).to.be.false
})
done()
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!