diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 3568822aad..3e57ecc089 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -29,6 +29,19 @@ coreHelpers = function (ghost) { return date; }); + + // ### Author Helper + // + // *Usage example:* + // `{{author}}` + // + // Returns the full name of the author of a given post, or a blank string + // if the author could not be determined. + // + ghost.registerThemeHelper('author', function (context, options) { + return this.author ? this.author.full_name : ""; + }); + // ### Content Helper // // *Usage example:* diff --git a/core/server/models/post.js b/core/server/models/post.js index 93bc354fc6..afe5877c80 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -123,6 +123,22 @@ Post = GhostBookshelf.Model.extend({ }, { + // #### findAll + // Extends base model findAll to eager-fetch author and user relationships. + findAll: function (options) { + options = options || {}; + options.withRelated = [ "author", "user" ]; + return GhostBookshelf.Model.findAll.call(this, options); + }, + + // #### findOne + // Extends base model findOne to eager-fetch author and user relationships. + findOne: function (args, options) { + options = options || {}; + options.withRelated = [ "author", "user" ]; + return GhostBookshelf.Model.findOne.call(this, args, options); + }, + // #### findPage // Find results by page - returns an object containing the // information about the request (page, limit), along with the @@ -173,6 +189,8 @@ Post = GhostBookshelf.Model.extend({ postCollection.query('where', opts.where); } + opts.withRelated = [ "author", "user" ]; + // Set the limit & offset for the query, fetching // with the opts (to specify any eager relations, etc.) // Omitting the `page`, `limit`, `where` just to be sure diff --git a/core/test/unit/api_posts_spec.js b/core/test/unit/api_posts_spec.js index 4633b2986c..191ef7365a 100644 --- a/core/test/unit/api_posts_spec.js +++ b/core/test/unit/api_posts_spec.js @@ -8,7 +8,8 @@ var _ = require("underscore"), describe('Post Model', function () { - var PostModel = Models.Post; + var PostModel = Models.Post, + UserModel = Models.User; beforeEach(function (done) { helpers.resetData().then(function () { @@ -46,6 +47,65 @@ describe('Post Model', function () { }).then(null, done); }); + it('can findAll, returning author and user data', function (done) { + var firstPost, + userData = { + password: 'testpass1', + email_address: "test@test1.com", + full_name: "Mr Biscuits" + }; + + helpers.resetData().then(function () { + UserModel.add(userData).then(function (createdUser) { + + PostModel.findAll({}).then(function (results) { + should.exist(results); + results.length.should.be.above(0); + firstPost = results.models[0].toJSON(); + + firstPost.author.should.be.a("object"); + firstPost.user.should.be.a("object"); + firstPost.author.full_name.should.equal("Mr Biscuits"); + firstPost.user.full_name.should.equal("Mr Biscuits"); + + return true; + + }).then(null, done); + + done(); + }).then(null, done); + }); + }); + + it('can findOne, returning author and user data', function (done) { + var firstPost, + userData = { + password: 'testpass1', + email_address: "test@test1.com", + full_name: "Mr Biscuits" + }; + + helpers.resetData().then(function () { + UserModel.add(userData).then(function (createdUser) { + + PostModel.findOne({}).then(function (result) { + should.exist(result); + firstPost = result.toJSON(); + + firstPost.author.should.be.a("object"); + firstPost.user.should.be.a("object"); + firstPost.author.full_name.should.equal("Mr Biscuits"); + firstPost.user.full_name.should.equal("Mr Biscuits"); + + return true; + + }).then(null, done); + + done(); + }).then(null, done); + }); + }); + it('can edit', function (done) { var firstPost; diff --git a/core/test/unit/frontend_helpers_index_spec.js b/core/test/unit/frontend_helpers_index_spec.js index 471563ed3e..784e555f9a 100644 --- a/core/test/unit/frontend_helpers_index_spec.js +++ b/core/test/unit/frontend_helpers_index_spec.js @@ -60,6 +60,28 @@ describe('Core Helpers', function () { rendered.string.should.equal("
Hello Wo
"); }); }); + + describe('Author Helper', function () { + + it('has loaded author helper', function () { + should.exist(handlebars.helpers.author); + }); + + it("Returns the full name of the author from the context",function() { + var content = {"author":{"full_name":"abc123"}}, + result = handlebars.helpers.author.call(content); + + String(result).should.equal("abc123"); + }); + + it("Returns a blank string where author data is missing",function() { + var content = {"author":null}, + result = handlebars.helpers.author.call(content); + + String(result).should.equal(""); + }); + + }); describe('Excerpt Helper', function () {