2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

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
This commit is contained in:
Alan Richards 2014-04-21 18:08:11 -07:00
parent 190d31fb15
commit 686aec7ab5
3 changed files with 13 additions and 1 deletions

View file

@ -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;
}
}, {

View file

@ -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);

View file

@ -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',