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

Commit 2c34249d by Verdier

Use has-one scope in creator

1 parent 4516bebd
...@@ -209,6 +209,13 @@ HasOne.prototype.injectCreator = function(instancePrototype) { ...@@ -209,6 +209,13 @@ HasOne.prototype.injectCreator = function(instancePrototype) {
values = values || {}; values = values || {};
options = options || {}; options = options || {};
if (association.scope) {
Object.keys(association.scope).forEach(function (attribute) {
values[attribute] = association.scope[attribute];
if (options.fields) options.fields.push(attribute);
});
}
values[association.foreignKey] = instance.get(association.sourceIdentifier); values[association.foreignKey] = instance.get(association.sourceIdentifier);
if (options.fields) options.fields.push(association.foreignKey); if (options.fields) options.fields.push(association.foreignKey);
return association.target.create(values, options); return association.target.create(values, options);
......
...@@ -18,7 +18,10 @@ describe(Support.getTestDialectTeaser('associations'), function() { ...@@ -18,7 +18,10 @@ describe(Support.getTestDialectTeaser('associations'), function() {
title: Sequelize.STRING, title: Sequelize.STRING,
commentable: Sequelize.STRING, commentable: Sequelize.STRING,
commentable_id: Sequelize.INTEGER, commentable_id: Sequelize.INTEGER,
isMain: Sequelize.BOOLEAN isMain: {
type: Sequelize.BOOLEAN,
defaultValue: false
}
}, { }, {
instanceMethods: { instanceMethods: {
getItem: function() { getItem: function() {
...@@ -97,18 +100,22 @@ describe(Support.getTestDialectTeaser('associations'), function() { ...@@ -97,18 +100,22 @@ describe(Support.getTestDialectTeaser('associations'), function() {
return post.createComment({ return post.createComment({
title: 'I am a post comment' title: 'I am a post comment'
}); });
}).then(function() { }).then(function(comment) {
return self.Post.scope('withMainComment').findById(this.post.id); expect(comment.get('commentable')).to.equal('post');
expect(comment.get('isMain')).to.be.false;
return self.Post.scope('withMainComment').findById(this.post.get('id'));
}).then(function(post) { }).then(function(post) {
expect(post.mainComment).to.be.null; expect(post.mainComment).to.be.null;
return Promise.join( return post.createMainComment({
post.createMainComment({
title: 'I am a main post comment' title: 'I am a main post comment'
}), });
self.Post.scope('withMainComment').findById(this.post.id) }).then(function(comment) {
); this.mainComment = comment;
}).spread(function(mainComment, post) { expect(comment.get('commentable')).to.equal('post');
expect(post.mainComment.id).to.equal(mainComment.id); expect(comment.get('isMain')).to.be.true;
return self.Post.scope('withMainComment').findById(this.post.id);
}).then(function (post) {
expect(post.mainComment.get('id')).to.equal(this.mainComment.get('id'));
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!