Skip to content

Commit 43caa10

Browse files
committed
Smart Segments - Prevent includes from queryBuilder to overwrite include from segment scope
1 parent b864362 commit 43caa10

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### Added
55
- Technical - Add babel.
66

7+
### Changed
8+
- Smart Segments - Prevent includes from queryBuilder to overwrite include from segment scope.
9+
710
## RELEASE 2.16.9 - 2018-11-08
811
### Changed
912
- Smart Fields - Display a warning to show Smart Fields declared without a field attribute.

src/services/query-builder.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ function QueryBuilder(model, opts, params) {
2424
};
2525

2626
this.getIncludes = function (modelForIncludes, fieldNamesRequested) {
27-
var includes = [];
28-
_.values(modelForIncludes.associations).forEach(function (association) {
29-
if (!fieldNamesRequested ||
30-
(fieldNamesRequested.indexOf(association.as) !== -1)) {
31-
if (['HasOne', 'BelongsTo'].indexOf(association.associationType) > -1) {
32-
includes.push({
33-
model: association.target.unscoped(),
34-
as: association.associationAccessor
35-
});
36-
}
37-
}
38-
});
39-
40-
return includes;
27+
return _.values(modelForIncludes.associations)
28+
.filter(function (association) {
29+
return (
30+
(!fieldNamesRequested ||
31+
fieldNamesRequested.includes(association.as)) &&
32+
['HasOne', 'BelongsTo'].includes(association.associationType) &&
33+
// Don't include models that are already included by the segment scope
34+
(modelForIncludes._scope.include || []).every(function (include) {
35+
return include.model !== association.target
36+
})
37+
);
38+
})
39+
.map(function (association) {
40+
return {
41+
model: association.target.unscoped(),
42+
as: association.associationAccessor
43+
};
44+
});
4145
};
4246

4347
this.getOrder = function (aliasName) {

0 commit comments

Comments
 (0)