From 686aec7ab5c9b343ba5ac41640617cc508e5e547 Mon Sep 17 00:00:00 2001 From: Alan Richards Date: Mon, 21 Apr 2014 18:08:11 -0700 Subject: [PATCH] Post API response to use author not author_id Closes #2608 - added toJSON method override for post model - in the event no expanded author relation is present the id will be used - removed author_id from expected response JSON for posts. - updated integration tests to check for existence or not of author and author_id --- core/server/models/post.js | 9 +++++++++ core/test/integration/model/model_posts_spec.js | 3 +++ core/test/utils/api.js | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/server/models/post.js b/core/server/models/post.js index 4b40582a9d..039de936fd 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -211,6 +211,15 @@ Post = ghostBookshelf.Model.extend({ fields: function () { return this.morphMany(AppField, 'relatable'); + }, + + toJSON: function (options) { + var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options); + + attrs.author = attrs.author || attrs.author_id; + delete attrs.author_id; + + return attrs; } }, { diff --git a/core/test/integration/model/model_posts_spec.js b/core/test/integration/model/model_posts_spec.js index c52ca6b9bf..4ca6327b8b 100644 --- a/core/test/integration/model/model_posts_spec.js +++ b/core/test/integration/model/model_posts_spec.js @@ -73,6 +73,7 @@ describe('Post Model', function () { results.length.should.be.above(0); firstPost = results.models[0].toJSON(); + should.not.exist(firstPost.author_id); firstPost.author.should.be.an.Object; firstPost.fields.should.be.an.Array; firstPost.author.name.should.equal(DataGenerator.Content.users[0].name); @@ -89,6 +90,7 @@ describe('Post Model', function () { should.exist(result); firstPost = result.toJSON(); + should.not.exist(firstPost.author_id); firstPost.author.should.be.an.Object; firstPost.fields.should.be.an.Array; firstPost.author.name.should.equal(testUtils.DataGenerator.Content.users[0].name); @@ -142,6 +144,7 @@ describe('Post Model', function () { 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.has('author').should.equal(false); createdPost.get('created_by').should.equal(createdPost.get('author_id')); createdPost.get('updated_at').should.be.above(new Date(0).getTime()); createdPost.get('updated_by').should.equal(1); diff --git a/core/test/utils/api.js b/core/test/utils/api.js index 817bb8b126..bb7a974e84 100644 --- a/core/test/utils/api.js +++ b/core/test/utils/api.js @@ -8,7 +8,7 @@ var _ = require('lodash'), posts: ['posts', 'meta'], pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'], post: ['id', 'uuid', 'title', 'slug', 'markdown', 'html', 'meta_title', 'meta_description', - 'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', + 'featured', 'image', 'status', 'language', 'created_at', 'created_by', 'updated_at', 'updated_by', 'published_at', 'published_by', 'page', 'author', 'tags', 'fields'], // TODO: remove databaseVersion, dbHash settings: ['databaseVersion', 'dbHash', 'title', 'description', 'email', 'logo', 'cover', 'defaultLang',