-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Make Revision behaviour use the appropriate document methods/prope…
…rties
- Loading branch information
Showing
2 changed files
with
16 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,14 +201,14 @@ Revision.setMethod(function compare(left, right) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.0.1 | ||
* @version 1.0.5 | ||
* @version 1.4.0 | ||
*/ | ||
Revision.setMethod(function beforeSave(record, options, creating) { | ||
|
||
let main = record[this.model.name]; | ||
let main = record.$main; | ||
|
||
if (!main) { | ||
throw new Error('Unable to find main "' + this.model.name + '" data'); | ||
throw new Error('Unable to find main "' + this.model.model_name + '" data'); | ||
} | ||
|
||
// No revision to save when creating a record | ||
|
@@ -221,14 +221,12 @@ Revision.setMethod(function beforeSave(record, options, creating) { | |
next = this.wait('series'); | ||
|
||
// Find the original record | ||
Model.get(that.model.name).findById(main._id, async function gotRecord(err, result) { | ||
|
||
var ori; | ||
Model.get(this.model.model_name).findById(record.$pk, async function gotRecord(err, result) { | ||
|
||
if (result) { | ||
|
||
// Get the original data | ||
ori = await that.model.convertRecordToDatasourceFormat(result); | ||
let ori = await that.model.convertRecordToDatasourceFormat(result); | ||
|
||
// Store the original data in a weakmap for later | ||
revision_before.set(options, ori); | ||
|
@@ -250,17 +248,11 @@ Revision.setMethod(function beforeSave(record, options, creating) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.0.1 | ||
* @version 1.1.0 | ||
* @version 1.4.0 | ||
*/ | ||
Revision.setMethod(function afterSave(record, options, created) { | ||
|
||
var that = this, | ||
earlier_data, | ||
right, | ||
main, | ||
that, | ||
left, | ||
next; | ||
let earlier_data; | ||
|
||
if (created) { | ||
earlier_data = {}; | ||
|
@@ -273,11 +265,12 @@ Revision.setMethod(function afterSave(record, options, created) { | |
return; | ||
} | ||
|
||
next = this.wait(); | ||
main = record[that.model.name] || record; | ||
let doc = this.model.createDocument(record); | ||
let next = this.wait(); | ||
const that = this; | ||
|
||
// Find the complete saved item | ||
Model.get(that.model.name).findById(main._id, async function gotRecord(err, result) { | ||
Model.get(this.model.model_name).findByPk(doc.$pk, async function gotRecord(err, result) { | ||
|
||
if (result) { | ||
|
||
|
@@ -287,8 +280,8 @@ Revision.setMethod(function afterSave(record, options, created) { | |
if (new_data) { | ||
|
||
// Convert the objects so they can be diffed properly | ||
left = JSON.toDryObject(earlier_data); | ||
right = JSON.toDryObject(new_data); | ||
let left = JSON.toDryObject(earlier_data), | ||
right = JSON.toDryObject(new_data); | ||
|
||
// Diff them | ||
let delta = that.compare(left, right); | ||
|
@@ -311,7 +304,7 @@ Revision.setMethod(function afterSave(record, options, created) { | |
|
||
// Add the delta information | ||
revision_data = { | ||
[that.revision_model.name] : revision_data | ||
[that.revision_model.model_name] : revision_data | ||
}; | ||
|
||
// Save the data | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters