Skip to content

Commit

Permalink
Moved a few chunks of code.
Browse files Browse the repository at this point in the history
  • Loading branch information
wbyoung committed Jan 1, 2015
1 parent 5371eb8 commit 8a9071f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 95 deletions.
162 changes: 67 additions & 95 deletions lib/relations/has_many.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var _ = require('lodash');
var util = require('util');
var BluebirdPromise = require('bluebird');
var BaseRelation = require('./base');
var Actionable = require('../util/actionable');
Expand Down Expand Up @@ -85,79 +84,6 @@ var notify = function(name) {

HasMany.reopen(/** @lends HasMany# */ {

/**
* Determine if this relation is a many-to-many relationship, that is if the
* inverse relationship is a many relationship as well. Has many
* relationships are not many-to-many by default.
*
* Mixins can override {@link HasMany#inverseIsMany} when they work in such a
* way to allow many-to-many relationships. This is the default
* implementation.
*
* @return {Boolean} True if the relationship is many-to-many.
*/
_inverseIsMany: function() {
return false;
},

/**
* Prepare many related objects for a foreign key update.
*
* This will save any dirty records unless the only attribute that is dirty
* is the foreign key. It will also save any newly created objects regardless
* of whether or not they are dirty.
*
* @method
* @private
* @param {Model} instance The model instance on which to operate.
* @param {Array.<Model>} relatedObjects The related models to prepare.
* @return {Promise} A promise that resolves when the prep work has finished.
*/
_prepareForForeignKeyUpdate: function(instance, relatedObjects) {
var foreignKey = this.foreignKey;
return BluebirdPromise.map(relatedObjects, function(obj) {
var dirty = _.without(obj.dirtyAttributes, foreignKey);
var save = dirty.length || obj.newRecord;
return save && obj.save();
});
},

/**
* Update foreign keys for a set of related objects.
*
* This will update the foreign keys of several objects in a single query.
* This allows much faster creation of associations at the database level.
*
* After a successful update, it will clean the foreign key attribute of
* each updated object, likely leaving the object completely clean.
*
* @method
* @private
* @param {Model} instance The model instance on which to operate.
* @param {Array.<Model>} relatedObjects The related models on which to
* update keys.
* @param {?} id The id to use as the value of the foreign key.
* @return {Promise} A promise that resolves when the update has finished.
*/
_updateForeignKeys: function(instance, relatedObjects, id) {
if (!_.any(relatedObjects, 'dirty')) { return BluebirdPromise.bind(); }

var foreignKey = this.foreignKey;
var relatedIds = _.map(relatedObjects, 'id');

var updates = _.object([[foreignKey, id]]);
var conditions = {};
if (relatedIds.length === 1) { conditions.id = relatedIds[0]; }
else { conditions['id[in]'] = relatedIds; }

var query = this._relatedModel.objects;
return query.where(conditions).update(updates).execute().then(function() {
relatedObjects.forEach(function(obj) {
obj.cleanAttribute(foreignKey);
});
});
},

/**
* The identity property for this relation.
*
Expand All @@ -183,30 +109,18 @@ HasMany.reopen(/** @lends HasMany# */ {
},

/**
* The collection property for this relation.
*
* This property allows access to the cached objects that have been fetched
* for a specific model in a given relation. Before the cache has been
* filled, accessing this property will throw an exception.
*
* It is accessible on an individual model via `<plural>`. For
* instance, a user that has many articles would cause this method to get
* triggered via `user.articles`.
* Determine if this relation is a many-to-many relationship, that is if the
* inverse relationship is a many relationship as well. Has many
* relationships are not many-to-many by default.
*
* The naming conventions are set forth in {@link HasMany.methods}.
* Mixins can override {@link HasMany#inverseIsMany} when they work in such a
* way to allow many-to-many relationships. This is the default
* implementation.
*
* @method
* @protected
* @param {Model} instance The model instance on which to operate.
* @see {@link BaseRelation#methods}
* @return {Boolean} True if the relationship is many-to-many.
*/
collection: function(instance) {
var result = this._getCollectionCache(instance);
if (!result) {
throw new Error(util.format('The relation "%s" has not yet been ' +
'loaded.', this._name));
}
return result;
_inverseIsMany: function() {
return false;
},

/**
Expand Down Expand Up @@ -403,6 +317,64 @@ HasMany.reopen(/** @lends HasMany# */ {
return query.execute().tap(after);
},

/**
* Prepare many related objects for a foreign key update.
*
* This will save any dirty records unless the only attribute that is dirty
* is the foreign key. It will also save any newly created objects regardless
* of whether or not they are dirty.
*
* @method
* @private
* @param {Model} instance The model instance on which to operate.
* @param {Array.<Model>} relatedObjects The related models to prepare.
* @return {Promise} A promise that resolves when the prep work has finished.
*/
_prepareForForeignKeyUpdate: function(instance, relatedObjects) {
var foreignKey = this.foreignKey;
return BluebirdPromise.map(relatedObjects, function(obj) {
var dirty = _.without(obj.dirtyAttributes, foreignKey);
var save = dirty.length || obj.newRecord;
return save && obj.save();
});
},

/**
* Update foreign keys for a set of related objects.
*
* This will update the foreign keys of several objects in a single query.
* This allows much faster creation of associations at the database level.
*
* After a successful update, it will clean the foreign key attribute of
* each updated object, likely leaving the object completely clean.
*
* @method
* @private
* @param {Model} instance The model instance on which to operate.
* @param {Array.<Model>} relatedObjects The related models on which to
* update keys.
* @param {?} id The id to use as the value of the foreign key.
* @return {Promise} A promise that resolves when the update has finished.
*/
_updateForeignKeys: function(instance, relatedObjects, id) {
if (!_.any(relatedObjects, 'dirty')) { return BluebirdPromise.bind(); }

var foreignKey = this.foreignKey;
var relatedIds = _.map(relatedObjects, 'id');

var updates = _.object([[foreignKey, id]]);
var conditions = {};
if (relatedIds.length === 1) { conditions.id = relatedIds[0]; }
else { conditions['id[in]'] = relatedIds; }

var query = this._relatedModel.objects;
return query.where(conditions).update(updates).execute().then(function() {
relatedObjects.forEach(function(obj) {
obj.cleanAttribute(foreignKey);
});
});
},

/**
* Override of {@link HasMany#associateFetchedObjects} to call before & after
* hooks so that mixins can get involved in the association process.
Expand Down
28 changes: 28 additions & 0 deletions lib/relations/has_many_collection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _ = require('lodash');
var util = require('util');
var Mixin = require('../util/mixin');

_.str = require('underscore.string');
Expand All @@ -17,6 +18,33 @@ module.exports = Mixin.create(/** @lends HasMany# */ {
this._collectionCacheKey = '_' + this._name;
},

/**
* The collection property for this relation.
*
* This property allows access to the cached objects that have been fetched
* for a specific model in a given relation. Before the cache has been
* filled, accessing this property will throw an exception.
*
* It is accessible on an individual model via `<plural>`. For
* instance, a user that has many articles would cause this method to get
* triggered via `user.articles`.
*
* The naming conventions are set forth in {@link HasMany.methods}.
*
* @method
* @protected
* @param {Model} instance The model instance on which to operate.
* @see {@link BaseRelation#methods}
*/
collection: function(instance) {
var result = this._getCollectionCache(instance);
if (!result) {
throw new Error(util.format('The relation "%s" has not yet been ' +
'loaded.', this._name));
}
return result;
},

/**
* Get the {@link HasMany#collection} cache value.
*
Expand Down

0 comments on commit 8a9071f

Please sign in to comment.