diff --git a/app/mirage/config.js b/app/mirage/config.js index e39ccee..935eb17 100644 --- a/app/mirage/config.js +++ b/app/mirage/config.js @@ -36,7 +36,7 @@ export function testConfig() { return { strain: db.strains.find(request.params.id), species: db.species, // Just send back everything we've got - } + }; }); this.put('/strains/:id'); } diff --git a/app/mixins/save-model.js b/app/mixins/save-model.js index 8d3b233..d4459e3 100644 --- a/app/mixins/save-model.js +++ b/app/mixins/save-model.js @@ -13,17 +13,12 @@ export default Mixin.create({ const fallbackRoute = this.get('fallbackRouteSave'); model.setProperties(properties); - - if (model.get('hasDirtyAttributes')) { - model.save().then((model) => { - this.get('flashMessages').clearMessages(); - this.transitionToRoute(fallbackRoute, model); - }, () => { - ajaxError(model.get('errors'), this.get('flashMessages')); - }); - } else { + model.save().then((model) => { + this.get('flashMessages').clearMessages(); this.transitionToRoute(fallbackRoute, model); - } + }, () => { + ajaxError(model.get('errors'), this.get('flashMessages')); + }); }, cancel: function() { diff --git a/app/models/strain.js b/app/models/strain.js index 72d4ff6..0c59f2f 100644 --- a/app/models/strain.js +++ b/app/models/strain.js @@ -25,6 +25,10 @@ export default DS.Model.extend({ return Ember.String.htmlSafe(`${this.get('strainName')}${type}`); }.property('strainName', 'typeStrain').readOnly(), + fullName: Ember.computed('species', 'strainName', function() { + return `${this.get('species.speciesName')} ${this.get('strainNameMU')}`; + }), + fullNameMU: function() { return Ember.String.htmlSafe(`${this.get('species.speciesName')} ${this.get('strainNameMU')}`); }.property('species', 'strainNameMU').readOnly(), diff --git a/app/pods/protected/strains/edit/template.hbs b/app/pods/protected/strains/edit/template.hbs index 822ac93..1ae2d5b 100644 --- a/app/pods/protected/strains/edit/template.hbs +++ b/app/pods/protected/strains/edit/template.hbs @@ -2,7 +2,6 @@ protected/strains/strain-form strain=model speciesList=speciesList - canAdd=metaData.canAdd on-save=(action "save") on-cancel=(action "cancel") }} diff --git a/app/pods/protected/strains/index/strain-table/component.js b/app/pods/protected/strains/index/strain-table/component.js index 0e801ca..7b3b21c 100644 --- a/app/pods/protected/strains/index/strain-table/component.js +++ b/app/pods/protected/strains/index/strain-table/component.js @@ -6,7 +6,7 @@ const { Component, computed: { sort } } = Ember; export default Component.extend(SetupMetaData, { strains: null, - sortParams: ['fullNameMU'], + sortParams: ['fullName'], sortedStrains: sort('strains', 'sortParams'), }); diff --git a/app/pods/protected/strains/new/controller.js b/app/pods/protected/strains/new/controller.js index d3e1b36..a38edbb 100644 --- a/app/pods/protected/strains/new/controller.js +++ b/app/pods/protected/strains/new/controller.js @@ -1,29 +1,10 @@ import Ember from 'ember'; -import ajaxError from '../../../../utils/ajax-error'; +import SaveModel from '../../../../mixins/save-model'; -export default Ember.Controller.extend({ - actions: { - save: function() { - let strain = this.get('strain'); +const { Controller } = Ember; - if (strain.get('hasDirtyAttributes')) { - strain.save().then((strain) => { - this.transitionToRoute('protected.strains.show', strain); - }, () => { - ajaxError(strain.get('errors'), this.get('flashMessages')); - }); - } else { - strain.destroyRecord().then(() => { - this.transitionToRoute('protected.strains.index'); - }); - } - }, - - cancel: function() { - this.get('strain').destroyRecord().then(() => { - this.transitionToRoute('protected.strains.index'); - }); - }, - - }, +export default Controller.extend(SaveModel, { + // Required for SaveModel mixin + fallbackRouteSave: 'protected.strains.show', + fallbackRouteCancel: 'protected.strains.index', }); diff --git a/app/pods/protected/strains/new/route.js b/app/pods/protected/strains/new/route.js index 837b713..a9f4c6f 100644 --- a/app/pods/protected/strains/new/route.js +++ b/app/pods/protected/strains/new/route.js @@ -1,16 +1,12 @@ import Ember from 'ember'; +import ElevatedAccess from '../../../../mixins/elevated-access'; -export default Ember.Route.extend({ - currentUser: Ember.inject.service('session-account'), +const { Route } = Ember; - beforeModel: function(transition) { - this._super(transition); - this.get('currentUser.account').then((user) => { - if (user.get('isReader')) { - this.transitionTo('protected.strains.index'); - } - }); - }, +export default Route.extend(ElevatedAccess, { + // Required for ElevatedAccess mixin + fallbackRouteBefore: 'protected.strains.index', + fallbackRouteAfter: 'protected.strains.show', model: function() { return Ember.RSVP.hash({ @@ -19,19 +15,28 @@ export default Ember.Route.extend({ }); }, + // Overriding afterModel because of RSVP hash + afterModel: function(models) { + if (!models.strain.get('isNew') && !models.strain.get('canEdit')) { + this.transitionTo(this.get('fallbackRouteAfter'), models.strain.get('id')); + } + }, + + // Setting up controller because of RSVP hash setupController: function(controller, models) { - controller.setProperties(models); + controller.set('model', models.strain); + controller.set('speciesList', models.species); }, actions: { + // Overriding willTransition because of RSVP hash willTransition: function(/*transition*/) { const controller = this.get('controller'); - const strain = controller.get('strain'); + const model = controller.get('model'); - if (strain.get('isNew')) { - strain.destroyRecord(); + if (model.get('isNew')) { + model.destroyRecord(); } }, }, - }); diff --git a/app/pods/protected/strains/new/template.hbs b/app/pods/protected/strains/new/template.hbs index d9c4d43..1ae2d5b 100644 --- a/app/pods/protected/strains/new/template.hbs +++ b/app/pods/protected/strains/new/template.hbs @@ -1,7 +1,7 @@ {{ protected/strains/strain-form - strain=strain - species=species - save="save" - cancel="cancel" + strain=model + speciesList=speciesList + on-save=(action "save") + on-cancel=(action "cancel") }} diff --git a/app/pods/protected/strains/show/strain-card/template.hbs b/app/pods/protected/strains/show/strain-card/template.hbs index 989db71..58ff553 100644 --- a/app/pods/protected/strains/show/strain-card/template.hbs +++ b/app/pods/protected/strains/show/strain-card/template.hbs @@ -98,7 +98,7 @@ {{#if strain.canEdit}}
- {{#link-to 'protected.strains.edit' strain class="button-gray smaller"}} + {{#link-to 'protected.strains.edit' strain.id class="button-gray smaller"}} Edit {{/link-to}} {{delete-button delete=(action 'deleteStrain')}} diff --git a/app/pods/protected/strains/strain-form/component.js b/app/pods/protected/strains/strain-form/component.js index 07841a7..b5544f3 100644 --- a/app/pods/protected/strains/strain-form/component.js +++ b/app/pods/protected/strains/strain-form/component.js @@ -63,7 +63,8 @@ export default Component.extend(SetupMetaData, { }, speciesDidChange: function(value) { - this.updateField('species', value); + const newSpecies = this.get('speciesList').findBy('id', value); + this.updateField('species', newSpecies); }, isolatedFromDidChange: function(value) { diff --git a/app/pods/protected/strains/strain-form/template.hbs b/app/pods/protected/strains/strain-form/template.hbs index a959f4f..ea27230 100644 --- a/app/pods/protected/strains/strain-form/template.hbs +++ b/app/pods/protected/strains/strain-form/template.hbs @@ -17,7 +17,7 @@