Fix bug preventing tags from being saved when a Post is created.

Fixes #658
This commit is contained in:
Adam Howard 2013-09-13 14:29:59 +01:00
parent c82ebb15d8
commit f6b86a98ff
2 changed files with 26 additions and 3 deletions

View File

@ -140,17 +140,20 @@ Post = GhostBookshelf.Model.extend({
return checkIfSlugExists(slug);
},
updateTags: function () {
updateTags: function (newTags) {
var self = this,
tagOperations = [],
newTags = this.get('tags'),
tagsToDetach,
existingTagIDs,
tagsToCreateAndAdd,
tagsToAddByID,
fetchOperation;
if (!newTags) {
if (newTags === this) {
newTags = this.get('tags');
}
if (!newTags || !this.id) {
return;
}
@ -371,6 +374,13 @@ Post = GhostBookshelf.Model.extend({
// Otherwise, you shall not pass.
return when.reject();
},
add: function (newPostData, options) {
return GhostBookshelf.Model.add.call(this, newPostData, options).tap(function (post) {
// associated models can't be created until the post has an ID, so run this after
return post.updateTags(newPostData.tags);
});
}
});

View File

@ -207,6 +207,19 @@ describe('Tag Model', function () {
done();
}).then(null, done);
});
it('can add a tag to a post on creation', function (done) {
var newPost = {title: 'Test Title 1', content_raw: 'Test Content 1', tags: ['test_tag_1']};
PostModel.add(newPost).then(function (createdPost) {
return PostModel.read({id: createdPost.id}, { withRelated: ['tags']});
}).then(function (postWithTag) {
postWithTag.related('tags').length.should.equal(1);
done();
}).then(null, done);
});
});
});