Code coverage report for upstream/lib/associations/has-many-single-linked.js

Statements: 93.1% (54 / 58)      Branches: 66.67% (26 / 39)      Functions: 77.78% (14 / 18)      Lines: 92.98% (53 / 57)     

All files » upstream/lib/associations/ » has-many-single-linked.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 1251     1 1 81 81     1 50     50   216 50 50   50 7 7 7 7     43     50     1 28           4         32           28 3 3     28   3 4 4     3 3   3       3 3             28   25               25 32 32     25 25 25   25             28   28   28     1 3     3   3 3   3     1    
var Utils       = require('./../utils')
  , Transaction = require('./../transaction')
 
module.exports = (function() {
  var HasManySingleLinked = function(definition, instance) {
    this.__factory = definition
    this.instance = instance
  }
 
  HasManySingleLinked.prototype.injectGetter = function(options) {
    var self = this
      , where = {}
      , smart
    options = options || {}
 
    var primaryKey = Object.keys(this.instance.rawAttributes).filter(function(k) { return self.instance.rawAttributes[k].primaryKey === true })
    primaryKey = primaryKey.length === 1 ? primaryKey[0] : 'id'
    where[this.__factory.identifier] = this.instance[primaryKey]
 
    if (options.where) {
      smart = Utils.smartWhere([where, options.where], this.__factory.target.daoFactoryManager.sequelize.options.dialect)
      smart = Utils.compileSmartWhere.call(this.__factory.target, smart, this.__factory.target.daoFactoryManager.sequelize.options.dialect)
      Eif (smart.length > 0) {
        options.where = smart
      }
    } else {
      options.where = where
    }
 
    return this.__factory.target.all(options)
  }
 
  HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations, defaultAttributes) {
    var self                 = this
      , associationKeys      = Object.keys((oldAssociations[0] || newAssociations[0] || {daoFactory: {primaryKeys: {}}}).daoFactory.primaryKeys || {})
      , associationKey       = (associationKeys.length === 1) ? associationKeys[0] : 'id'
      , chainer              = new Utils.QueryChainer()
      , options              = {}
      , obsoleteAssociations = oldAssociations.filter(function (old) {
          return !Utils._.find(newAssociations, function (obj) {
            return obj[associationKey] === old[associationKey]
          })
        })
      , unassociatedObjects  = newAssociations.filter(function (obj) {
          return !Utils._.find(oldAssociations, function (old) {
            return obj[associationKey] === old[associationKey]
          })
        })
      , update
 
    if ((defaultAttributes || {}).transaction instanceof Transaction) {
      options.transaction = defaultAttributes.transaction
      delete defaultAttributes.transaction
    }
 
    if (obsoleteAssociations.length > 0) {
      // clear the old associations
      var obsoleteIds = obsoleteAssociations.map(function(associatedObject) {
        associatedObject[self.__factory.identifier] = (newAssociations.length < 1 ? null : self.instance.id)
        return associatedObject[associationKey]
      })
 
      update = {}
      update[self.__factory.identifier] = null
 
      var primaryKeys = Object.keys(this.__factory.target.primaryKeys)
        , primaryKey  = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
        , updateWhere = {}
 
      updateWhere[primaryKey] = obsoleteIds
      chainer.add(this.__factory.target.update(
        update,
        updateWhere,
        Utils._.extend(options, { allowNull: [self.__factory.identifier] })
      ))
    }
 
    if (unassociatedObjects.length > 0) {
      // For the self.instance
      var pkeys       = Object.keys(self.instance.daoFactory.primaryKeys)
        , pkey        = pkeys.length === 1 ? pkeys[0] : 'id'
        // For chainer
        , primaryKeys = Object.keys(this.__factory.target.primaryKeys)
        , primaryKey  = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
        , updateWhere = {}
 
      // set the new associations
      var unassociatedIds = unassociatedObjects.map(function(associatedObject) {
        associatedObject[self.__factory.identifier] = self.instance[pkey] || self.instance.id
        return associatedObject[associationKey]
      })
 
      update                            = {}
      update[self.__factory.identifier] = (newAssociations.length < 1 ? null : self.instance[pkey] || self.instance.id)
      updateWhere[primaryKey]           = unassociatedIds
 
      chainer.add(this.__factory.target.update(
        update,
        updateWhere,
        Utils._.extend(options, { allowNull: [self.__factory.identifier] })
      ))
    }
 
    chainer
      .run()
      .success(function() { emitter.emit('success', newAssociations) })
      .error(function(err) { emitter.emit('error', err) })
      .on('sql', function(sql) { emitter.emit('sql', sql) })
  }
 
  HasManySingleLinked.prototype.injectAdder = function(emitterProxy, newAssociation) {
    var primaryKeys = Object.keys(this.instance.daoFactory.primaryKeys)
      , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
 
    newAssociation[this.__factory.identifier] = this.instance[primaryKey]
 
    newAssociation.save()
      .success(function() { emitterProxy.emit('success', newAssociation) })
      .error(function(err) { emitterProxy.emit('error', err) })
      .on('sql', function(sql) { emitterProxy.emit('sql', sql) })
  }
 
  return HasManySingleLinked
})()