Skip to content

Commit

Permalink
Rename Model.rollback() to Model.rollbackAttributes()
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicEric committed Jun 11, 2015
1 parent 354c6d7 commit 78564c1
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 412 deletions.
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ InternalModel.prototype = {
Ember Data has 3 buckets for storing the value of an attribute on an internalModel.
`_data` holds all of the attributes that have been acknowledged by
a backend via the adapter. When rollback is called on a model all
a backend via the adapter. When rollbackAttributes is called on a model all
attributes will revert to the record's state in `_data`.
`_attributes` holds any change the user has made to an attribute
Expand Down
28 changes: 25 additions & 3 deletions packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ var Model = Ember.Object.extend(Ember.Evented, {
/**
Marks the record as deleted but does not save it. You must call
`save` afterwards if you want to persist it. You might use this
method if you want to allow the user to still `rollback()` a
delete after it was made.
method if you want to allow the user to still `rollbackAttributes()`
after a delete it was made.
Example
Expand All @@ -486,7 +486,7 @@ var Model = Ember.Object.extend(Ember.Evented, {
this.controller.get('model').save();
},
undo: function() {
this.controller.get('model').rollback();
this.controller.get('model').rollbackAttributes();
}
}
});
Expand Down Expand Up @@ -621,8 +621,30 @@ var Model = Ember.Object.extend(Ember.Evented, {
```
@method rollback
@deprecated Use `addAttributes()` instead
*/
rollback: function() {
Ember.deprecate('Using model.rollback() has been deprecated. Use model.rollbackAttributes() to discard any unsaved changes to a model.');
this.rollbackAttributes();
},

/**
If the model `isDirty` this function will discard any unsaved
changes. If the model `isNew` it will be removed from the store.
Example
```javascript
record.get('name'); // 'Untitled Document'
record.set('name', 'Doc 1');
record.get('name'); // 'Doc 1'
record.rollbackAttributes();
record.get('name'); // 'Untitled Document'
```
@method rollbackAttributes
*/
rollbackAttributes: function() {
this._internalModel.rollback();
},

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ var DirtyState = {
},

rollback: function(internalModel) {
internalModel.rollback();
internalModel.rollbackAttributes();
internalModel.triggerLater('ready');
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test("when deleted records are rolled back, they are still in their previous rec

run(function() {
jaime.deleteRecord();
jaime.rollback();
jaime.rollbackAttributes();
});
equal(all.get('length'), 2, 'record was not removed');
equal(filtered.get('length'), 2, 'record was not removed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ test("A sync belongsTo errors out if the record is unlaoded", function() {
}, /You looked up the 'user' relationship on a 'message' with id 1 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async \(`DS.belongsTo\({ async: true }\)`\)/);
});

test("Rollbacking a deleted record restores implicit relationship - async", function () {
test("Rollbacking attributes for a deleted record restores implicit relationship - async", function () {
Book.reopen({
author: DS.belongsTo('author', { async: true })
});
Expand All @@ -592,24 +592,24 @@ test("Rollbacking a deleted record restores implicit relationship - async", func
});
run(function() {
author.deleteRecord();
author.rollback();
author.rollbackAttributes();
book.get('author').then(function(fetchedAuthor) {
equal(fetchedAuthor, author, 'Book has an author after rollback');
equal(fetchedAuthor, author, 'Book has an author after rollback attributes');
});
});
});

test("Rollbacking a deleted record restores implicit relationship - sync", function () {
test("Rollbacking attributes for a deleted record restores implicit relationship - sync", function () {
var book, author;
run(function() {
book = env.store.push('book', { id: 1, name: "Stanley's Amazing Adventures", author: 2 });
author = env.store.push('author', { id: 2, name: 'Stanley' });
});
run(function() {
author.deleteRecord();
author.rollback();
author.rollbackAttributes();
});
equal(book.get('author'), author, 'Book has an author after rollback');
equal(book.get('author'), author, 'Book has an author after rollback attributes');
});

test("Passing a model as type to belongsTo should not work", function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,39 +1060,39 @@ test("If reordered hasMany data has been pushed to the store, the many array ref
deepEqual(post.get('comments').toArray(), [comment4, comment2, comment3, comment1], 'Updated ordering is correct');
});

test("Rollbacking a deleted record restores implicit relationship correctly when the hasMany side has been deleted - async", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the hasMany side has been deleted - async", function () {
var book, chapter;
run(function() {
book = env.store.push('book', { id: 1, title: "Stanley's Amazing Adventures", chapters: [2] });
chapter = env.store.push('chapter', { id: 2, title: 'Sailing the Seven Seas' });
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
book.get('chapters').then(function(fetchedChapters) {
equal(fetchedChapters.objectAt(0), chapter, 'Book has a chapter after rollback');
equal(fetchedChapters.objectAt(0), chapter, 'Book has a chapter after rollback attributes');
});
});
});

test("Rollbacking a deleted record restores implicit relationship correctly when the hasMany side has been deleted - sync", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the hasMany side has been deleted - sync", function () {
var book, chapter;
run(function() {
book = env.store.push('book', { id: 1, title: "Stanley's Amazing Adventures", chapters: [2] });
chapter = env.store.push('chapter', { id: 2, title: 'Sailing the Seven Seas' });
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
equal(book.get('chapters.firstObject'), chapter, "Book has a chapter after rollback");
equal(book.get('chapters.firstObject'), chapter, "Book has a chapter after rollback attributes");
});
});

test("Rollbacking a deleted record restores implicit relationship correctly when the belongsTo side has been deleted - async", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the belongsTo side has been deleted - async", function () {
Page.reopen({
chapter: DS.belongsTo('chapter', { async: true })
});
Expand All @@ -1103,27 +1103,27 @@ test("Rollbacking a deleted record restores implicit relationship correctly when
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
page.get('chapter').then(function(fetchedChapter) {
equal(fetchedChapter, chapter, 'Page has a chapter after rollback');
equal(fetchedChapter, chapter, 'Page has a chapter after rollback attributes');
});
});
});

test("Rollbacking a deleted record restores implicit relationship correctly when the belongsTo side has been deleted - sync", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the belongsTo side has been deleted - sync", function () {
var chapter, page;
run(function() {
chapter = env.store.push('chapter', { id: 2, title: 'Sailing the Seven Seas' });
page = env.store.push('page', { id: 3, number: 1, chapter: 2 });
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
equal(page.get('chapter'), chapter, "Page has a chapter after rollback");
equal(page.get('chapter'), chapter, "Page has a chapter after rollback attributes");
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,18 @@ test("Deleting a record that has a hasMany relationship removes it from the othe
});

/*
Rollback tests
Rollback Attributes tests
*/

test("Rollbacking a deleted record that has a ManyToMany relationship works correctly - async", function () {
test("Rollbacking attributes for a deleted record that has a ManyToMany relationship works correctly - async", function () {
var user, topic;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley', topics: [2] });
topic = store.push('topic', { id: 2, title: 'EmberFest was great' });
});
run(function() {
topic.deleteRecord();
topic.rollback();
topic.rollbackAttributes();
});
run(function() {
topic.get('users').then(async(function(fetchedUsers) {
Expand All @@ -235,13 +235,13 @@ test("Deleting a record that has a hasMany relationship removes it from the othe
});
run(function() {
account.deleteRecord();
account.rollback();
account.rollbackAttributes();
});
equal(account.get('users.length'), 1, 'Users are still there');
equal(user.get('accounts.length'), 1, 'Account got rolledback correctly into the user');
});

test("Rollbacking a created record that has a ManyToMany relationship works correctly - async", function () {
test("Rollbacking attributes for a created record that has a ManyToMany relationship works correctly - async", function () {
var user, topic;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley' });
Expand All @@ -250,7 +250,7 @@ test("Rollbacking a created record that has a ManyToMany relationship works corr
run(function() {
user.get('topics').then(async(function(fetchedTopics) {
fetchedTopics.pushObject(topic);
topic.rollback();
topic.rollbackAttributes();
topic.get('users').then(async(function(fetchedUsers) {
equal(fetchedUsers.get('length'), 0, 'Users got removed');
equal(fetchedUsers.objectAt(0), null, "User can't be fetched");
Expand All @@ -271,7 +271,7 @@ test("Deleting a record that has a hasMany relationship removes it from the othe
});
run(function() {
account.get('users').pushObject(user);
user.rollback();
user.rollbackAttributes();
});
equal(account.get('users.length'), 0, 'Users got removed');
equal(user.get('accounts.length'), undefined, 'Accounts got rolledback correctly');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,18 @@ test("When deleting a record that has a hasMany it is removed from the belongsTo
});

/*
Rollback from deleted state
Rollback attributes from deleted state
*/

test("Rollbacking a deleted record works correctly when the hasMany side has been deleted - async", function () {
test("Rollbacking attributes of a deleted record works correctly when the hasMany side has been deleted - async", function () {
var user, message;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley', messages: [2] });
message = store.push('message', { id: 2, title: 'EmberFest was great' });
});
run(function() {
message.deleteRecord();
message.rollback();
message.rollbackAttributes();
});
run(function() {
message.get('user').then(function(fetchedUser) {
Expand All @@ -469,29 +469,29 @@ test("Rollbacking a deleted record works correctly when the hasMany side has bee
});
});

test("Rollbacking a deleted record works correctly when the hasMany side has been deleted - sync", function () {
test("Rollbacking attributes of a deleted record works correctly when the hasMany side has been deleted - sync", function () {
var account, user;
run(function() {
account = store.push('account', { id: 2 , state: 'lonely' });
user = store.push('user', { id: 1, name: 'Stanley', accounts: [2] });
});
run(function() {
account.deleteRecord();
account.rollback();
account.rollbackAttributes();
});
equal(user.get('accounts.length'), 1, "Accounts are rolled back");
equal(account.get('user'), user, 'Account still has the user');
});

test("Rollbacking a deleted record works correctly when the belongsTo side has been deleted - async", function () {
test("Rollbacking attributes of deleted record works correctly when the belongsTo side has been deleted - async", function () {
var user, message;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley', messages: [2] });
message = store.push('message', { id: 2, title: 'EmberFest was great' });
});
run(function() {
user.deleteRecord();
user.rollback();
user.rollbackAttributes();
});
run(function() {
message.get('user').then(function(fetchedUser) {
Expand All @@ -503,31 +503,31 @@ test("Rollbacking a deleted record works correctly when the belongsTo side has b
});
});

test("Rollbacking a deleted record works correctly when the belongsTo side has been deleted - sync", function () {
test("Rollbacking attributes of a deleted record works correctly when the belongsTo side has been deleted - sync", function () {
var account, user;
run(function() {
account = store.push('account', { id: 2 , state: 'lonely' });
user = store.push('user', { id: 1, name: 'Stanley', accounts: [2] });
});
run(function() {
user.deleteRecord();
user.rollback();
user.rollbackAttributes();
});
equal(user.get('accounts.length'), 1, "User still has the accounts");
equal(account.get('user'), user, 'Account has the user again');
});

/*
Rollback from created state
Rollback attributes from created state
*/

test("Rollbacking a created record works correctly when the hasMany side has been created - async", function () {
test("Rollbacking attributes of a created record works correctly when the hasMany side has been created - async", function () {
var user, message;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley' });
message = store.createRecord('message', { user: user });
});
run(message, 'rollback');
run(message, 'rollbackAttributes');
run(function() {
message.get('user').then(function(fetchedUser) {
equal(fetchedUser, null, 'Message does not have the user anymore');
Expand All @@ -539,18 +539,18 @@ test("Rollbacking a created record works correctly when the hasMany side has bee
});
});

test("Rollbacking a created record works correctly when the hasMany side has been created - sync", function () {
test("Rollbacking attributes of a created record works correctly when the hasMany side has been created - sync", function () {
var user, account;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley' });
account = store.createRecord('account', { user: user });
});
run(account, 'rollback');
run(account, 'rollbackAttributes');
equal(user.get('accounts.length'), 0, "Accounts are rolled back");
equal(account.get('user'), null, 'Account does not have the user anymore');
});

test("Rollbacking a created record works correctly when the belongsTo side has been created - async", function () {
test("Rollbacking attributes of a created record works correctly when the belongsTo side has been created - async", function () {
var message, user;
run(function() {
message = store.push('message', { id: 2, title: 'EmberFest was great' });
Expand All @@ -559,7 +559,7 @@ test("Rollbacking a created record works correctly when the belongsTo side has b
run(function() {
user.get('messages').then(function(messages) {
messages.pushObject(message);
user.rollback();
user.rollbackAttributes();
message.get('user').then(function(fetchedUser) {
equal(fetchedUser, null, 'Message does not have the user anymore');
});
Expand All @@ -571,7 +571,7 @@ test("Rollbacking a created record works correctly when the belongsTo side has b
});
});

test("Rollbacking a created record works correctly when the belongsTo side has been created - sync", function () {
test("Rollbacking attributes of a created record works correctly when the belongsTo side has been created - sync", function () {
var account, user;
run(function() {
account = store.push('account', { id: 2 , state: 'lonely' });
Expand All @@ -580,7 +580,7 @@ test("Rollbacking a created record works correctly when the belongsTo side has b
run(function() {
user.get('accounts').pushObject(account);
});
run(user, 'rollback');
run(user, 'rollbackAttributes');
equal(user.get('accounts.length'), undefined, "User does not have the account anymore");
equal(account.get('user'), null, 'Account does not have the user anymore');
});
Loading

0 comments on commit 78564c1

Please sign in to comment.