diff --git a/app/components/file-menu.js b/app/components/file-menu.js index 60f9f35f..5ce06e10 100644 --- a/app/components/file-menu.js +++ b/app/components/file-menu.js @@ -42,6 +42,9 @@ export default Ember.Component.extend({ fork(model) { this.sendAction('fork', model); }, + copy() { + this.sendAction('copy'); + }, deleteGist(model) { this.attrs.deleteGist(model); }, diff --git a/app/gist/new/controller.js b/app/gist/new/controller.js new file mode 100644 index 00000000..64773dcb --- /dev/null +++ b/app/gist/new/controller.js @@ -0,0 +1,7 @@ +import Ember from 'ember'; + +export default Ember.Controller.extend({ + queryParams: ['copyCurrentTwiddle'], + + copyCurrentTwiddle: false +}); diff --git a/app/gist/new/route.js b/app/gist/new/route.js index 39d8ae37..5193cf4c 100644 --- a/app/gist/new/route.js +++ b/app/gist/new/route.js @@ -4,14 +4,18 @@ import GistRoute from "ember-twiddle/routes/gist-base-route"; export default GistRoute.extend({ emberCli: Ember.inject.service('ember-cli'), - model () { - this.store.unloadAll('gistFile'); - + model (params) { var model = this.store.createRecord('gist', {description: 'New Twiddle'}); - model.get('files').pushObject(this.get('emberCli').generate('controllers/application')); - model.get('files').pushObject(this.get('emberCli').generate('templates/application')); - model.get('files').pushObject(this.get('emberCli').generate('twiddle.json')); + if (params.copyCurrentTwiddle) { + this.store.peekAll('gistFile').setEach('gist', model); + } else { + this.store.unloadAll('gistFile'); + + model.get('files').pushObject(this.get('emberCli').generate('controllers/application')); + model.get('files').pushObject(this.get('emberCli').generate('templates/application')); + model.get('files').pushObject(this.get('emberCli').generate('twiddle.json')); + } return model; }, diff --git a/app/gist/route.js b/app/gist/route.js index 0bdb343d..177194d4 100644 --- a/app/gist/route.js +++ b/app/gist/route.js @@ -47,6 +47,14 @@ export default Ember.Route.extend({ }).catch(this.catchForkError.bind(this)); }, + copy () { + this.transitionTo('gist.new', { + queryParams: { + copyCurrentTwiddle: true + } + }); + }, + signInViaGithub () { this.session.open('github-oauth2').catch(function(error) { alert('Could not sign you in: ' + error.message); diff --git a/app/gist/template.hbs b/app/gist/template.hbs index df92f602..464a719c 100644 --- a/app/gist/template.hbs +++ b/app/gist/template.hbs @@ -10,6 +10,7 @@ removeFile=(action "removeFile") saveGist="saveGist" fork="fork" + copy="copy" deleteGist=(action "deleteGist") signInViaGithub="signInViaGithub"}} diff --git a/app/templates/components/file-menu.hbs b/app/templates/components/file-menu.hbs index 956db9fa..656abd42 100644 --- a/app/templates/components/file-menu.hbs +++ b/app/templates/components/file-menu.hbs @@ -37,6 +37,8 @@
  • Embed Twiddle
  • {{#unless belongsToUser}}
  • Fork Twiddle
  • + {{else}} +
  • Copy Twiddle
  • {{/unless}}
  • Delete Twiddle
  • {{/unless}} diff --git a/tests/acceptance/gist-test.js b/tests/acceptance/gist-test.js index 3af5d6dd..aafe0574 100644 --- a/tests/acceptance/gist-test.js +++ b/tests/acceptance/gist-test.js @@ -3,6 +3,7 @@ import { module, test } from 'qunit'; import startApp from 'ember-twiddle/tests/helpers/start-app'; import { findMapText } from 'ember-twiddle/tests/helpers/util'; import ErrorMessages from 'ember-twiddle/helpers/error-messages'; +import { stubValidSession } from 'ember-twiddle/tests/helpers/torii'; const firstColumn = '.code:first-of-type'; @@ -223,3 +224,37 @@ test('unsaved indicator', function(assert) { assert.equal(find(indicator).length, 1, "Unsaved indicator reappears after editing"); }); }); + +test('own gist can be copied into a new one', function(assert) { + // set owner of gist as currently logged in user + stubValidSession(this.application, { + currentUser: { login: "Gaurav0" }, + "github-oauth2": {} + }); + + runGist([ + { + filename: 'index/controller.js', + content: `import Ember from 'ember'; + export default Ember.Controller.extend();`, + }, + { + filename: 'index/route.js', + content: 'export default Ember.Route.extend();', + } + ]); + + fillIn('.title input', "my twiddle"); + andThen(function() { + assert.equal(find('.title input').val(), "my twiddle"); + assert.equal(find('.test-unsaved-indicator').length, 0, "No unsaved indicator shown"); + }); + + click('.test-copy-action'); + + andThen(function() { + assert.equal(find('.title input').val(), "New Twiddle", "Description is reset"); + assert.equal(find('.test-unsaved-indicator').length, 1, "Unsaved indicator appears when gist is copied"); + assert.equal(find('.test-copy-action').length, 0, "Menu item to copy gist is not shown anymore"); + }); +}); diff --git a/tests/integration/components/file-menu-test.js b/tests/integration/components/file-menu-test.js index 665236c3..d1053729 100644 --- a/tests/integration/components/file-menu-test.js +++ b/tests/integration/components/file-menu-test.js @@ -11,6 +11,7 @@ moduleForComponent('file-menu', 'Integration | Component | file menu', { this.removeFileCalled = false; this.saveGistCalled = false; this.forkCalled = false; + this.copyCalled = false; this.deleteGistCalled = false; this.signInViaGithubCalled = false; @@ -53,6 +54,7 @@ moduleForComponent('file-menu', 'Integration | Component | file menu', { this.on('removeFile', (file) => { this.removeFileCalled = true; this.removedFile = file; }); this.on('saveGist', (gist) => { this.saveGistCalled = true; this.gistToSave = gist; }); this.on('fork', (gist) => { this.forkCalled = true; this.gistToFork = gist; }); + this.on('copy', () => { this.copyCalled = true; }); this.on('deleteGist', (gist) => { this.deleteGistCalled = true; this.gistToDelete = gist; }); this.on('signInViaGithub', () => { this.signInViaGithubCalled = true; }); @@ -65,6 +67,7 @@ moduleForComponent('file-menu', 'Integration | Component | file menu', { removeFile=(action "removeFile") saveGist="saveGist" fork="fork" + copy="copy" deleteGist=(action "deleteGist") signInViaGithub="signInViaGithub"}}`); } @@ -115,6 +118,16 @@ test("it calls fork on clicking 'Fork Twiddle'", function(assert) { assert.equal(this.gistToFork, this.gist, 'fork was called with gist to fork'); }); +test("it calls copy on clicking 'Copy Twiddle'", function(assert) { + assert.expect(1); + + // logged in user is the same as the owner of the gist + this.set('session.currentUser.login', 'Gaurav0'); + this.$('.test-copy-action').click(); + + assert.ok(this.copyCalled, 'copy was called'); +}); + test("it calls deleteGist on clicking 'Delete Twiddle'", function(assert) { assert.expect(2);