From 309105c4adcc594897cc69e39306fee3364992d2 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Mon, 28 Jul 2014 11:12:20 +0200 Subject: [PATCH] Updated remaining relations to use polymorphicParams --- lib/relation-definition.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index bf7d3826..eddcd91c 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -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 }); }