diff --git a/core/server/data/migration/000.js b/core/server/data/migration/000.js index e9c309bffb..003e62b3c5 100644 --- a/core/server/data/migration/000.js +++ b/core/server/data/migration/000.js @@ -131,19 +131,19 @@ up = function () { down = function () { return when.all([ - knex.Schema.dropTableIfExists('posts'), - knex.Schema.dropTableIfExists('users'), - knex.Schema.dropTableIfExists('roles'), - knex.Schema.dropTableIfExists('settings'), - knex.Schema.dropTableIfExists('permissions'), - knex.Schema.dropTableIfExists('tags') + knex.Schema.dropTableIfExists('posts_tags'), + knex.Schema.dropTableIfExists('roles_users'), + knex.Schema.dropTableIfExists('permissions_users'), + knex.Schema.dropTableIfExists('permissions_roles'), + knex.Schema.dropTableIfExists('users') + ]).then(function () { - // Drop the relation tables after the model tables return when.all([ - knex.Schema.dropTableIfExists('roles_users'), - knex.Schema.dropTableIfExists('permissions_users'), - knex.Schema.dropTableIfExists('permissions_roles'), - knex.Schema.dropTableIfExists('posts_tags') + knex.Schema.dropTableIfExists('roles'), + knex.Schema.dropTableIfExists('settings'), + knex.Schema.dropTableIfExists('permissions'), + knex.Schema.dropTableIfExists('tags'), + knex.Schema.dropTableIfExists('posts') ]); }); }; diff --git a/core/server/models/post.js b/core/server/models/post.js index de77f75df9..74a197c263 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -128,7 +128,7 @@ Post = GhostBookshelf.Model.extend({ } }); - if (tagsToAttach) { + if (!_.isEmpty(tagsToAttach)) { return Tags.forge().query('whereIn', 'name', _.pluck(tagsToAttach, 'name')).fetch().then(function (matchingTags) { _.each(matchingTags.toJSON(), function (matchingTag) { tagOperations.push(self.tags().attach(matchingTag.id)); @@ -332,6 +332,19 @@ Post = GhostBookshelf.Model.extend({ // associated models can't be created until the post has an ID, so run this after return post.updateTags(newPostData.tags); }); + }, + destroy: function (_identifier, options) { + options = options || {}; + return this.forge({id: _identifier}).fetch({withRelated: ['tags']}).then(function destroyTags(post) { + var tagIds = _.pluck(post.related('tags').toJSON(), 'id'); + if (tagIds) { + return post.tags().detach(tagIds).then(function destroyPost() { + return post.destroy(options); + }); + } + + return post.destroy(options); + }); } }); diff --git a/core/test/unit/api_posts_spec.js b/core/test/unit/api_posts_spec.js index f5cc49d327..6731d9222b 100644 --- a/core/test/unit/api_posts_spec.js +++ b/core/test/unit/api_posts_spec.js @@ -139,11 +139,11 @@ describe('Post Model', function () { createdPost.has('html').should.equal(true); createdPost.get('html').should.equal('
' + newPost.markdown + '
'); createdPost.get('slug').should.equal('test-title-1'); - createdPost.get('created_at').should.be.below(new Date().getTime()).and.be.above(new Date(0).getTime()); + createdPost.get('created_at').should.be.above(new Date(0).getTime()); createdPost.get('created_by').should.equal(1); createdPost.get('author_id').should.equal(1); createdPost.get('created_by').should.equal(createdPost.get('author_id')); - createdPost.get('updated_at').should.be.below(new Date().getTime()).and.be.above(new Date(0).getTime()); + createdPost.get('updated_at').should.be.above(new Date(0).getTime()); createdPost.get('updated_by').should.equal(1); should.equal(createdPost.get('published_at'), null); should.equal(createdPost.get('published_by'), null); diff --git a/core/test/unit/api_settings_spec.js b/core/test/unit/api_settings_spec.js index fa7b25f598..ff8351ce21 100644 --- a/core/test/unit/api_settings_spec.js +++ b/core/test/unit/api_settings_spec.js @@ -76,13 +76,7 @@ describe('Settings Model', function () { results.length.should.be.above(0); - firstSetting = results.models[1]; - - // The edit method has been modified to take an object of - // key/value pairs - firstSetting.set('value', 'new value'); - - return SettingsModel.edit(firstSetting); + return SettingsModel.edit({key: "description", value: "new value"}); }).then(function (edited) { @@ -92,7 +86,7 @@ describe('Settings Model', function () { edited = edited[0]; - edited.attributes.key.should.equal(firstSetting.attributes.key); + edited.attributes.key.should.equal('description'); edited.attributes.value.should.equal('new value'); done(); @@ -111,13 +105,8 @@ describe('Settings Model', function () { results.length.should.be.above(0); - model1 = results.models[1]; - model2 = results.models[2]; - - // The edit method has been modified to take an object of - // key/value pairs - model1.set('value', 'new value1'); - model2.set('value', 'new value2'); + model1 = {key: "description", value: "another new value"}; + model2 = {key: "title", value: "new title"}; return SettingsModel.edit([model1, model2]); @@ -129,13 +118,13 @@ describe('Settings Model', function () { editedModel = edited[0]; - editedModel.attributes.key.should.equal(model1.attributes.key); - editedModel.attributes.value.should.equal('new value1'); + editedModel.attributes.key.should.equal(model1.key); + editedModel.attributes.value.should.equal(model1.value); editedModel = edited[1]; - editedModel.attributes.key.should.equal(model2.attributes.key); - editedModel.attributes.value.should.equal('new value2'); + editedModel.attributes.key.should.equal(model2.key); + editedModel.attributes.value.should.equal(model2.value); done(); diff --git a/core/test/unit/api_tags_spec.js b/core/test/unit/api_tags_spec.js index 3acec15709..072db66b9d 100644 --- a/core/test/unit/api_tags_spec.js +++ b/core/test/unit/api_tags_spec.js @@ -130,7 +130,7 @@ describe('Tag Model', function () { return postModel.save(); }).then(function (postModel) { var tagNames = postModel.related('tags').models.map(function (t) { return t.attributes.name; }); - tagNames.should.eql(seededTagNames); + tagNames.sort().should.eql(seededTagNames); return TagModel.findAll(); }).then(function (tagsFromDB) { @@ -155,7 +155,7 @@ describe('Tag Model', function () { return PostModel.read({id: postModel.id}, { withRelated: ['tags']}); }).then(function (reloadedPost) { var tagNames = reloadedPost.related('tags').models.map(function (t) { return t.attributes.name; }); - tagNames.should.eql(['tag1', 'tag3']); + tagNames.sort().should.eql(['tag1', 'tag3']); done(); }).then(null, done); @@ -180,7 +180,7 @@ describe('Tag Model', function () { }).then(function (reloadedPost) { var tagModels = reloadedPost.related('tags').models, tagNames = tagModels.map(function (t) { return t.attributes.name; }); - tagNames.should.eql(['tag1', 'tag2', 'tag3']); + tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']); tagModels[2].id.should.eql(4); // make sure it hasn't just added a new tag with the same name done(); @@ -201,7 +201,7 @@ describe('Tag Model', function () { return PostModel.read({id: postModel.id}, { withRelated: ['tags']}); }).then(function (reloadedPost) { var tagNames = reloadedPost.related('tags').models.map(function (t) { return t.attributes.name; }); - tagNames.should.eql(['tag1', 'tag2', 'tag3']); + tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']); done(); }).then(null, done); diff --git a/core/test/unit/permissions_spec.js b/core/test/unit/permissions_spec.js index 112d20f4ec..468e8aab7f 100644 --- a/core/test/unit/permissions_spec.js +++ b/core/test/unit/permissions_spec.js @@ -94,7 +94,7 @@ describe('permissions', function () { .then(function (actionsMap) { should.exist(actionsMap); - actionsMap.edit.should.eql(['post', 'tag', 'user', 'page']); + actionsMap.edit.sort().should.eql(['post', 'tag', 'user', 'page'].sort()); actionsMap.should.equal(permissions.actionsMap); diff --git a/core/test/unit/testUtils.js b/core/test/unit/testUtils.js index 489ff5f7b5..7f1310c881 100644 --- a/core/test/unit/testUtils.js +++ b/core/test/unit/testUtils.js @@ -37,7 +37,7 @@ sampleUser = function (i) { email: "joe_" + i + "@bloggs.com", password: "$2a$10$c5G9RS5.dXRt3UqvZ5wNgOLQLc7ZFc2DJo01du0oLT1YYOM67KJMe", created_by: 1, - created_at: 1234567890 + created_at: new Date() }; };