Skip to content

Commit

Permalink
added url handlebars helper
Browse files Browse the repository at this point in the history
closes TryGhost#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.
  • Loading branch information
cobbspur committed Aug 26, 2013
1 parent 88a6541 commit 5c12c78
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/server/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
//
Expand Down
4 changes: 4 additions & 0 deletions core/server/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
};
19 changes: 19 additions & 0 deletions core/test/unit/frontend_helpers_index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 5c12c78

Please sign in to comment.