Merge pull request #209 from fabien/fix/refactor-modelto
Refactor modelTo
This commit is contained in:
commit
6ea6f19752
|
@ -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
|
* Normalize polymorphic parameters
|
||||||
* @param {Object|String} params Name of the polymorphic relation or params
|
* @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) {
|
RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
|
||||||
var thisClassName = modelFrom.modelName;
|
var thisClassName = modelFrom.modelName;
|
||||||
params = params || {};
|
params = params || {};
|
||||||
if (typeof modelTo === 'string') {
|
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
|
||||||
params.as = modelTo;
|
|
||||||
if (params.model) {
|
|
||||||
modelTo = params.model;
|
|
||||||
} else {
|
|
||||||
var modelToName = i8n.singularize(modelTo).toLowerCase();
|
|
||||||
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
var relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
||||||
var fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true);
|
var fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true);
|
||||||
|
@ -953,13 +960,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
|
||||||
var discriminator, polymorphic;
|
var discriminator, polymorphic;
|
||||||
params = params || {};
|
params = params || {};
|
||||||
if ('string' === typeof modelTo && !params.polymorphic) {
|
if ('string' === typeof modelTo && !params.polymorphic) {
|
||||||
params.as = modelTo;
|
modelTo = lookupModelTo(modelFrom, modelTo, params);
|
||||||
if (params.model) {
|
|
||||||
modelTo = params.model;
|
|
||||||
} else {
|
|
||||||
var modelToName = modelTo.toLowerCase();
|
|
||||||
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var idName, relationName, fk;
|
var idName, relationName, fk;
|
||||||
|
@ -1182,19 +1183,9 @@ BelongsTo.prototype.related = function (refresh, params) {
|
||||||
*/
|
*/
|
||||||
RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom, modelTo, params) {
|
RelationDefinition.hasAndBelongsToMany = function hasAndBelongsToMany(modelFrom, modelTo, params) {
|
||||||
params = params || {};
|
params = params || {};
|
||||||
var models = modelFrom.dataSource.modelBuilder.models;
|
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
|
||||||
|
|
||||||
if ('string' === typeof modelTo) {
|
var models = modelFrom.dataSource.modelBuilder.models;
|
||||||
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.through) {
|
||||||
if (params.polymorphic) throw new Error('Polymorphic relations need a through model');
|
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) {
|
RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
|
||||||
params = params || {};
|
params = params || {};
|
||||||
if ('string' === typeof modelTo) {
|
modelTo = lookupModelTo(modelFrom, modelTo, params);
|
||||||
params.as = modelTo;
|
|
||||||
if (params.model) {
|
|
||||||
modelTo = params.model;
|
|
||||||
} else {
|
|
||||||
var modelToName = modelTo.toLowerCase();
|
|
||||||
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var pk = modelFrom.dataSource.idName(modelTo.modelName) || 'id';
|
var pk = modelFrom.dataSource.idName(modelTo.modelName) || 'id';
|
||||||
var relationName = params.as || i8n.camelize(modelTo.modelName, true);
|
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) {
|
RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params) {
|
||||||
var thisClassName = modelFrom.modelName;
|
|
||||||
params = params || {};
|
params = params || {};
|
||||||
if (typeof modelTo === 'string') {
|
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
|
||||||
params.as = modelTo;
|
|
||||||
if (params.model) {
|
|
||||||
modelTo = params.model;
|
|
||||||
} else {
|
|
||||||
var modelToName = i8n.singularize(modelTo).toLowerCase();
|
|
||||||
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var thisClassName = modelFrom.modelName;
|
||||||
var accessorName = params.as || (i8n.camelize(modelTo.modelName, true) + 'List');
|
var accessorName = params.as || (i8n.camelize(modelTo.modelName, true) + 'List');
|
||||||
var relationName = params.property || i8n.camelize(modelTo.pluralModelName, true);
|
var relationName = params.property || i8n.camelize(modelTo.pluralModelName, true);
|
||||||
var fk = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
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) {
|
RelationDefinition.referencesMany = function referencesMany(modelFrom, modelTo, params) {
|
||||||
var thisClassName = modelFrom.modelName;
|
|
||||||
params = params || {};
|
params = params || {};
|
||||||
if (typeof modelTo === 'string') {
|
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
|
||||||
params.as = modelTo;
|
|
||||||
if (params.model) {
|
|
||||||
modelTo = params.model;
|
|
||||||
} else {
|
|
||||||
var modelToName = i8n.singularize(modelTo).toLowerCase();
|
|
||||||
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var thisClassName = modelFrom.modelName;
|
||||||
var relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
var relationName = params.as || i8n.camelize(modelTo.pluralModelName, true);
|
||||||
var fk = params.foreignKey || i8n.camelize(modelTo.modelName + '_ids', true);
|
var fk = params.foreignKey || i8n.camelize(modelTo.modelName + '_ids', true);
|
||||||
var idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
var idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
||||||
|
|
|
@ -42,12 +42,15 @@ mixins.define('TimeStamp', timestamps);
|
||||||
|
|
||||||
describe('Model class', function () {
|
describe('Model class', function () {
|
||||||
|
|
||||||
it('should define a mixin', function() {
|
it('should define mixins', function() {
|
||||||
mixins.define('Example', function(Model, options) {
|
mixins.define('Example', function(Model, options) {
|
||||||
Model.prototype.example = function() {
|
Model.prototype.example = function() {
|
||||||
return options;
|
return options;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
mixins.define('Demo', function(Model, options) {
|
||||||
|
Model.demoMixin = options.ok;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should apply a mixin class', function() {
|
it('should apply a mixin class', function() {
|
||||||
|
@ -58,7 +61,7 @@ describe('Model class', function () {
|
||||||
|
|
||||||
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
||||||
var Item = memory.createModel('Item', { name: 'string' }, {
|
var Item = memory.createModel('Item', { name: 'string' }, {
|
||||||
mixins: { TimeStamp: true, demo: true, Address: true }
|
mixins: { Address: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
var properties = Item.definition.properties;
|
var properties = Item.definition.properties;
|
||||||
|
@ -70,11 +73,12 @@ describe('Model class', function () {
|
||||||
it('should apply mixins', function(done) {
|
it('should apply mixins', function(done) {
|
||||||
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
var memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
||||||
var Item = memory.createModel('Item', { name: 'string' }, {
|
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('Example', { foo: 'bar' });
|
||||||
Item.mixin('other');
|
|
||||||
|
Item.demoMixin.should.be.true;
|
||||||
|
|
||||||
var properties = Item.definition.properties;
|
var properties = Item.definition.properties;
|
||||||
properties.createdAt.should.eql({ type: Date });
|
properties.createdAt.should.eql({ type: Date });
|
||||||
|
|
Loading…
Reference in New Issue