diff --git a/app/components/file-menu.js b/app/components/file-menu.js index fad5b638..e8444c42 100644 --- a/app/components/file-menu.js +++ b/app/components/file-menu.js @@ -21,7 +21,7 @@ export default Ember.Component.extend({ prompt('Ctrl + C ;-)', window.location.href); }, fork(model) { - this.attrs.fork(model); + this.sendAction('fork', model); }, deleteGist(model) { this.attrs.deleteGist(model); diff --git a/app/gist/controller.js b/app/gist/controller.js index 1577dc08..5283bd6f 100644 --- a/app/gist/controller.js +++ b/app/gist/controller.js @@ -161,12 +161,6 @@ export default Ember.Controller.extend({ } }, - fork (gist) { - gist.fork().then((response) => { - this.transitionToRoute('gist.edit', response.id); - }); - }, - removeFile (file) { if(confirm(`Are you sure you want to remove this file?\n\n${file.get('filePath')}`)) { file.deleteRecord(); diff --git a/app/gist/route.js b/app/gist/route.js index f5581dca..57f060d1 100644 --- a/app/gist/route.js +++ b/app/gist/route.js @@ -26,6 +26,19 @@ export default Ember.Route.extend({ this.get('controller').set('unsaved', false); }, + fork (gist) { + gist.fork().then((response) => { + this.get('store').find('gist', response.id).then((newGist) => { + gist.get('files').toArray().forEach((file) => { + file.set('gist', newGist); + }); + return newGist.save().then(() => { + this.transitionTo('gist.edit', newGist); + }); + }); + }).catch(this.catchForkError.bind(this)); + }, + signInViaGithub () { this.session.open('github-oauth2').catch(function(error) { alert('Could not sign you in: ' + error.message); @@ -36,5 +49,16 @@ export default Ember.Route.extend({ signOut () { this.session.close(); } + }, + + catchForkError(error) { + if (error && error.errors) { + let firstError = error.errors[0]; + if (firstError.code === "unprocessable" && firstError.field === "forks") { + this.notify.info("You already own this gist."); + return; + } + } + throw error; } }); diff --git a/app/gist/template.hbs b/app/gist/template.hbs index 6e67fb84..2324ad68 100644 --- a/app/gist/template.hbs +++ b/app/gist/template.hbs @@ -7,7 +7,7 @@ renameFile=(action "renameFile") removeFile=(action "removeFile") saveGist="saveGist" - fork=(action "fork") + fork="fork" deleteGist=(action "deleteGist") signInViaGithub="signInViaGithub" }} diff --git a/tests/integration/components/file-menu-test.js b/tests/integration/components/file-menu-test.js index ebc2fbcb..ee5bdda5 100644 --- a/tests/integration/components/file-menu-test.js +++ b/tests/integration/components/file-menu-test.js @@ -60,7 +60,7 @@ moduleForComponent('file-menu', 'Integration | Component | file menu', { renameFile=(action "renameFile") removeFile=(action "removeFile") saveGist="saveGist" - fork=(action "fork") + fork="fork" deleteGist=(action "deleteGist") signInViaGithub="signInViaGithub"}}`); }