Permalink
Browse files

Better handling of multiple named associations

  • Loading branch information...
1 parent ba841bb commit 550ee646d5770b40b660075190f670941335cfab mde committed Nov 14, 2012
Showing with 37 additions and 14 deletions.
  1. +34 −12 lib/index.js
  2. +3 −2 lib/query/query.js
View
@@ -200,6 +200,7 @@ utils.mixin(model, new (function () {
, opts
, otherKeyName
, selfKeyName
+ , keyName
, queryName
, reg = model.descriptionRegistry
, assn = reg[this.type].associations[assnType]
@@ -217,9 +218,6 @@ utils.mixin(model, new (function () {
modelName = utils.inflection.singularize(modelName);
assnName = utils.inflection.singularize(assnName);
- otherKeyName = utils.string.decapitalize(assnName)
- selfKeyName = utils.string.decapitalize(this.type)
-
// Has query object
if (assnType == 'hasMany') {
query = args.shift() || {};
@@ -234,13 +232,23 @@ utils.mixin(model, new (function () {
// I belong to the other model; look for the item
// whose id matches my foreign key for that model
if (assnType == 'belongsTo') {
- query.id = this[otherKeyName + 'Id'];
+ otherKeyName = modelName;
+ if (modelName != assnName) {
+ otherKeyName = assnName + otherKeyName;
+ }
+ otherKeyName = utils.string.decapitalize(otherKeyName + 'Id');
+ query.id = this[otherKeyName];
}
// The other model belongs to me; look for any
// items whose foreign keys match my id
// (hasOne is just a special case of hasMany)
else {
- query[selfKeyName + 'Id'] = this.id;
+ selfKeyName = this.type;
+ if (modelName != assnName) {
+ selfKeyName = assnName + selfKeyName;
+ }
+ selfKeyName = utils.string.decapitalize(selfKeyName + 'Id');
+ query[selfKeyName] = this.id;
}
queryName = assnType == 'hasMany' ? 'all' : 'first';
@@ -271,15 +279,17 @@ utils.mixin(model, new (function () {
modelName = utils.inflection.singularize(modelName);
assnName = utils.inflection.singularize(assnName);
- otherKeyName = utils.string.decapitalize(assnName)
- selfKeyName = utils.string.decapitalize(this.type)
-
if (assnType == 'belongsTo') {
if (!(data._saved && data.id)) {
throw new Error('Item cannot have a belongTo association ' +
'if the item it belongs to is not yet saved.');
}
- this[otherKeyName + 'Id'] = data.id;
+ otherKeyName = modelName;
+ if (modelName != assnName) {
+ otherKeyName = assnName + otherKeyName;
+ }
+ otherKeyName = utils.string.decapitalize(otherKeyName + 'Id');
+ this[otherKeyName] = data.id;
unsaved = data._unsavedAssociations || [];
unsaved.push(this);
data._unsavedAssociations = unsaved;
@@ -289,7 +299,12 @@ utils.mixin(model, new (function () {
throw new Error('Item cannot have a hasOne/hasMany association ' +
'if it is not yet saved.');
}
- data[selfKeyName + 'Id'] = this.id;
+ selfKeyName = this.type;
+ if (modelName != assnName) {
+ selfKeyName = assnName + selfKeyName;
+ }
+ selfKeyName = utils.string.decapitalize(selfKeyName + 'Id');
+ data[selfKeyName] = this.id;
unsaved = this._unsavedAssociations || [];
unsaved.push(data);
this._unsavedAssociations = unsaved;
@@ -879,12 +894,19 @@ model.ModelDefinitionBase = function (name) {
if (assnKey == 'belongsTo') {
ownerModelName = modelName;
ownedModelName = self.name;
- idKey = utils.string.decapitalize(assnName) + 'Id'
+ idKey = modelName;
}
else {
ownerModelName = self.name;
ownedModelName = modelName;
- idKey = utils.string.decapitalize(ownerModelName) + 'Id'
+ idKey = self.name;
+ }
+
+ if (assnName == modelName) {
+ idKey = utils.string.decapitalize(idKey) + 'Id'
+ }
+ else {
+ idKey = utils.string.decapitalize(assnName) + idKey + 'Id'
}
if (!reg[ownedModelName].properties[idKey]) {
View
@@ -104,8 +104,9 @@ Query.prototype = new (function () {
, _createComparison = function (val, key) {
var type
- , descr = model.descriptionRegistry[
- this.model.modelName].properties[key]
+ , modelName = this.model.modelName
+ , props = model.descriptionRegistry[modelName].properties
+ , descr = props[key]
, datatype
, opts = _createComparisonOpts.apply(this, [key]);

0 comments on commit 550ee64

Please sign in to comment.