不要怂,就是干,撸起袖子干!

Commit c1bb8aef by Jan Aagaard Meier

Fix scopes to support aliassed models. Closes #3690

1 parent c4b5f2af
Showing with 28 additions and 2 deletions
...@@ -1805,7 +1805,14 @@ module.exports = (function() { ...@@ -1805,7 +1805,14 @@ module.exports = (function() {
// Reverse so we consider the latest include first. // Reverse so we consider the latest include first.
// This is used if several scopes specify the same include - the last scope should take precendence // This is used if several scopes specify the same include - the last scope should take precendence
scope.include.reverse().forEach(function (scopeInclude) { scope.include.reverse().forEach(function (scopeInclude) {
if (!_.any(options.include, _.matchesDots('model.name', scopeInclude.model.name))) { if (!_.any(options.include, function matchesModelAndAlias(item) {
var sameModel = item.model.name === scopeInclude.model.name;
if (sameModel && item.as) {
return item.as === scopeInclude.as;
}
return sameModel;
})) {
options.include.push(scopeInclude); options.include.push(scopeInclude);
} }
}); });
...@@ -1948,7 +1955,7 @@ module.exports = (function() { ...@@ -1948,7 +1955,7 @@ module.exports = (function() {
model = include.target; model = include.target;
} }
include = { model: model, association: include }; include = { model: model, association: include, as: include.as };
} else if (include instanceof Model) { } else if (include instanceof Model) {
model = include; model = include;
...@@ -1964,6 +1971,9 @@ module.exports = (function() { ...@@ -1964,6 +1971,9 @@ module.exports = (function() {
if (!include.model) { if (!include.model) {
include.model = model; include.model = model;
} }
if (!include.as) {
include.as = include.association.as;
}
} else { } else {
model = include.model; model = include.model;
} }
......
...@@ -196,6 +196,22 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -196,6 +196,22 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }}); expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
}); });
it('should be able to merge aliassed includes with the same model', function () {
var scope = {
include: [{model: User, as: 'someUser'}]
};
var options = {
include: [{model: User, as: 'otherUser'}]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({model: User, as: 'otherUser'});
expect(options.include[1]).to.deep.equal({model: User, as: 'someUser'});
});
it('should be able to merge scoped include with include in find', function () { it('should be able to merge scoped include with include in find', function () {
var scope = { var scope = {
include: [ include: [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!