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); this._super(...arguments);
const model = this.get('model'); const model = this.get('model');
const validations = this.get('validations'); const validations = this.get('validations');
const hasMany = this.get('hasMany');
let changesets = {}; let changesets = {};
changesets['new'] = []; changesets['new'] = [];
@ -20,72 +21,43 @@ export default Component.extend({
lookupValidator(validations['collection']), lookupValidator(validations['collection']),
validations['collection']); validations['collection']);
let collectionSpeciesChangesets = []; hasMany.forEach((hasMany) => {
const collectionSpecies = model.get('collectionSpecies'); let relatedChangesets = [];
collectionSpecies.forEach((cs) => { let validation = validations[hasMany];
const changeset = new Changeset(cs, const related = model.get(hasMany);
lookupValidator(validations['collectionSpecies']), related.forEach((r) => {
validations['collectionSpecies']); const changeset = new Changeset(r, lookupValidator(validation),
collectionSpeciesChangesets.push({ model: cs, changeset: changeset }); validation);
relatedChangesets.push({ model: r, changeset: changeset });
}); });
changesets['hasMany']['collectionSpecies'] = collectionSpeciesChangesets; changesets['hasMany'][hasMany] = relatedChangesets;
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']['datasheets'] = datasheetsChangesets;
this.set('changesets', changesets); this.set('changesets', changesets);
}, },
actions: { actions: {
addCollectionSpecies() { addHasMany(modelName, relatedName) {
const store = this.get('store'); const store = this.get('store');
let changesets = this.get('changesets'); let changesets = this.get('changesets');
const validations = this.get('validations'); const validations = this.get('validations');
const collection = this.get('model'); const validation = validations[relatedName];
const cs = store.createRecord('collection-species', { collection: collection }); const model = this.get('model');
collection.get('collectionSpecies').pushObject(cs); const related = store.createRecord(modelName, { collection: model });
changesets['new'].pushObject(cs); model.get(relatedName).pushObject(related);
const changeset = new Changeset(cs, changesets['new'].pushObject(related);
lookupValidator(validations['collectionSpecies']), const changeset = new Changeset(related, lookupValidator(validation), validation);
validations['collectionSpecies']); changesets['hasMany'][relatedName].pushObject({ model: related, changeset: changeset });
changesets['hasMany']['collectionSpecies'].pushObject({ model: cs, changeset: changeset });
}, },
deleteCollectionSpecies(changesetRecord) { deleteHasMany(changesetRecord, relatedName) {
let changesets = this.get('changesets'); let changesets = this.get('changesets');
changesets['delete'].pushObject(changesetRecord.model); changesets['delete'].pushObject(changesetRecord.model);
changesets['hasMany']['collectionSpecies'].removeObject(changesetRecord); changesets['hasMany'][relatedName].removeObject(changesetRecord);
}, },
updateDatasheet(changeset, event) { updateDatasheet(changeset, event) {
changeset.set('datasheet', event.target.files[0]); 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, CollectionSpeciesValidations,
DatasheetValidations, DatasheetValidations,
hasMany: ['collectionSpecies', 'datasheets'],
options: computed('projectOptions', 'studyLocationOptions', options: computed('projectOptions', 'studyLocationOptions',
'collectionTypeOptions', 'collectionMethodOptions', 'collectionTypeOptions', 'collectionMethodOptions',
'speciesOptions', 'adfgPermitOptions', function() { 'speciesOptions', 'adfgPermitOptions', function() {

View file

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

View file

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

View file

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

View file

@ -95,7 +95,7 @@
<table class="table"> <table class="table">
<caption> <caption>
Species / Count Info 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> </caption>
<thead> <thead>
<tr> <tr>
@ -138,7 +138,7 @@
{{/validated-field}} {{/validated-field}}
</td> </td>
<td class="col-md-2"> <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> </td>
</tr> </tr>
{{/each}} {{/each}}
@ -152,7 +152,7 @@
<table class="table"> <table class="table">
<caption> <caption>
Attachments 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> </caption>
<thead> <thead>
<tr> <tr>
@ -173,7 +173,7 @@
{{/if}} {{/if}}
</td> </td>
<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> </td>
</tr> </tr>
{{/each}} {{/each}}