Skip to content

Commit 33164fd

Browse files
Merge pull request #53 from RobinBuschmann/bugfix-43
Bugfix 43; resolves #43
2 parents f12e603 + 3102c69 commit 33164fd

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

lib/services/association.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,16 @@ export function setAssociations(target: any, associations: ISequelizeAssociation
141141
}
142142

143143
export function getAssociationsByRelation(target: any, relatedClass: any): ISequelizeAssociation[] {
144-
145144
const associations = getAssociations(target);
146145

147-
return (associations || []).filter(association => association.relatedClassGetter() === relatedClass);
146+
return (associations || []).filter(association => {
147+
const _relatedClass = association.relatedClassGetter();
148+
return (
149+
_relatedClass.prototype === relatedClass.prototype || // v3 + v4
150+
/* istanbul ignore next */
151+
relatedClass.prototype instanceof _relatedClass // v4 (for child classes)
152+
);
153+
});
148154
}
149155

150156
/**

test/models/Manufacturer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import {Table, Model, Column, HasMany} from "../../index";
1+
import {Table, Model, Column, HasMany, Scopes} from "../../index";
22
import {ShoeWithScopes} from "./ShoeWithScopes";
33

4-
4+
@Scopes({
5+
brandOnly: {
6+
attributes: {
7+
exclude: ['notInScopeBrandOnly']
8+
}
9+
}
10+
})
511
@Table
612
export class Manufacturer extends Model<Manufacturer> {
713

814
@Column
915
brand: string;
1016

17+
@Column
18+
notInScopeBrandOnly: string;
19+
1120
@HasMany(() => ShoeWithScopes)
1221
shoes: ShoeWithScopes[];
1322
}

test/models/ShoeWithScopes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export const SHOE_SCOPES = {
1212
},
1313
yellow: {
1414
where: {primaryColor: 'yellow'}
15+
},
16+
red: {
17+
where: {primaryColor: 'red'}
18+
},
19+
manufacturerWithScope: {
20+
include: [() => Manufacturer.scope('brandOnly')]
1521
}
1622
};
1723

test/specs/scopes.spec.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ describe('scopes', () => {
4141
secondaryColor: 'blue',
4242
producedAt: new Date(),
4343
manufacturer: {
44-
brand: BRAND
44+
brand: BRAND,
45+
notInScopeBrandOnly: 'invisible :)',
4546
},
4647
owner: {
4748
name: OWNER
@@ -78,7 +79,7 @@ describe('scopes', () => {
7879
.unscoped()
7980
.findOne()
8081
.then(shoe => {
81-
expect(shoe).to.have.property('secretKey').which.is.not.null;
82+
expect(shoe).to.have.property('secretKey').which.is.a('string');
8283
})
8384
);
8485

@@ -140,6 +141,61 @@ describe('scopes', () => {
140141
})
141142
);
142143

144+
describe('with using scoped included model', () => {
145+
146+
it('should consider scope of included model (without own scope)', () =>
147+
ShoeWithScopes
148+
.findOne({
149+
include: [Manufacturer.scope('brandOnly')]
150+
})
151+
.then(shoe => {
152+
expect(shoe).to.have.property('manufacturer')
153+
.that.have.property('notInScopeBrandOnly')
154+
.which.is.undefined;
155+
})
156+
);
157+
158+
it('should consider scope of included model (with own scope)', () =>
159+
ShoeWithScopes
160+
.scope('red')
161+
.findOne({
162+
include: [Manufacturer.scope('brandOnly')]
163+
})
164+
.then(shoe => {
165+
expect(shoe).to.have.property('manufacturer')
166+
.that.have.property('notInScopeBrandOnly')
167+
.which.is.undefined;
168+
})
169+
);
170+
171+
});
172+
173+
});
174+
175+
describe('with nested scope', () => {
176+
177+
it('should consider nested scope', () =>
178+
ShoeWithScopes
179+
.scope('manufacturerWithScope')
180+
.findOne()
181+
.then(shoe => {
182+
expect(shoe).to.have.property('manufacturer')
183+
.that.have.property('notInScopeBrandOnly')
184+
.which.is.undefined;
185+
})
186+
);
187+
188+
it('should not consider nested scope', () =>
189+
ShoeWithScopes
190+
.scope('full')
191+
.findOne()
192+
.then(shoe => {
193+
expect(shoe).to.have.property('manufacturer')
194+
.that.have.property('notInScopeBrandOnly')
195+
.which.is.a('string');
196+
})
197+
);
198+
143199
});
144200

145201
});

0 commit comments

Comments
 (0)