Skip to content

Commit

Permalink
#4
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstennett committed Oct 18, 2016
1 parent e4468bf commit 525aa79
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
30 changes: 16 additions & 14 deletions historical.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var _ = require('lodash'),
models = {};

module.exports = function (schema, options) {
options = options || {};
options = options || {};
var mongoose = options.mongoose /* DEPRECATED */ || require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId,
Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports = function (schema, options) {
o = o[n];
} else {
o[n] = {};
o = o[n];
o = o[n];
}
}
o[a[a.length - 1]] = v;
Expand All @@ -57,19 +57,21 @@ module.exports = function (schema, options) {
var me = this,
HistoricalModel = getHistoricalModel(me),
modified = _.uniq(me.modifiedPaths()),
diff = {};
diff = this.isNew ? me.toObject() : {};

modified.forEach(function (index) {
var value = read(me, index);
if (_.isPlainObject(value)) {
return;
}
if (value === undefined) {
write(diff, index, null);
return;
}
write(diff, index, value);
});
if (!this.isNew) {
modified.forEach(function (index) {
var value = read(me, index);
if (_.isPlainObject(value)) {
return;
}
if (value === undefined) {
write(diff, index, null);
return;
}
write(diff, index, value);
});
}

var historical = new HistoricalModel({
document: me[primaryKeyName],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"url": "git://github.com/stennettm/historical.git"
},
"main": "historical.js",
"version": "1.0.0",
"version": "1.0.1",
"dependencies": {
"lodash": "^3.10.1"
},
Expand Down
8 changes: 6 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var assert = require("assert"),
_ = require('lodash'),
mongoose = require('mongoose'),
TestSchema = new mongoose.Schema({
testString: String,
testString: {
type: String,
default: 'My default value'
},
testNumber: Number,
testArray: [String],
testBoolean: Boolean,
Expand All @@ -18,7 +21,6 @@ var connection = mongoose.createConnection('mongodb://localhost/historical_test'
describe('Document', function(){

var document = new TestModel({
testString: 'test',
testNumber: 42,
testArray: ['test1', 'test2'],
testObject: {
Expand All @@ -41,6 +43,7 @@ describe('Document', function(){
var diff = _.merge(details.pop().diff, {_id: obj._id, __v: obj.__v});

assert.deepEqual(obj.toObject(), diff);
assert.equal(obj.testString, 'My default value');

done();
});
Expand Down Expand Up @@ -81,6 +84,7 @@ describe('Document', function(){

obj.historicalRestore(time, function(e, obj){
assert.equal(obj.testObject.testObjectElement, previousValue);
assert.equal(obj.testString, 'My default value');

obj.save(function(e, obj){
document = obj;
Expand Down

0 comments on commit 525aa79

Please sign in to comment.