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:
parent
e018efbf43
commit
abf57ff497
|
@ -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;
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue