Merge pull request #438 from cgiffard/hbs-helper-author

Template Helpers: Added author hbs helper
This commit is contained in:
Hannah Wolfe 2013-08-24 07:45:36 -07:00
commit 60c7643a58
4 changed files with 114 additions and 1 deletions

View File

@ -29,6 +29,19 @@ coreHelpers = function (ghost) {
return date; 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 // ### Content Helper
// //
// *Usage example:* // *Usage example:*

View File

@ -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 // #### findPage
// Find results by page - returns an object containing the // Find results by page - returns an object containing the
// information about the request (page, limit), along with the // information about the request (page, limit), along with the
@ -173,6 +189,8 @@ Post = GhostBookshelf.Model.extend({
postCollection.query('where', opts.where); postCollection.query('where', opts.where);
} }
opts.withRelated = [ "author", "user" ];
// Set the limit & offset for the query, fetching // Set the limit & offset for the query, fetching
// with the opts (to specify any eager relations, etc.) // with the opts (to specify any eager relations, etc.)
// Omitting the `page`, `limit`, `where` just to be sure // Omitting the `page`, `limit`, `where` just to be sure

View File

@ -8,7 +8,8 @@ var _ = require("underscore"),
describe('Post Model', function () { describe('Post Model', function () {
var PostModel = Models.Post; var PostModel = Models.Post,
UserModel = Models.User;
beforeEach(function (done) { beforeEach(function (done) {
helpers.resetData().then(function () { helpers.resetData().then(function () {
@ -46,6 +47,65 @@ describe('Post Model', function () {
}).then(null, done); }).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) { it('can edit', function (done) {
var firstPost; var firstPost;

View File

@ -60,6 +60,28 @@ describe('Core Helpers', function () {
rendered.string.should.equal("<p>Hello <strong>Wo</strong></p>"); rendered.string.should.equal("<p>Hello <strong>Wo</strong></p>");
}); });
}); });
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 () { describe('Excerpt Helper', function () {