Skip to content

[*] Smart Segments - Prevent includes from queryBuilder to overwrite include from segment scope #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions src/services/resources-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function ResourcesGetter(model, opts, params) {

function getRecords() {
var scope = segmentScope ? model.scope(segmentScope) : model.unscoped();
var include = queryBuilder.getIncludes(model, fieldNamesRequested);
var include = queryBuilder.getIncludes(scope, fieldNamesRequested);

return getWhere()
.then(function (where) {
Expand Down Expand Up @@ -157,7 +157,7 @@ function ResourcesGetter(model, opts, params) {

function countRecords() {
var scope = segmentScope ? model.scope(segmentScope) : model.unscoped();
var include = queryBuilder.getIncludes(model, fieldNamesRequested);
var include = queryBuilder.getIncludes(scope, fieldNamesRequested);

return getWhere()
.then(function (where) {
Expand Down