scope: add _targetClass to scope property

Store the class of the results returned by a scope find method.
This class is different from the "targetClass" when the relations
is using a third "through" model (e.g. hasAndBelongsToMany).
This commit is contained in:
Miroslav Bajtoš 2014-04-02 19:32:37 +02:00
parent e018efbf43
commit abf57ff497
2 changed files with 12 additions and 0 deletions

View File

@ -1,3 +1,4 @@
var i8n = require('inflection');
var utils = require('./utils');
var defineCachedRelations = utils.defineCachedRelations;
/**
@ -83,6 +84,10 @@ function defineScope(cls, targetClass, name, params, methods) {
}
};
f._scope = typeof params === 'function' ? params.call(this) : params;
f._targetClass = targetClass.modelName;
if (f._scope.collect) {
f._targetClass = i8n.capitalize(f._scope.collect);
}
f.build = build;
f.create = create;

View File

@ -117,6 +117,10 @@ describe('relations', function () {
});
}
});
it('should set targetClass on scope property', function() {
should.equal(Book.prototype.chapters._targetClass, 'Chapter');
});
});
describe('belongsTo', function () {
@ -248,6 +252,9 @@ describe('relations', function () {
});
});
it('should set targetClass on scope property', function() {
should.equal(Article.prototype.tags._targetClass, 'Tag');
});
});
});