diff --git a/core/server/models/post.js b/core/server/models/post.js index e418f1352c..ba114af9f3 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -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); + }); } }); diff --git a/core/test/unit/api_tags_spec.js b/core/test/unit/api_tags_spec.js index df26540a8f..557433fd3d 100644 --- a/core/test/unit/api_tags_spec.js +++ b/core/test/unit/api_tags_spec.js @@ -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); + + }); }); });