不要怂,就是干,撸起袖子干!

Commit ffbaf77f by Mick Hansen

refactor(has-many): move association methods to the association

To prepare for being able to run a association method on multiple source instances i've moved the association methods to the association prototype
instead of having them on the instance prototype, the instance prototype now only has proxy methods to the methods on the association.
1 parent 7e5b7c5a
...@@ -205,27 +205,26 @@ Mixin.belongsTo = singleLinked(BelongsTo); ...@@ -205,27 +205,26 @@ Mixin.belongsTo = singleLinked(BelongsTo);
* @param {string} [options.onUpdate='CASCADE'] * @param {string} [options.onUpdate='CASCADE']
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key. * @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
*/ */
Mixin.hasMany = function(targetModel, options) { // testhint options:none Mixin.hasMany = function(target, options) { // testhint options:none
if (!(targetModel instanceof this.sequelize.Model)) { if (!(target instanceof this.sequelize.Model)) {
throw new Error(this.name + '.hasMany called with something that\'s not an instance of Sequelize.Model'); throw new Error(this.name + '.hasMany called with something that\'s not an instance of Sequelize.Model');
} }
var sourceModel = this; var source = this;
// Since this is a mixin, we'll need a unique variable name for hooks (since Model will override our hooks option) // Since this is a mixin, we'll need a unique variable name for hooks (since Model will override our hooks option)
options = options || {}; options = options || {};
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks); options.hooks = options.hooks === undefined ? false : Boolean(options.hooks);
options.useHooks = options.hooks; options.useHooks = options.hooks;
options = _.extend(options, _.omit(sourceModel.options, ['hooks'])); options = _.extend(options, _.omit(source.options, ['hooks']));
// the id is in the foreign table or in a connecting table // the id is in the foreign table or in a connecting table
var association = new HasMany(sourceModel, targetModel, options); var association = new HasMany(source, target, options);
sourceModel.associations[association.associationAccessor] = association.injectAttributes(); source.associations[association.associationAccessor] = association;
association.injectGetter(sourceModel.Instance.prototype); association.injectAttributes();
association.injectSetter(sourceModel.Instance.prototype); association.mixin(source.Instance.prototype);
association.injectCreator(sourceModel.Instance.prototype);
return association; return association;
}; };
......
...@@ -7,11 +7,12 @@ var chai = require('chai') ...@@ -7,11 +7,12 @@ var chai = require('chai')
, stub = sinon.stub , stub = sinon.stub
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types') , DataTypes = require(__dirname + '/../../../lib/data-types')
, HasMany = require(__dirname + '/../../../lib/associations/has-many')
, current = Support.sequelize , current = Support.sequelize
, Promise = current.Promise; , Promise = current.Promise;
describe(Support.getTestDialectTeaser('hasMany'), function() { describe(Support.getTestDialectTeaser('hasMany'), function() {
describe('optimizations using bulk create, destroy and update', function() { describe('optimizations using bulk create, destroy and update', function() {
var User = current.define('User', { username: DataTypes.STRING }) var User = current.define('User', { username: DataTypes.STRING })
, Task = current.define('Task', { title: DataTypes.STRING }); , Task = current.define('Task', { title: DataTypes.STRING });
...@@ -61,4 +62,27 @@ describe(Support.getTestDialectTeaser('hasMany'), function() { ...@@ -61,4 +62,27 @@ describe(Support.getTestDialectTeaser('hasMany'), function() {
}); });
}); });
}); });
describe('mixin', function () {
var User = current.define('User')
, Task = current.define('Task');
it('should mixin association methods', function () {
var as = Math.random().toString()
, association = new HasMany(User, Task, {as: as})
, obj = {};
association.mixin(obj);
expect(obj[association.accessors.get]).to.be.an('function');
expect(obj[association.accessors.set]).to.be.an('function');
expect(obj[association.accessors.addMultiple]).to.be.an('function');
expect(obj[association.accessors.add]).to.be.an('function');
expect(obj[association.accessors.remove]).to.be.an('function');
expect(obj[association.accessors.removeMultiple]).to.be.an('function');
expect(obj[association.accessors.hasSingle]).to.be.an('function');
expect(obj[association.accessors.hasAll]).to.be.an('function');
expect(obj[association.accessors.count]).to.be.an('function');
});
});
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!