Skip to content
This repository
Browse code

Fucking named associations

  • Loading branch information...
commit 7ba54cafe426f426ff2f077fac80aeab634f2856 1 parent 40e72a8
Matthew Eernisse authored March 26, 2013
88  lib/adapters/sql/base.js
@@ -13,38 +13,92 @@ Adapter = function () {
13 13
 Adapter.prototype = new BaseAdapter();
14 14
 utils.mixin(Adapter.prototype, new (function () {
15 15
 
  16
+  var _getModelNameForAssociation = function (mainName, assnName) {
  17
+    var assn
  18
+      , assnItem;
  19
+    if (model.descriptionRegistry[assnName]) {
  20
+      return assnName;
  21
+    }
  22
+    assn = model.descriptionRegistry[mainName].associations;
  23
+    for (var p in assn) {
  24
+      assnItem = assn[p][assnName];
  25
+      if (assnItem) {
  26
+        return assnItem.model;
  27
+      }
  28
+    }
  29
+  };
  30
+
16 31
   this._tableizeModelName = function (name) {
17 32
     var tableName = utils.inflection.pluralize(name);
18 33
     tableName = utils.string.snakeize(tableName);
19 34
     return tableName;
20 35
   };
21 36
 
22  
-  this._createSelectStatement = function (modelName) {
  37
+  this._createSelectStatement = function (modelName, ownerModelName) {
  38
+
23 39
     var infl = utils.string.getInflections(modelName)
24  
-      , name = infl.constructor.singular
25  
-      , tableName = this._tableizeModelName(name)
26  
-      , props = model.descriptionRegistry[name].properties
27  
-      , propArr = [];
  40
+      , name
  41
+      , assumedName = infl.constructor.singular
  42
+      , ownerName
  43
+      , tableName
  44
+      , assumedTableName
  45
+      , props
  46
+      , propArr;
28 47
 
29  
-    propArr.push(tableName + '."id" AS "' + tableName + '#id"');
  48
+    // Assumed name is a real model
  49
+    if (model.descriptionRegistry[assumedName]) {
  50
+      name = assumedName;
  51
+    }
  52
+    // Otherwise it's a named association, need to look up the
  53
+    // actual model via it's owner's associations list
  54
+    else {
  55
+      infl = utils.string.getInflections(ownerModelName);
  56
+      ownerName = infl.constructor.singular;
  57
+      name = _getModelNameForAssociation(ownerName, assumedName);
  58
+    }
  59
+
  60
+    tableName = this._tableizeModelName(name);
  61
+    assumedTableName = this._tableizeModelName(assumedName);
  62
+    props = model.descriptionRegistry[name].properties;
  63
+    propArr = [];
  64
+    propArr.push(assumedTableName + '."id" AS "' + assumedTableName + '#id"');
30 65
 
31 66
     for (var p in props) {
32 67
       propArr.push(tableName + '.' + this._columnizePropertyName(p) + ' AS "' +
33  
-        tableName + '#' + this._columnizePropertyName(p, {useQuotes: false}) + '"');
  68
+        assumedTableName + '#' + this._columnizePropertyName(p, {useQuotes: false}) + '"');
34 69
     }
35 70
     return propArr.join(', ');
36 71
   };
37 72
 
38  
-  this._createLeftOuterJoinStatement = function (fromModelName, toModelName) {
39  
-    var fromInfl = utils.string.getInflections(fromModelName)
40  
-      , fromName = fromInfl.constructor.singular
41  
-      , fromTableName = this._tableizeModelName(fromName)
42  
-      , toInfl = utils.string.getInflections(toModelName)
43  
-      , toName = toInfl.constructor.singular
44  
-      , toTableName = this._tableizeModelName(toName)
45  
-      , toColName = this._columnizePropertyName(fromInfl.property.singular + 'Id');
46  
-      return 'LEFT OUTER JOIN ' + toTableName + ' ON (' +
47  
-          fromTableName + '."id" = ' + toTableName + '.' + toColName + ')';
  73
+  this._createLeftOuterJoinStatement = function (mainModelName, assnModelName) {
  74
+    var mainInfl
  75
+      , mainName
  76
+      , mainTableName
  77
+      , assnInfl
  78
+      , assnName
  79
+      , assnModelName
  80
+      , assnTableName
  81
+      , assnModelTableName
  82
+      , assnColName
  83
+
  84
+    mainInfl = utils.string.getInflections(mainModelName);
  85
+    mainName = mainInfl.constructor.singular;
  86
+    mainTableName = this._tableizeModelName(mainName);
  87
+    assnInfl = utils.string.getInflections(assnModelName);
  88
+    assnName = assnInfl.constructor.singular;
  89
+    assnModelName = _getModelNameForAssociation(mainName, assnName);
  90
+    assnTableName = this._tableizeModelName(assnName);
  91
+    assnModelTableName = this._tableizeModelName(assnModelName);
  92
+    if (assnName == assnModelName) {
  93
+      assnColName = assnName + 'Id';
  94
+    }
  95
+    else {
  96
+      assnColName = assnName + assnModelName + 'Id';
  97
+    }
  98
+    assnColName = this._columnizePropertyName(assnColName);
  99
+
  100
+      return 'LEFT OUTER JOIN ' + assnModelTableName + ' ' + assnTableName +' ON (' +
  101
+          mainTableName + '."id" = ' + assnTableName + '.' + assnColName + ')';
48 102
   };
49 103
 
50 104
   this._createInsertStatement = function (item, props, useAutoIncrementId) {
2  lib/adapters/sql/postgres.js
@@ -112,7 +112,7 @@ utils.mixin(Adapter.prototype, new (function () {
112 112
 
113 113
     sql += 'SELECT ';
114 114
     sql += selects.map(function (item) {
115  
-      return self._createSelectStatement(item);
  115
+      return self._createSelectStatement(item, tableName);
116 116
     }).join(', ');
117 117
     sql += ' ';
118 118
     sql += 'FROM ' + tableName;
8  test/adapters/shared.js
@@ -459,12 +459,12 @@ tests = {
459 459
         if (err) {
460 460
           throw err;
461 461
         }
462  
-        user.addFriend(User.create({
  462
+        user.addKid(User.create({
463 463
           login: 'qwer'
464 464
         , password: 'zerb'
465 465
         , confirmPassword: 'zerb'
466 466
         }));
467  
-        user.addFriend(User.create({
  467
+        user.addKid(User.create({
468 468
           login: 'zxcv'
469 469
         , password: 'zerb'
470 470
         , confirmPassword: 'zerb'
@@ -473,7 +473,7 @@ tests = {
473 473
           if (err) {
474 474
             throw err;
475 475
           }
476  
-          user.getFriends(function (err, data) {
  476
+          user.getKids(function (err, data) {
477 477
             assert.equal(2, data.length);
478 478
             if (err) {
479 479
               throw err;
@@ -534,7 +534,7 @@ tests = {
534 534
   }
535 535
 
536 536
 , 'test includes eager-fetch of hasMany association': function (next) {
537  
-    User.all({}, {includes: ['accounts', 'profiles']}, function (err, data) {
  537
+    User.all({}, {includes: ['kids']}, function (err, data) {
538 538
       throw new Error();
539 539
       next();
540 540
     });
2  test/fixtures/user.js
@@ -14,7 +14,7 @@ var User = function () {
14 14
 
15 15
   this.hasOne('Profile');
16 16
   this.hasMany('Accounts');
17  
-  this.hasMany('Friends', {model: 'Users'});
  17
+  this.hasMany('Kids', {model: 'Users'});
18 18
   this.hasMany('Avatars', {model: 'Profiles'});
19 19
 };
20 20
 

0 notes on commit 7ba54ca

Please sign in to comment.
Something went wrong with that request. Please try again.