Updated remaining relations to use polymorphicParams

This commit is contained in:
Fabien Franzen 2014-07-28 11:12:20 +02:00
parent 7ddfbb6409
commit 309105c4ad
1 changed files with 13 additions and 13 deletions

View File

@ -871,7 +871,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
if (params.polymorphic) {
if (params.polymorphic === true) {
// modelTo will be the name of the polymorphic relation (string)
// modelTo arg will be the name of the polymorphic relation (string)
var polymorphic = polymorphicParams(modelTo);
} else {
var polymorphic = polymorphicParams(params.polymorphic);
@ -1091,11 +1091,9 @@ RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom,
throw new Error('Could not find "' + modelTo + '" relation for ' + modelFrom.modelName);
}
}
var isPolymorphic = (typeof params.polymorphic === 'string');
if (!params.through) {
if (isPolymorphic) throw new Error('Polymorphic relations need a through model');
if (params.polymorphic) throw new Error('Polymorphic relations need a through model');
var name1 = modelFrom.modelName + modelTo.modelName;
var name2 = modelTo.modelName + modelFrom.modelName;
params.through = lookupModel(models, name1) || lookupModel(models, name2) ||
@ -1106,11 +1104,13 @@ RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom,
options.properties = params.properties;
options.scope = params.scope;
if (isPolymorphic) {
options.polymorphic = params.polymorphic;
var accessor = params.through.prototype[params.polymorphic];
if (params.polymorphic) {
var polymorphic = polymorphicParams(params.polymorphic);
options.polymorphic = polymorphic; // pass through
var accessor = params.through.prototype[polymorphic.as];
if (typeof accessor !== 'function') { // declare once
params.through.belongsTo(params.polymorphic, { polymorphic: true });
// use the name of the polymorphic rel, not modelTo
params.through.belongsTo(polymorphic.as, { polymorphic: true });
}
} else {
params.through.belongsTo(modelFrom);
@ -1155,10 +1155,10 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
var fk = params.foreignKey || i8n.camelize(modelFrom.modelName + '_id', true);
var discriminator;
if (typeof params.polymorphic === 'string') {
var polymorphic = params.polymorphic;
fk = i8n.camelize(polymorphic + '_id', true);
discriminator = i8n.camelize(polymorphic + '_type', true);
if (params.polymorphic) {
var polymorphic = polymorphicParams(params.polymorphic);
fk = polymorphic.foreignKey;
discriminator = polymorphic.discriminator;
if (!params.through) {
modelTo.dataSource.defineProperty(modelTo.modelName, discriminator, { type: 'string', index: true });
}