Skip to content

Commit

Permalink
Make save options instead of using a boolean to increase readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
stacey-gammon committed Dec 30, 2016
1 parent 5875c15 commit 1701b9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Expand Up @@ -158,7 +158,7 @@ uiModules.get('apps/management')
return service.get().then(function (obj) {
obj.id = doc._id;
return obj.applyESResp(doc).then(function () {
return obj.save(true);
return obj.save({ confirmOverwrite : true });
});
});
})
Expand Down
10 changes: 5 additions & 5 deletions src/ui/public/courier/__tests__/saved_object.js
Expand Up @@ -128,7 +128,7 @@ describe('Saved Object', function () {

savedObject.lastSavedTitle = 'original title';
savedObject.title = 'new title';
return savedObject.save(true)
return savedObject.save({ confirmOverwrite : true })
.then(() => {
expect(window.confirm.called).to.be(true);
expect(savedObject.id).to.be('myId');
Expand All @@ -148,7 +148,7 @@ describe('Saved Object', function () {

savedObject.lastSavedTitle = 'original title';
savedObject.title = 'new title';
return savedObject.save(true)
return savedObject.save({ confirmOverwrite : true })
.then(() => {
expect(savedObject.id).to.be('HI');
expect(savedObject.isSaving).to.be(false);
Expand All @@ -164,11 +164,11 @@ describe('Saved Object', function () {
stubConfirmOverwrite();
esAdminStub.index.restore();
esDataStub.index.restore();

sinon.stub(esAdminStub, 'index').returns(BluebirdPromise.reject());
sinon.stub(esDataStub, 'index').returns(BluebirdPromise.reject());

return savedObject.save(true)
return savedObject.save({ confirmOverwrite : true })
.then(() => {
expect(true).to.be(false); // Force failure, the save should not succeed.
})
Expand All @@ -188,7 +188,7 @@ describe('Saved Object', function () {
});

stubConfirmOverwrite();
return savedObject.save(false)
return savedObject.save({ confirmOverwrite : false })
.then(() => {
expect(window.confirm.called).to.be(false);
});
Expand Down
14 changes: 10 additions & 4 deletions src/ui/public/courier/saved_object/saved_object.js
Expand Up @@ -285,15 +285,21 @@ export default function SavedObjectFactory(esAdmin, kbnIndex, Promise, Private,
});
};


/**
* @typedef {Object} SaveOptions
* @property {boolean} confirmOverwrite=false - If true, attempts to create the source so it
* can confirm an overwrite if a document with the id already exists. Defaults to false.
*/

/**
* Saves this object.
*
* @param {bool} confirmOverwrite=false If true, attempts to create the source so it
* can confirm an overwrite if a document with the id already exists. Defaults to false.
* @param {SaveOptions} saveOptions?
* @return {Promise}
* @resolved {String} - The id of the doc
*/
this.save = (confirmOverwrite = false) => {
this.save = (saveOptions = {}) => {
// Save the original id in case the save fails.
const originalId = this.id;
// Read https://github.com/elastic/kibana/issues/9056 and
Expand All @@ -314,7 +320,7 @@ export default function SavedObjectFactory(esAdmin, kbnIndex, Promise, Private,
const source = this.serialize();

this.isSaving = true;
const doSave = confirmOverwrite ? createSource(source) : docSource.doIndex(source);
const doSave = saveOptions.confirmOverwrite ? createSource(source) : docSource.doIndex(source);
return doSave
.then((id) => { this.id = id; })
.then(refreshIndex)
Expand Down

0 comments on commit 1701b9c

Please sign in to comment.