0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Misc helper updates & fixes

refs #5205

- Ensure that pages are treated the same as posts in meta_title, add test
- Added a private-template body class for the private context, add test
- Added a missing test to is_spec.js
This commit is contained in:
Hannah Wolfe 2015-05-14 11:31:43 +01:00
parent 1f0fb3c5f3
commit d11a91e75c
5 changed files with 122 additions and 46 deletions

View file

@ -14,44 +14,45 @@ var hbs = require('express-hbs'),
template = require('./template'),
body_class;
body_class = function () {
body_class = function (options) {
var classes = [],
context = options.data.root.context,
post = this.post,
tags = this.post && this.post.tags ? this.post.tags : this.tags || [],
page = this.post && this.post.page ? this.post.page : this.page || false;
if (this.tag !== undefined) {
classes.push('tag-template');
classes.push('tag-' + this.tag.slug);
}
if (this.author !== undefined) {
classes.push('author-template');
classes.push('author-' + this.author.slug);
}
if (_.isString(this.relativeUrl) && this.relativeUrl.match(/\/(page\/\d)/)) {
classes.push('paged');
if (post) {
// To be removed from pages by #2597 when we're ready to deprecate this
classes.push('archive-template');
} else if (!this.relativeUrl || this.relativeUrl === '/' || this.relativeUrl === '') {
classes.push('home-template');
} else if (post) {
// To be removed from pages by #2597 when we're ready to deprecate this
// i.e. this should be if (post && !page) { ... }
// i.e. this should be if (_.contains(context, 'post') && post) { ... }
classes.push('post-template');
}
if (page) {
if (_.contains(context, 'home')) {
classes.push('home-template');
} else if (_.contains(context, 'page') && page) {
classes.push('page-template');
// To be removed by #2597 when we're ready to deprecate this
classes.push('page');
} else if (_.contains(context, 'tag') && this.tag) {
classes.push('tag-template');
classes.push('tag-' + this.tag.slug);
} else if (_.contains(context, 'author') && this.author) {
classes.push('author-template');
classes.push('author-' + this.author.slug);
} else if (_.contains(context, 'private')) {
classes.push('private-template');
}
if (tags) {
classes = classes.concat(tags.map(function (tag) { return 'tag-' + tag.slug; }));
}
if (_.contains(context, 'paged')) {
classes.push('paged');
// To be removed from pages by #2597 when we're ready to deprecate this
classes.push('archive-template');
}
return api.settings.read({context: {internal: true}, key: 'activeTheme'}).then(function (response) {
var activeTheme = response.settings[0],
paths = config.paths.availableThemes[activeTheme.value],

View file

@ -31,7 +31,7 @@ meta_title = function (options) {
title = this.author.name + pageString + ' - ' + blog.title;
} else if (_.contains(context, 'tag') && this.tag) {
title = this.tag.meta_title || this.tag.name + pageString + ' - ' + blog.title;
} else if (_.contains(context, 'post') && this.post) {
} else if ((_.contains(context, 'post') || _.contains(context, 'page')) && this.post) {
title = this.post.meta_title || this.post.title;
} else {
title = blog.title + pageString;

View file

@ -46,7 +46,7 @@ describe('{{body_class}} helper', function () {
});
it('can render class string', function (done) {
helpers.body_class.call({}).then(function (rendered) {
helpers.body_class.call({}, {data: {root: {context: ['home']}}}).then(function (rendered) {
should.exist(rendered);
rendered.string.should.equal('home-template');
@ -57,15 +57,53 @@ describe('{{body_class}} helper', function () {
it('can render class string for context', function (done) {
Promise.all([
helpers.body_class.call({relativeUrl: '/'}),
helpers.body_class.call({relativeUrl: '/a-post-title', post: {}}),
helpers.body_class.call({relativeUrl: '/page/4'}),
helpers.body_class.call({relativeUrl: '/tag/foo', tag: {slug: 'foo'}}),
helpers.body_class.call({relativeUrl: '/tag/foo/page/2', tag: {slug: 'foo'}}),
helpers.body_class.call({relativeUrl: '/author/bar', author: {slug: 'bar'}}),
helpers.body_class.call({relativeUrl: '/author/bar/page/2', author: {slug: 'bar'}})
// Standard home page
helpers.body_class.call(
{relativeUrl: '/'},
{data: {root: {context: ['home', 'index']}}}
),
// A post
helpers.body_class.call(
{relativeUrl: '/a-post-title', post: {}},
{data: {root: {context: ['post']}}}
),
// Paginated index
helpers.body_class.call(
{relativeUrl: '/page/4'},
{data: {root: {context: ['index', 'paged']}}}
),
// Tag page
helpers.body_class.call(
{relativeUrl: '/tag/foo', tag: {slug: 'foo'}},
{data: {root: {context: ['tag']}}}
),
// Paginated tag page
helpers.body_class.call(
{relativeUrl: '/tag/foo/page/2', tag: {slug: 'foo'}},
{data: {root: {context: ['tag', 'paged']}}}
),
// Author page
helpers.body_class.call(
{relativeUrl: '/author/bar', author: {slug: 'bar'}},
{data: {root: {context: ['author']}}}
),
// Paginated author page
helpers.body_class.call(
{relativeUrl: '/author/bar/page/2', author: {slug: 'bar'}},
{data: {root: {context: ['author', 'paged']}}}
),
// Private route for password protection
helpers.body_class.call(
{relativeUrl: '/private/'},
{data: {root: {context: ['private']}}}
),
// Post with tags
helpers.body_class.call(
{relativeUrl: '/my-awesome-post/', post: {tags: [{slug: 'foo'}, {slug: 'bar'}]}},
{data: {root: {context: ['post']}}}
)
]).then(function (rendered) {
rendered.length.should.equal(7);
rendered.length.should.equal(9);
should.exist(rendered[0]);
should.exist(rendered[1]);
@ -74,6 +112,8 @@ describe('{{body_class}} helper', function () {
should.exist(rendered[4]);
should.exist(rendered[5]);
should.exist(rendered[6]);
should.exist(rendered[7]);
should.exist(rendered[8]);
rendered[0].string.should.equal('home-template');
rendered[1].string.should.equal('post-template');
@ -82,34 +122,40 @@ describe('{{body_class}} helper', function () {
rendered[4].string.should.equal('tag-template tag-foo paged archive-template');
rendered[5].string.should.equal('author-template author-bar');
rendered[6].string.should.equal('author-template author-bar paged archive-template');
rendered[7].string.should.equal('private-template');
rendered[8].string.should.equal('post-template tag-foo tag-bar');
done();
}).catch(done);
});
it('can render class for static page', function (done) {
helpers.body_class.call({
relativeUrl: '/',
post: {
page: true
}
}).then(function (rendered) {
helpers.body_class.call(
{
relativeUrl: '/about',
post: {
page: true
}
},
{data: {root: {context: ['page']}}}
).then(function (rendered) {
should.exist(rendered);
rendered.string.should.equal('home-template page-template page');
rendered.string.should.equal('post-template page-template page');
done();
}).catch(done);
});
it('can render class for static page with custom template', function (done) {
helpers.body_class.call({
relativeUrl: '/about',
post: {
page: true,
slug: 'about'
}
}).then(function (rendered) {
helpers.body_class.call(
{
relativeUrl: '/about',
post: {
page: true,
slug: 'about'
}
},
{data: {root: {context: ['page']}}}).then(function (rendered) {
should.exist(rendered);
rendered.string.should.equal('post-template page-template page page-about page-template-about');

View file

@ -7,7 +7,8 @@ var should = require('should'),
// Stuff we are testing
handlebars = hbs.handlebars,
helpers = require('../../../server/helpers');
helpers = require('../../../server/helpers'),
errors = require('../../../server/errors');
describe('{{#is}} helper', function () {
before(function () {
@ -60,4 +61,20 @@ describe('{{#is}} helper', function () {
fn.called.should.be.false;
inverse.called.should.be.true;
});
it('should log warning with no args', function () {
var fn = sinon.spy(),
inverse = sinon.spy(),
logWarn = sinon.stub(errors, 'logWarn');
helpers.is.call(
{},
undefined,
{fn: fn, inverse: inverse, data: {root: {context: ['index', 'home']}}}
);
logWarn.called.should.be.true;
fn.called.should.be.false;
inverse.called.should.be.false;
});
});

View file

@ -74,6 +74,18 @@ describe('{{meta_title}} helper', function () {
}).catch(done);
});
it('returns correct title for a page with meta_title set', function (done) {
helpers.meta_title.call(
{post: {title: 'About Page', meta_title: 'All about my awesomeness', page: true}},
{data: {root: {context: ['page']}}}
).then(function (rendered) {
should.exist(rendered);
String(rendered).should.equal('All about my awesomeness');
done();
}).catch(done);
});
it('returns correct title for a tag page', function (done) {
var tag = {relativeUrl: '/tag/rasper-red', tag: {name: 'Rasper Red'}};
helpers.meta_title.call(