Skip to content

Commit

Permalink
Smart Segments - Prevent includes from queryBuilder to overwrite incl…
Browse files Browse the repository at this point in the history
…ude from segment scope
  • Loading branch information
louisremi committed Dec 14, 2018
1 parent b864362 commit 43caa10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### Added
- Technical - Add babel.

### Changed
- Smart Segments - Prevent includes from queryBuilder to overwrite include from segment scope.

## RELEASE 2.16.9 - 2018-11-08
### Changed
- Smart Fields - Display a warning to show Smart Fields declared without a field attribute.
Expand Down
32 changes: 18 additions & 14 deletions src/services/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ function QueryBuilder(model, opts, params) {
};

this.getIncludes = function (modelForIncludes, fieldNamesRequested) {
var includes = [];
_.values(modelForIncludes.associations).forEach(function (association) {
if (!fieldNamesRequested ||
(fieldNamesRequested.indexOf(association.as) !== -1)) {
if (['HasOne', 'BelongsTo'].indexOf(association.associationType) > -1) {
includes.push({
model: association.target.unscoped(),
as: association.associationAccessor
});
}
}
});

return includes;
return _.values(modelForIncludes.associations)
.filter(function (association) {
return (
(!fieldNamesRequested ||
fieldNamesRequested.includes(association.as)) &&
['HasOne', 'BelongsTo'].includes(association.associationType) &&
// Don't include models that are already included by the segment scope
(modelForIncludes._scope.include || []).every(function (include) {
return include.model !== association.target
})
);
})
.map(function (association) {
return {
model: association.target.unscoped(),
as: association.associationAccessor
};
});
};

this.getOrder = function (aliasName) {
Expand Down

0 comments on commit 43caa10

Please sign in to comment.