diff --git a/app/components/not-active.js b/app/components/not-active.js index a3e1d832..e9b922ac 100644 --- a/app/components/not-active.js +++ b/app/components/not-active.js @@ -1,3 +1,46 @@ import Ember from 'ember'; +import config from 'travis/config/environment'; -export default Ember.Component.extend(); +export default Ember.Component.extend({ + flashes: Ember.inject.service(), + + canActivate: function() { + let user = this.get('user'); + if(user) { + let permissions = user.get('pushPermissions'), + repoId = parseInt(this.get('repo.id')); + + return permissions.contains(repoId); + } + }.property('user.pushPermissions.[]', 'repo'), + + actions: { + activate: function() { + let apiEndpoint = config.apiEndpoint, + repoId = this.get('repo.id'); + + this.set('isActivating', true); + $.ajax(apiEndpoint + '/v3/repo/' + repoId + '/enable', { + headers: { + Authorization: 'token ' + this.get('auth').token() + }, + method: 'POST' + }).then((response) => { + if(response.active) { + let pusher = this.get('pusher'), + repoId = this.get('repo.id'); + + pusher.subscribe('repo-' + repoId); + + this.get('repo').set('active', true); + + this.get('flashes').add('success', 'Repository has been successfully activated.'); + } + this.set('isActivating', false); + }, () => { + this.set('isActivating', false); + this.get('flashes').add('error', 'There was an error while trying to activate the repository.'); + }); + } + } +}); diff --git a/app/instance-initializers/pusher.js b/app/instance-initializers/pusher.js index c969ab91..7c5a0899 100644 --- a/app/instance-initializers/pusher.js +++ b/app/instance-initializers/pusher.js @@ -11,6 +11,7 @@ initialize = function(data) { instantiate: false }); application.inject('route', 'pusher', 'pusher:main'); + application.inject('component', 'pusher', 'pusher:main'); return application.pusher.store = data.container.lookup('service:store'); } }; diff --git a/app/routes/repo/index.js b/app/routes/repo/index.js index 8c0615d6..45e54f54 100644 --- a/app/routes/repo/index.js +++ b/app/routes/repo/index.js @@ -4,7 +4,8 @@ import Config from 'travis/config/environment'; export default TravisRoute.extend({ setupController(controller, model) { this._super.apply(this, arguments); - return this.controllerFor('repo').activate('current'); + this.controllerFor('repo').activate('current'); + controller.set('repo', model); }, deactivate() { diff --git a/app/services/flashes.js b/app/services/flashes.js index 91a7f379..2c72aacc 100644 --- a/app/services/flashes.js +++ b/app/services/flashes.js @@ -48,5 +48,9 @@ export default Ember.Service.extend({ close(msg) { return this.get('flashes').removeObject(msg); + }, + + add(type, message) { + this.loadFlashes([ { [type]: message } ]); } }); diff --git a/app/templates/components/not-active.hbs b/app/templates/components/not-active.hbs index fd055090..4a751b49 100644 --- a/app/templates/components/not-active.hbs +++ b/app/templates/components/not-active.hbs @@ -63,7 +63,14 @@

This is not an active repository

Want to start testing this project on Travis CI?

Read the Docs on Getting Started - {{#if user.pushPermissions.length}} -

If this repository is already in active use,
make sure it’s activated on {{#link-to "account" repo.owner}}your profile{{/link-to}}.

+ + {{#if canActivate}} +

You can activate the repository on {{#link-to "account" repo.owner}}your profile{{/link-to}},
+ or by clicking the button below:

+ + + {{#if isActivating}} + {{loading-indicator}} + {{/if}} {{/if}} diff --git a/app/templates/repo/not-active.hbs b/app/templates/repo/not-active.hbs index 7028061e..6d9cc8af 100644 --- a/app/templates/repo/not-active.hbs +++ b/app/templates/repo/not-active.hbs @@ -1 +1 @@ -{{not-active user=currentUser repo=repo}} +{{not-active user=auth.currentUser repo=repo}} diff --git a/tests/unit/components/not-active-test.js b/tests/unit/components/not-active-test.js new file mode 100644 index 00000000..97876f71 --- /dev/null +++ b/tests/unit/components/not-active-test.js @@ -0,0 +1,40 @@ +import { moduleForComponent, test } from 'ember-qunit'; +import Ember from 'ember'; + +moduleForComponent('not-active', 'Unit | Component | not active', { + // Specify the other units that are required for this test + // needs: ['component:foo', 'helper:bar'], + unit: true +}); + +test('canActivate returns true if a user has access to a repo', function(assert) { + let component = this.subject(); + + let user = Ember.Object.create({ pushPermissions: [1] }); + let repo = Ember.Object.create({ id: 1 }); + + component.set('user', user); + component.set('repo', repo); + + //this.render(); + //assert.equal(this.$().text().trim(), ''); + assert.ok(component.get('canActivate')); +}); + +test("canActivate returns false if user doesn't exist", function(assert) { + let component = this.subject(); + + assert.ok(!component.get('canActivate')); +}); + +test("canActivate returns false if user doesn't push access to the repo", function(assert) { + let component = this.subject(); + + let user = Ember.Object.create({ pushPermissions: [2] }); + let repo = Ember.Object.create({ id: 1 }); + + component.set('user', user); + component.set('repo', repo); + + assert.ok(!component.get('canActivate')); +});