Skip to content

Commit 60386e4

Browse files
committed
♻️ Make Revision behaviour use the appropriate document methods/properties
1 parent f9165d3 commit 60386e4

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

lib/app/behaviour/revision_behaviour.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@ Revision.setMethod(function compare(left, right) {
201201
*
202202
* @author Jelle De Loecker <jelle@elevenways.be>
203203
* @since 0.0.1
204-
* @version 1.0.5
204+
* @version 1.4.0
205205
*/
206206
Revision.setMethod(function beforeSave(record, options, creating) {
207207

208-
let main = record[this.model.name];
208+
let main = record.$main;
209209

210210
if (!main) {
211-
throw new Error('Unable to find main "' + this.model.name + '" data');
211+
throw new Error('Unable to find main "' + this.model.model_name + '" data');
212212
}
213213

214214
// No revision to save when creating a record
@@ -221,14 +221,12 @@ Revision.setMethod(function beforeSave(record, options, creating) {
221221
next = this.wait('series');
222222

223223
// Find the original record
224-
Model.get(that.model.name).findById(main._id, async function gotRecord(err, result) {
225-
226-
var ori;
224+
Model.get(this.model.model_name).findById(record.$pk, async function gotRecord(err, result) {
227225

228226
if (result) {
229227

230228
// Get the original data
231-
ori = await that.model.convertRecordToDatasourceFormat(result);
229+
let ori = await that.model.convertRecordToDatasourceFormat(result);
232230

233231
// Store the original data in a weakmap for later
234232
revision_before.set(options, ori);
@@ -250,17 +248,11 @@ Revision.setMethod(function beforeSave(record, options, creating) {
250248
*
251249
* @author Jelle De Loecker <jelle@elevenways.be>
252250
* @since 0.0.1
253-
* @version 1.1.0
251+
* @version 1.4.0
254252
*/
255253
Revision.setMethod(function afterSave(record, options, created) {
256254

257-
var that = this,
258-
earlier_data,
259-
right,
260-
main,
261-
that,
262-
left,
263-
next;
255+
let earlier_data;
264256

265257
if (created) {
266258
earlier_data = {};
@@ -273,11 +265,12 @@ Revision.setMethod(function afterSave(record, options, created) {
273265
return;
274266
}
275267

276-
next = this.wait();
277-
main = record[that.model.name] || record;
268+
let doc = this.model.createDocument(record);
269+
let next = this.wait();
270+
const that = this;
278271

279272
// Find the complete saved item
280-
Model.get(that.model.name).findById(main._id, async function gotRecord(err, result) {
273+
Model.get(this.model.model_name).findByPk(doc.$pk, async function gotRecord(err, result) {
281274

282275
if (result) {
283276

@@ -287,8 +280,8 @@ Revision.setMethod(function afterSave(record, options, created) {
287280
if (new_data) {
288281

289282
// Convert the objects so they can be diffed properly
290-
left = JSON.toDryObject(earlier_data);
291-
right = JSON.toDryObject(new_data);
283+
let left = JSON.toDryObject(earlier_data),
284+
right = JSON.toDryObject(new_data);
292285

293286
// Diff them
294287
let delta = that.compare(left, right);
@@ -311,7 +304,7 @@ Revision.setMethod(function afterSave(record, options, created) {
311304

312305
// Add the delta information
313306
revision_data = {
314-
[that.revision_model.name] : revision_data
307+
[that.revision_model.model_name] : revision_data
315308
};
316309

317310
// Save the data

lib/class/model.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,6 @@ Model.setMethod(function auditRecord(document, options, callback) {
11811181
*/
11821182
Model.setMethod(function convertRecordToDatasourceFormat(record, options, callback) {
11831183

1184-
var that = this,
1185-
pledge,
1186-
data;
1187-
11881184
if (typeof options == 'function') {
11891185
callback = options;
11901186
options = {};
@@ -1194,12 +1190,12 @@ Model.setMethod(function convertRecordToDatasourceFormat(record, options, callba
11941190
options = {};
11951191
}
11961192

1197-
data = record[this.name] || record;
1193+
let data = record.$main || record[this.model_name] || record;
11981194

11991195
// Normalize the data
12001196
data = this.compose(data, options);
12011197

1202-
pledge = this.datasource.toDatasource(this, data);
1198+
let pledge = this.datasource.toDatasource(this, data);
12031199

12041200
pledge.handleCallback(callback);
12051201

0 commit comments

Comments
 (0)