diff --git a/app/mixins/elevated-access.js b/app/mixins/elevated-access.js new file mode 100644 index 0000000..15607e3 --- /dev/null +++ b/app/mixins/elevated-access.js @@ -0,0 +1,22 @@ +import Ember from 'ember'; + +const { Mixin , inject: { service } } = Ember; + +export default Mixin.create({ + currentUser: service('session-account'), + + beforeModel: function(transition) { + this._super(transition); + this.get('currentUser.account').then((user) => { + if (user.get('isReader')) { + this.transitionTo(this.get('fallbackRouteBefore')); + } + }); + }, + + afterModel: function(model) { + if (!model.get('canEdit')) { + this.transitionTo(this.get('fallbackRouteAfter'), model.get('id')); + } + }, +}); diff --git a/app/mixins/save-model.js b/app/mixins/save-model.js index 5db43fa..0f83491 100644 --- a/app/mixins/save-model.js +++ b/app/mixins/save-model.js @@ -1,7 +1,9 @@ import Ember from 'ember'; import ajaxError from '../utils/ajax-error'; -export default Ember.Mixin.create({ +const { Mixin } = Ember; + +export default Mixin.create({ actions: { save: function() { const model = this.get('model'); @@ -20,12 +22,11 @@ export default Ember.Mixin.create({ cancel: function() { const model = this.get('model'); - const fallbackRoute = this.get('fallbackRoute'); model.get('errors').clear(); model.rollbackAttributes(); - this.transitionToRoute(fallbackRoute, model); + this.transitionToRoute(this.get('fallbackRoute'), model); }, }, }); diff --git a/app/pods/protected/species/edit/route.js b/app/pods/protected/species/edit/route.js index 7767f41..f429c37 100644 --- a/app/pods/protected/species/edit/route.js +++ b/app/pods/protected/species/edit/route.js @@ -1,23 +1,10 @@ import Ember from 'ember'; +import ElevatedAccess from '../../../../mixins/elevated-access'; -const { Route, inject: { service } } = Ember; - -export default Route.extend({ - currentUser: service('session-account'), - - beforeModel: function(transition) { - this._super(transition); - this.get('currentUser.account').then((user) => { - if (user.get('isReader')) { - this.transitionTo('protected.species.index'); - } - }); - }, - - afterModel: function(model) { - if (!model.get('canEdit')) { - this.transitionTo('species.show', model.get('id')); - } - }, +const { Route } = Ember; +export default Route.extend(ElevatedAccess, { + // Required for ElevatedAccess mixin + fallbackRouteBefore: 'protected.species.index', + fallbackRouteAfter: 'protected.species.show', }); diff --git a/tests/unit/mixins/elevated-access-test.js b/tests/unit/mixins/elevated-access-test.js new file mode 100644 index 0000000..9791fc6 --- /dev/null +++ b/tests/unit/mixins/elevated-access-test.js @@ -0,0 +1,12 @@ +import Ember from 'ember'; +import ElevatedAccessMixin from '../../../mixins/elevated-access'; +import { module, test } from 'qunit'; + +module('Unit | Mixin | elevated access'); + +// Replace this with your real tests. +test('it works', function(assert) { + var ElevatedAccessObject = Ember.Object.extend(ElevatedAccessMixin); + var subject = ElevatedAccessObject.create(); + assert.ok(subject); +});