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

Commit df205ab9 by Verdier

Use has-one scope in getter and setter

1 parent 2c34249d
......@@ -135,6 +135,10 @@ HasOne.prototype.injectGetter = function(instancePrototype) {
var where = {};
where[association.foreignKey] = this.get(association.sourceIdentifier);
if (association.scope) {
_.assign(where, association.scope);
}
options = association.target.__optClone(options) || {};
options.where = {
......@@ -191,7 +195,10 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
isNewRecord: false
});
}
_.assign(associatedInstance, association.scope);
associatedInstance.set(association.foreignKey, instance.get(association.sourceIdentifier));
return associatedInstance.save(options);
}
return null;
......
......@@ -103,19 +103,34 @@ describe(Support.getTestDialectTeaser('associations'), function() {
}).then(function(comment) {
expect(comment.get('commentable')).to.equal('post');
expect(comment.get('isMain')).to.be.false;
return self.Post.scope('withMainComment').findById(this.post.get('id'));
return this.Post.scope('withMainComment').findById(this.post.get('id'));
}).then(function(post) {
expect(post.mainComment).to.be.null;
return post.createMainComment({
title: 'I am a main post comment'
title: 'I am a main post comment'
});
}).then(function(comment) {
this.mainComment = comment;
expect(comment.get('commentable')).to.equal('post');
expect(comment.get('isMain')).to.be.true;
return self.Post.scope('withMainComment').findById(this.post.id);
}).then(function(mainComment) {
this.mainComment = mainComment;
expect(mainComment.get('commentable')).to.equal('post');
expect(mainComment.get('isMain')).to.be.true;
return this.Post.scope('withMainComment').findById(this.post.id);
}).then(function (post) {
expect(post.mainComment.get('id')).to.equal(this.mainComment.get('id'));
return post.getMainComment();
}).then(function (mainComment, post) {
expect(mainComment.get('commentable')).to.equal('post');
expect(mainComment.get('isMain')).to.be.true;
return this.Comment.create({
title: 'I am a future main comment'
});
}).then(function (comment) {
return this.post.setMainComment(comment);
}).then( function () {
return this.post.getMainComment();
}).then(function (mainComment) {
expect(mainComment.get('commentable')).to.equal('post');
expect(mainComment.get('isMain')).to.be.true;
expect(mainComment.get('title')).to.equal('I am a future main comment');
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!