From 25e7fd7a2e91578d00f710a555c5e8f7dbab9aa9 Mon Sep 17 00:00:00 2001 From: Bill Heaton Date: Fri, 13 Jan 2017 14:24:22 -0800 Subject: [PATCH] Update rollback methods, not to behave like undo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix behavior for rollbackAttributes. The intent is to rollback to the value fetched from the persistence layer; not to stash an attribute’s value on every call to `.set()`. - Fix failing tests for previousAttributes and rollbackAttributes - Update `attr()` `set()` method to only assign to `.previous` once --- addon/utils/attr.js | 6 ++++-- tests/unit/models/resource-test.js | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/addon/utils/attr.js b/addon/utils/attr.js index 0da3be5..2c6bdbc 100644 --- a/addon/utils/attr.js +++ b/addon/utils/attr.js @@ -71,9 +71,11 @@ export default function attr(type = 'any', mutable = true) { this.set('attributes.' + key, value); if (!this.get('isNew')) { this._attributes[key] = this._attributes[key] || {}; + if (this._attributes[key].previous === undefined) { + this._attributes[key].previous = lastValue; + } this._attributes[key].changed = value; - this._attributes[key].previous = lastValue; - const service = this.get('service'); + let service = this.get('service'); if (service) { service.trigger('attributeChanged', this); } diff --git a/tests/unit/models/resource-test.js b/tests/unit/models/resource-test.js index 7c8991d..268197e 100644 --- a/tests/unit/models/resource-test.js +++ b/tests/unit/models/resource-test.js @@ -208,11 +208,11 @@ test('#rollbackAttributes resets attributes based on #previousAttributes', funct test('#rollbackRelationships resets relationships', function(assert) { let post = createPostWithRelationships.call(this); - let ogAuthorId = post.get('relationships.author.data.id'); + const ogAuthorId = post.get('relationships.author.data.id'); let relationships = post.get('relationships'); post.addRelationship('author', '5'); - assert.notEqual(relationships.author.id, ogAuthorId, 'author changed'); + assert.notEqual(relationships.author.data.id, ogAuthorId, 'author changed'); assert.equal(relationships.comments.data.length, 1, 'one comment'); post.removeRelationships('comments', ['3']); @@ -221,6 +221,19 @@ test('#rollbackRelationships resets relationships', function(assert) { let changes = post.get('changedRelationships'); assert.equal(changes.length, 2, 'two relationships were changed'); + post.addRelationship('author', '6'); + assert.equal(relationships.author.data.id, 6, 'author changed'); + post.addRelationships('comments', ['4', '5']); + assert.equal(relationships.comments.data.length, 2, 'two comments added'); + + changes = post.get('changedRelationships'); + assert.equal(changes.length, 2, 'two relationships were changed'); + + post.removeRelationship('author'); + assert.equal(relationships.author.id, null, 'no author'); + changes = post.get('changedRelationships'); + assert.equal(changes.length, 2, 'two relationships were changed'); + post.rollbackRelationships(); changes = post.get('changedRelationships');