@@ -24,40 +24,33 @@ class ResourceCreator {
24
24
return targetKey ;
25
25
}
26
26
27
- _makePromisesBeforeSave ( record ) {
28
- return async ( promises , [ name , association ] ) => {
29
- if ( association . associationType === 'BelongsTo' ) {
30
- const setterName = `set${ _ . upperFirst ( name ) } ` ;
31
- const targetKey = await this . _getTargetKey ( name , association ) ;
32
- const promise = record [ setterName ] ( targetKey , { save : false } ) ;
33
- promises . push ( promise ) ;
34
- }
35
- return promises ;
36
- } ;
27
+ async _makePromisesBeforeSave ( record , [ name , association ] ) {
28
+ if ( association . associationType === 'BelongsTo' ) {
29
+ const setterName = `set${ _ . upperFirst ( name ) } ` ;
30
+ const targetKey = await this . _getTargetKey ( name , association ) ;
31
+ return record [ setterName ] ( targetKey , { save : false } ) ;
32
+ }
33
+ return null ;
37
34
}
38
35
39
- _makePromisesAfterSave ( record ) {
40
- return ( promises , [ name , association ] ) => {
41
- let setterName ;
42
- if ( association . associationType === 'HasOne' ) {
43
- setterName = `set${ _ . upperFirst ( name ) } ` ;
44
- } else if ( [ 'BelongsToMany' , 'HasMany' ] . includes ( association . associationType ) ) {
45
- setterName = `add${ _ . upperFirst ( name ) } ` ;
46
- }
47
- if ( setterName ) {
48
- const promise = record [ setterName ] ( this . params [ name ] ) ;
49
- promises . push ( promise ) ;
50
- }
51
- return promises ;
52
- } ;
36
+ _makePromisesAfterSave ( record , [ name , association ] ) {
37
+ let setterName ;
38
+ if ( association . associationType === 'HasOne' ) {
39
+ setterName = `set${ _ . upperFirst ( name ) } ` ;
40
+ } else if ( [ 'BelongsToMany' , 'HasMany' ] . includes ( association . associationType ) ) {
41
+ setterName = `add${ _ . upperFirst ( name ) } ` ;
42
+ }
43
+ if ( setterName ) {
44
+ return record [ setterName ] ( this . params [ name ] ) ;
45
+ }
46
+ return null ;
53
47
}
54
48
55
49
async _handleSave ( record , callback ) {
56
50
const { associations } = this . model ;
57
51
if ( associations ) {
58
- callback = callback . bind ( this ) ;
59
- const promisesBeforeSave = Object . entries ( associations ) . reduce ( callback ( record ) , [ ] ) ;
60
- await P . all ( promisesBeforeSave ) ;
52
+ await P . all ( Object . entries ( associations )
53
+ . map ( ( entry ) => callback . bind ( this ) ( record , entry ) ) ) ;
61
54
}
62
55
}
63
56
0 commit comments