Merge pull request #209 from fabien/fix/refactor-modelto

Refactor modelTo
This commit is contained in:
Miroslav Bajtoš 2014-08-14 20:00:24 +02:00
commit 6ea6f19752
2 changed files with 32 additions and 61 deletions

View File

@ -398,6 +398,21 @@ function lookupModel(models, modelName) {
}
}
function lookupModelTo(modelFrom, modelTo, params, singularize) {
if ('string' === typeof modelTo) {
params.as = params.as || modelTo;
modelTo = params.model || modelTo;
if (typeof modelTo === 'string') {
var modelToName = (singularize ? i8n.singularize(modelTo) : modelTo).toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName) || modelTo;
}
if (typeof modelTo !== 'function') {
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 +451,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 +960,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 +1183,9 @@ BelongsTo.prototype.related = function (refresh, params) {
*/
RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom, modelTo, params) {
params = params || {};
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
var models = modelFrom.dataSource.modelBuilder.models;
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);
}
}
if (!params.through) {
if (params.polymorphic) throw new Error('Polymorphic relations need a through model');
@ -1243,15 +1234,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 +1459,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';
@ -1923,18 +1898,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';

View File

@ -42,12 +42,15 @@ mixins.define('TimeStamp', timestamps);
describe('Model class', function () {
it('should define a mixin', function() {
it('should define mixins', function() {
mixins.define('Example', function(Model, options) {
Model.prototype.example = function() {
return options;
};
});
mixins.define('Demo', function(Model, options) {
Model.demoMixin = options.ok;
});
});
it('should apply a mixin class', function() {
@ -58,7 +61,7 @@ describe('Model class', function () {
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
var Item = memory.createModel('Item', { name: 'string' }, {
mixins: { TimeStamp: true, demo: true, Address: true }
mixins: { Address: true }
});
var properties = Item.definition.properties;
@ -70,11 +73,12 @@ describe('Model class', function () {
it('should apply mixins', function(done) {
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
var Item = memory.createModel('Item', { name: 'string' }, {
mixins: { TimeStamp: true, demo: { ok: true } }
mixins: { TimeStamp: true, Demo: { ok: true } }
});
Item.mixin('Example', { foo: 'bar' });
Item.mixin('other');
Item.demoMixin.should.be.true;
var properties = Item.definition.properties;
properties.createdAt.should.eql({ type: Date });