ENH: Refactor hasMany relations (#59)

Fixes #52
This commit is contained in:
Matthew Ryan Dillon 2017-12-04 05:32:39 -07:00 committed by GitHub
parent af9ac976dc
commit 4dbfcfa98b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 53 deletions

View file

@ -11,6 +11,7 @@ export default Component.extend({
this._super(...arguments);
const model = this.get('model');
const validations = this.get('validations');
const hasMany = this.get('hasMany');
let changesets = {};
changesets['new'] = [];
@ -20,72 +21,43 @@ export default Component.extend({
lookupValidator(validations['collection']),
validations['collection']);
let collectionSpeciesChangesets = [];
const collectionSpecies = model.get('collectionSpecies');
collectionSpecies.forEach((cs) => {
const changeset = new Changeset(cs,
lookupValidator(validations['collectionSpecies']),
validations['collectionSpecies']);
collectionSpeciesChangesets.push({ model: cs, changeset: changeset });
hasMany.forEach((hasMany) => {
let relatedChangesets = [];
let validation = validations[hasMany];
const related = model.get(hasMany);
related.forEach((r) => {
const changeset = new Changeset(r, lookupValidator(validation),
validation);
relatedChangesets.push({ model: r, changeset: changeset });
});
changesets['hasMany']['collectionSpecies'] = collectionSpeciesChangesets;
let datasheetsChangesets = [];
const datasheets = model.get('datasheets');
datasheets.forEach((d) => {
const changeset = new Changeset(d,
lookupValidator(validations['datasheet']),
validations['datasheet']);
datasheetsChangesets.push({ model: d, changeset: changeset });
changesets['hasMany'][hasMany] = relatedChangesets;
});
changesets['hasMany']['datasheets'] = datasheetsChangesets;
this.set('changesets', changesets);
},
actions: {
addCollectionSpecies() {
addHasMany(modelName, relatedName) {
const store = this.get('store');
let changesets = this.get('changesets');
const validations = this.get('validations');
const collection = this.get('model');
const cs = store.createRecord('collection-species', { collection: collection });
collection.get('collectionSpecies').pushObject(cs);
changesets['new'].pushObject(cs);
const changeset = new Changeset(cs,
lookupValidator(validations['collectionSpecies']),
validations['collectionSpecies']);
changesets['hasMany']['collectionSpecies'].pushObject({ model: cs, changeset: changeset });
const validation = validations[relatedName];
const model = this.get('model');
const related = store.createRecord(modelName, { collection: model });
model.get(relatedName).pushObject(related);
changesets['new'].pushObject(related);
const changeset = new Changeset(related, lookupValidator(validation), validation);
changesets['hasMany'][relatedName].pushObject({ model: related, changeset: changeset });
},
deleteCollectionSpecies(changesetRecord) {
deleteHasMany(changesetRecord, relatedName) {
let changesets = this.get('changesets');
changesets['delete'].pushObject(changesetRecord.model);
changesets['hasMany']['collectionSpecies'].removeObject(changesetRecord);
changesets['hasMany'][relatedName].removeObject(changesetRecord);
},
updateDatasheet(changeset, event) {
changeset.set('datasheet', event.target.files[0]);
},
addDatasheet() {
const store = this.get('store');
let changesets = this.get('changesets');
const validations = this.get('validations');
const collection = this.get('model');
const d = store.createRecord('datasheet-attachment', { collection: collection });
collection.get('datasheets').pushObject(d);
changesets['new'].pushObject(d);
const changeset = new Changeset(d,
lookupValidator(validations['datasheets']),
validations['datasheets']);
changesets['hasMany']['datasheets'].pushObject({ model: d, changeset: changeset });
},
deleteDatasheet(changesetRecord) {
let changesets = this.get('changesets');
changesets['delete'].pushObject(changesetRecord.model);
changesets['hasMany']['datasheets'].removeObject(changesetRecord);
},
},
});

View file

@ -11,6 +11,8 @@ export default Controller.extend(ValidationMixin, {
CollectionSpeciesValidations,
DatasheetValidations,
hasMany: ['collectionSpecies', 'datasheets'],
options: computed('projectOptions', 'studyLocationOptions',
'collectionTypeOptions', 'collectionMethodOptions',
'speciesOptions', 'adfgPermitOptions', function() {

View file

@ -11,6 +11,8 @@ export default Controller.extend(ValidationMixin, {
CollectionSpeciesValidations,
DatasheetValidations,
hasMany: ['collectionSpecies', 'datasheets'],
options: computed('projectOptions', 'studyLocationOptions',
'collectionTypeOptions', 'collectionMethodOptions',
'speciesOptions', 'adfgPermitOptions', function() {

View file

@ -6,6 +6,7 @@
collectionSpecies=CollectionSpeciesValidations
datasheet=DatasheetValidations)
options=options
hasMany=hasMany
onSave=(action 'onSave')
onCancel=(action 'onCancel')
}}

View file

@ -6,6 +6,7 @@
collectionSpecies=CollectionSpeciesValidations
datasheet=DatasheetValidations)
options=options
hasMany=hasMany
onSave=(action 'onSave')
onCancel=(action 'onCancel')
}}

View file

@ -95,7 +95,7 @@
<table class="table">
<caption>
Species / Count Info
{{action-button isSuccess=true isXSmall=true label='+' onClick=(action 'addCollectionSpecies')}}
{{action-button isSuccess=true isXSmall=true label='+' onClick=(action 'addHasMany' 'collection-species' 'collectionSpecies')}}
</caption>
<thead>
<tr>
@ -138,7 +138,7 @@
{{/validated-field}}
</td>
<td class="col-md-2">
{{action-button isDanger=true isXSmall=true label='X' onClick=(action 'deleteCollectionSpecies' cs)}}
{{action-button isDanger=true isXSmall=true label='X' onClick=(action 'deleteHasMany' cs 'collectionSpecies')}}
</td>
</tr>
{{/each}}
@ -152,7 +152,7 @@
<table class="table">
<caption>
Attachments
{{action-button isSuccess=true isXSmall=true label='+' onClick=(action 'addDatasheet')}}
{{action-button isSuccess=true isXSmall=true label='+' onClick=(action 'addHasMany' 'datasheet-attachment' 'datasheets')}}
</caption>
<thead>
<tr>
@ -173,7 +173,7 @@
{{/if}}
</td>
<td>
{{action-button isDanger=true isXSmall=true label='X' onClick=(action 'deleteDatasheet' d)}}
{{action-button isDanger=true isXSmall=true label='X' onClick=(action 'deleteHasMany' d 'datasheets')}}
</td>
</tr>
{{/each}}