File tree Expand file tree Collapse file tree 3 files changed +34
-14
lines changed Expand file tree Collapse file tree 3 files changed +34
-14
lines changed Original file line number Diff line number Diff line change @@ -46,9 +46,14 @@ export function getScopeOptions(target: any): IScopeOptions | undefined {
46
46
/**
47
47
* Resolves scope
48
48
*/
49
- function resolveScope ( scopeName : string , model : typeof Model , options : IScopeFindOptions | undefined ) : void {
49
+ function resolveScope ( scopeName : string , model : typeof Model , options : IScopeFindOptions | Function | undefined ) : void {
50
50
resolveModelGetter ( options ) ;
51
- options = inferAlias ( options , model ) ;
51
+ if ( typeof options === 'function' ) {
52
+ const fn : Function = options ;
53
+ options = ( ...args : any [ ] ) => inferAlias ( fn ( ...args ) , model ) ;
54
+ } else {
55
+ options = inferAlias ( options , model ) ;
56
+ }
52
57
model . addScope ( scopeName , options as IFindOptions , { override : true } ) ;
53
58
}
54
59
Original file line number Diff line number Diff line change @@ -22,6 +22,11 @@ export const SHOE_SCOPES = {
22
22
primaryColor : primaryColor => ( {
23
23
where : { primaryColor}
24
24
}
25
+ ) ,
26
+ primaryColorWithManufacturer : primaryColor => ( {
27
+ include : [ Manufacturer ] ,
28
+ where : { primaryColor} ,
29
+ }
25
30
)
26
31
} ;
27
32
Original file line number Diff line number Diff line change @@ -198,18 +198,28 @@ describe('scopes', () => {
198
198
199
199
} ) ;
200
200
201
- // describe('with scope function', () => {
202
- //
203
- // it('should consider nested scope', () =>
204
- // ShoeWithScopes
205
- // .scope({method: ['primaryColor', 'yellow']})
206
- // .findOne()
207
- // .then(shoe => {
208
- // expect(shoe).to.have.property('primaryColor', 'yellow');
209
- // })
210
- // );
211
- //
212
- // });
201
+ describe ( 'with scope function' , ( ) => {
202
+
203
+ it ( 'should find appropriate shoe due to correctly passed scope function param' , ( ) =>
204
+ ShoeWithScopes
205
+ . scope ( { method : [ 'primaryColor' , 'red' ] } )
206
+ . findOne ( )
207
+ . then ( shoe => {
208
+ expect ( shoe ) . to . have . property ( 'primaryColor' , 'red' ) ;
209
+ } )
210
+ ) ;
211
+
212
+ it ( 'should find appropriate shoe due to correctly passed scope function param including associated model' , ( ) =>
213
+ ShoeWithScopes
214
+ . scope ( { method : [ 'primaryColorWithManufacturer' , 'red' ] } )
215
+ . findOne ( )
216
+ . then ( shoe => {
217
+ expect ( shoe ) . to . have . property ( 'primaryColor' , 'red' ) ;
218
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . that . is . an ( 'object' ) ;
219
+ } )
220
+ ) ;
221
+
222
+ } ) ;
213
223
214
224
} ) ;
215
225
You can’t perform that action at this time.
0 commit comments