Skip to content

Commit

Permalink
Merge pull request #595 from strongloop/feature/fix-issue-594
Browse files Browse the repository at this point in the history
Make sure relation scope is applied during include
  • Loading branch information
raymondfeng committed May 16, 2015
2 parents d19001a + eac74ad commit 2de19f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/include.js
Expand Up @@ -350,6 +350,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[modelToIdName] = {
inq: targetIds
};

//make sure that the modelToIdName is included if fields are specified
if (Array.isArray(fields) && fields.indexOf(modelToIdName) === -1) {
fields.push(modelToIdName);
Expand Down Expand Up @@ -430,7 +431,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: allTargetIds
};

relation.applyScope(null, filter);
/**
* Make the DB Call, fetch all target objects
*/
Expand Down Expand Up @@ -494,6 +495,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: sourceIds
};
relation.applyScope(null, filter);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Process fetched related objects
Expand Down Expand Up @@ -571,14 +573,14 @@ Inclusion.include = function (objects, include, cb) {
typeFilter.where[relation.keyTo] = {
inq: targetIds
};
var app = relation.modelFrom.app;
var Model = lookupModel(relation.modelFrom.dataSource.modelBuilder.
models, modelType);
if (!Model) {
callback(new Error('Discriminator type "' + modelType +
' specified but no model exists with such name'));
return;
}
relation.applyScope(null, typeFilter);
Model.find(typeFilter, targetFetchHandler);
/**
* Process fetched related objects
Expand Down Expand Up @@ -647,6 +649,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: targetIds
};
relation.applyScope(null, filter);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Process fetched related objects
Expand Down
24 changes: 23 additions & 1 deletion test/relations.test.js
Expand Up @@ -2890,7 +2890,17 @@ describe('relations', function () {
});
});

it('should find record that match scope', function (done) {
it('should include record that matches scope', function(done) {
Supplier.findById(supplierId, {include: 'account'}, function(err, supplier) {
should.exists(supplier.toJSON().account);
supplier.account(function(err, account) {
should.exists(account);
done();
});
});
});

it('should not find record that does not match scope', function (done) {
Account.updateAll({ block: true }, function (err) {
Supplier.findById(supplierId, function (err, supplier) {
supplier.account(function (err, account) {
Expand All @@ -2901,6 +2911,18 @@ describe('relations', function () {
});
});

it('should not include record that does not match scope', function (done) {
Account.updateAll({ block: true }, function (err) {
Supplier.findById(supplierId, {include: 'account'}, function (err, supplier) {
should.not.exists(supplier.toJSON().account);
supplier.account(function (err, account) {
should.not.exists(account);
done();
});
});
});
});

it('can be used to query data with promises', function (done) {
db.automigrate(function () {
Supplier.create({name: 'Supplier 1'})
Expand Down

0 comments on commit 2de19f2

Please sign in to comment.