Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fucking named associations

  • Loading branch information...
commit 7ba54cafe426f426ff2f077fac80aeab634f2856 1 parent 40e72a8
mde authored
View
88 lib/adapters/sql/base.js
@@ -13,38 +13,92 @@ Adapter = function () {
Adapter.prototype = new BaseAdapter();
utils.mixin(Adapter.prototype, new (function () {
+ var _getModelNameForAssociation = function (mainName, assnName) {
+ var assn
+ , assnItem;
+ if (model.descriptionRegistry[assnName]) {
+ return assnName;
+ }
+ assn = model.descriptionRegistry[mainName].associations;
+ for (var p in assn) {
+ assnItem = assn[p][assnName];
+ if (assnItem) {
+ return assnItem.model;
+ }
+ }
+ };
+
this._tableizeModelName = function (name) {
var tableName = utils.inflection.pluralize(name);
tableName = utils.string.snakeize(tableName);
return tableName;
};
- this._createSelectStatement = function (modelName) {
+ this._createSelectStatement = function (modelName, ownerModelName) {
+
var infl = utils.string.getInflections(modelName)
- , name = infl.constructor.singular
- , tableName = this._tableizeModelName(name)
- , props = model.descriptionRegistry[name].properties
- , propArr = [];
+ , name
+ , assumedName = infl.constructor.singular
+ , ownerName
+ , tableName
+ , assumedTableName
+ , props
+ , propArr;
- propArr.push(tableName + '."id" AS "' + tableName + '#id"');
+ // Assumed name is a real model
+ if (model.descriptionRegistry[assumedName]) {
+ name = assumedName;
+ }
+ // Otherwise it's a named association, need to look up the
+ // actual model via it's owner's associations list
+ else {
+ infl = utils.string.getInflections(ownerModelName);
+ ownerName = infl.constructor.singular;
+ name = _getModelNameForAssociation(ownerName, assumedName);
+ }
+
+ tableName = this._tableizeModelName(name);
+ assumedTableName = this._tableizeModelName(assumedName);
+ props = model.descriptionRegistry[name].properties;
+ propArr = [];
+ propArr.push(assumedTableName + '."id" AS "' + assumedTableName + '#id"');
for (var p in props) {
propArr.push(tableName + '.' + this._columnizePropertyName(p) + ' AS "' +
- tableName + '#' + this._columnizePropertyName(p, {useQuotes: false}) + '"');
+ assumedTableName + '#' + this._columnizePropertyName(p, {useQuotes: false}) + '"');
}
return propArr.join(', ');
};
- this._createLeftOuterJoinStatement = function (fromModelName, toModelName) {
- var fromInfl = utils.string.getInflections(fromModelName)
- , fromName = fromInfl.constructor.singular
- , fromTableName = this._tableizeModelName(fromName)
- , toInfl = utils.string.getInflections(toModelName)
- , toName = toInfl.constructor.singular
- , toTableName = this._tableizeModelName(toName)
- , toColName = this._columnizePropertyName(fromInfl.property.singular + 'Id');
- return 'LEFT OUTER JOIN ' + toTableName + ' ON (' +
- fromTableName + '."id" = ' + toTableName + '.' + toColName + ')';
+ this._createLeftOuterJoinStatement = function (mainModelName, assnModelName) {
+ var mainInfl
+ , mainName
+ , mainTableName
+ , assnInfl
+ , assnName
+ , assnModelName
+ , assnTableName
+ , assnModelTableName
+ , assnColName
+
+ mainInfl = utils.string.getInflections(mainModelName);
+ mainName = mainInfl.constructor.singular;
+ mainTableName = this._tableizeModelName(mainName);
+ assnInfl = utils.string.getInflections(assnModelName);
+ assnName = assnInfl.constructor.singular;
+ assnModelName = _getModelNameForAssociation(mainName, assnName);
+ assnTableName = this._tableizeModelName(assnName);
+ assnModelTableName = this._tableizeModelName(assnModelName);
+ if (assnName == assnModelName) {
+ assnColName = assnName + 'Id';
+ }
+ else {
+ assnColName = assnName + assnModelName + 'Id';
+ }
+ assnColName = this._columnizePropertyName(assnColName);
+
+ return 'LEFT OUTER JOIN ' + assnModelTableName + ' ' + assnTableName +' ON (' +
+ mainTableName + '."id" = ' + assnTableName + '.' + assnColName + ')';
};
this._createInsertStatement = function (item, props, useAutoIncrementId) {
View
2  lib/adapters/sql/postgres.js
@@ -112,7 +112,7 @@ utils.mixin(Adapter.prototype, new (function () {
sql += 'SELECT ';
sql += selects.map(function (item) {
- return self._createSelectStatement(item);
+ return self._createSelectStatement(item, tableName);
}).join(', ');
sql += ' ';
sql += 'FROM ' + tableName;
View
8 test/adapters/shared.js
@@ -459,12 +459,12 @@ tests = {
if (err) {
throw err;
}
- user.addFriend(User.create({
+ user.addKid(User.create({
login: 'qwer'
, password: 'zerb'
, confirmPassword: 'zerb'
}));
- user.addFriend(User.create({
+ user.addKid(User.create({
login: 'zxcv'
, password: 'zerb'
, confirmPassword: 'zerb'
@@ -473,7 +473,7 @@ tests = {
if (err) {
throw err;
}
- user.getFriends(function (err, data) {
+ user.getKids(function (err, data) {
assert.equal(2, data.length);
if (err) {
throw err;
@@ -534,7 +534,7 @@ tests = {
}
, 'test includes eager-fetch of hasMany association': function (next) {
- User.all({}, {includes: ['accounts', 'profiles']}, function (err, data) {
+ User.all({}, {includes: ['kids']}, function (err, data) {
throw new Error();
next();
});
View
2  test/fixtures/user.js
@@ -14,7 +14,7 @@ var User = function () {
this.hasOne('Profile');
this.hasMany('Accounts');
- this.hasMany('Friends', {model: 'Users'});
+ this.hasMany('Kids', {model: 'Users'});
this.hasMany('Avatars', {model: 'Profiles'});
};
Please sign in to comment.
Something went wrong with that request. Please try again.