added url handlebars helper

closes #528

- adds method (isPost)to models index.js that returns true if content, content_raw, title and slug are valid properties
- adds url helper which checks context is post using  isPost method
- adds unit test to check a url is prefixed with  /
-adds unit test which checks for empty string if either of the 4 properties above are not present.
This commit is contained in:
cobbspur 2013-08-26 20:44:19 +01:00
parent 88a6541ab6
commit 5c12c78d00
3 changed files with 31 additions and 0 deletions

View File

@ -4,8 +4,10 @@ var _ = require('underscore'),
when = require('when'),
hbs = require('express-hbs'),
errors = require('../errorHandling'),
models = require('../models'),
coreHelpers;
coreHelpers = function (ghost) {
var navHelper,
paginationHelper;
@ -29,6 +31,12 @@ coreHelpers = function (ghost) {
return date;
});
ghost.registerThemeHelper('url', function (context, options) {
if (models.isPost(this)) {
return "/" + this.slug;
}
return '';
});
// ### Author Helper
//

View File

@ -13,5 +13,9 @@ module.exports = {
return migrations.reset().then(function () {
return migrations.init();
});
},
isPost: function (jsonData) {
return jsonData.hasOwnProperty("content") && jsonData.hasOwnProperty("content_raw")
&& jsonData.hasOwnProperty("title") && jsonData.hasOwnProperty("slug");
}
};

View File

@ -201,6 +201,25 @@ describe('Core Helpers', function () {
});
});
describe('url Helper', function () {
it('has loaded url helper', function () {
should.exist(handlebars.helpers.url);
});
it('if context is post, returns a the slug with a prefix slash', function () {
var rendered = handlebars.helpers.url.call({content: 'content', content_raw: "ff", title: "title", slug: "slug"});
should.exist(rendered);
rendered.should.equal('/slug');
});
it('it should return empty string if not a post', function () {
handlebars.helpers.url.call({content_raw: "ff", title: "title", slug: "slug"}).should.equal('');
handlebars.helpers.url.call({content: 'content', title: "title", slug: "slug"}).should.equal('');
handlebars.helpers.url.call({content: 'content', content_raw: "ff", slug: "slug"}).should.equal('');
handlebars.helpers.url.call({content: 'content', content_raw: "ff", title: "title"}).should.equal('');
});
});
describe('Navigation Helper', function () {
it('has loaded nav helper', function () {