Refactor modelTo logic into lookupModelTo

This commit is contained in:
Fabien Franzen 2014-08-10 17:08:31 +02:00
parent d1466bffcb
commit 1724f350de
1 changed files with 28 additions and 57 deletions

View File

@ -398,6 +398,25 @@ function lookupModel(models, modelName) {
}
}
function lookupModelTo(modelFrom, modelTo, params, singularize) {
if ('string' === typeof modelTo) {
params.as = params.as || modelTo;
if (typeof params.model === 'function') {
modelTo = params.model;
} else if (typeof params.model === 'string') {
modelTo = params.model;
}
if (typeof modelTo === 'string') {
var modelToName = (singularize ? i8n.singularize(modelTo) : modelTo).toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName) || modelTo;
}
if (typeof modelTo === 'string') {
throw new Error('Could not find "' + modelTo + '" relation for ' + modelFrom.modelName);
}
}
return modelTo;
}
/*!
* Normalize polymorphic parameters
* @param {Object|String} params Name of the polymorphic relation or params
@ -436,15 +455,7 @@ function polymorphicParams(params) {
RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
var thisClassName = modelFrom.modelName;
params = params || {};
if (typeof modelTo === 'string') {
params.as = modelTo;
if (params.model) {
modelTo = params.model;
} else {
var modelToName = i8n.singularize(modelTo).toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
}
}
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
var relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
var fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true);
@ -953,13 +964,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
var discriminator, polymorphic;
params = params || {};
if ('string' === typeof modelTo && !params.polymorphic) {
params.as = modelTo;
if (params.model) {
modelTo = params.model;
} else {
var modelToName = modelTo.toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
}
modelTo = lookupModelTo(modelFrom, modelTo, params);
}
var idName, relationName, fk;
@ -1182,19 +1187,9 @@ BelongsTo.prototype.related = function (refresh, params) {
*/
RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom, modelTo, params) {
params = params || {};
var models = modelFrom.dataSource.modelBuilder.models;
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
if ('string' === typeof modelTo) {
params.as = modelTo;
if (params.model) {
modelTo = params.model;
} else {
modelTo = lookupModel(models, i8n.singularize(modelTo)) || modelTo;
}
if (typeof modelTo === 'string') {
throw new Error('Could not find "' + modelTo + '" relation for ' + modelFrom.modelName);
}
}
var models = modelFrom.dataSource.modelBuilder.models;
if (!params.through) {
if (params.polymorphic) throw new Error('Polymorphic relations need a through model');
@ -1243,15 +1238,7 @@ RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom,
*/
RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
params = params || {};
if ('string' === typeof modelTo) {
params.as = modelTo;
if (params.model) {
modelTo = params.model;
} else {
var modelToName = modelTo.toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
}
}
modelTo = lookupModelTo(modelFrom, modelTo, params);
var pk = modelFrom.dataSource.idName(modelTo.modelName) || 'id';
var relationName = params.as || i8n.camelize(modelTo.modelName, true);
@ -1476,18 +1463,10 @@ HasOne.prototype.related = function (refresh, params) {
};
RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params) {
var thisClassName = modelFrom.modelName;
params = params || {};
if (typeof modelTo === 'string') {
params.as = modelTo;
if (params.model) {
modelTo = params.model;
} else {
var modelToName = i8n.singularize(modelTo).toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
}
}
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
var thisClassName = modelFrom.modelName;
var accessorName = params.as || (i8n.camelize(modelTo.modelName, true) + 'List');
var relationName = params.property || i8n.camelize(modelTo.pluralModelName, true);
var fk = modelTo.dataSource.idName(modelTo.modelName) || 'id';
@ -1925,18 +1904,10 @@ EmbedsMany.prototype.remove = function (acInst, cb) {
};
RelationDefinition.referencesMany = function referencesMany(modelFrom, modelTo, params) {
var thisClassName = modelFrom.modelName;
params = params || {};
if (typeof modelTo === 'string') {
params.as = modelTo;
if (params.model) {
modelTo = params.model;
} else {
var modelToName = i8n.singularize(modelTo).toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
}
}
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
var thisClassName = modelFrom.modelName;
var relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
var fk = params.foreignKey || i8n.camelize(modelTo.modelName + '_ids', true);
var idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';