Skip to content

Commit

Permalink
Add: Descriptive document title (#49)
Browse files Browse the repository at this point in the history
* Add: Descriptive document title

* Add: Document title for list pages (exceptions to using Get Page Data module)

* Add: Tests for GetPageData module documentTitle property

* Include: Theatre name in document title for Production instances
  • Loading branch information
andygout authored Jan 29, 2017
1 parent fa4249f commit da03de5
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/controllers/productions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ exports.list = (req, res, next) => {
return Production.list()
.then(productions => {

const page = { title: 'Productions' };
const page = { documentTitle: ' | Home', title: 'Productions' };

res.render('productions/list', Object.assign({ page, productions, alert: getAlert(req) }));

Expand Down
4 changes: 3 additions & 1 deletion server/controllers/theatres.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ exports.list = (req, res, next) => {
return Theatre.list()
.then(theatres => {

const page = { title: 'Theatres' };
pageTitle = 'Theatres';

const page = { documentTitle: ` | ${pageTitle}`, title: pageTitle };

res.render('theatres/list', Object.assign({ page, theatres, alert: getAlert(req) }));

Expand Down
23 changes: 22 additions & 1 deletion server/lib/get-page-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ const checkIfCreateAction = action => action === 'create';

const getPageTitleText = (model, instance) => instance.pageTitleText || instance[modelNamingPropMap[model]];

const getDocumentTitle = (instance, action, model, title) => {

let documentTitle = title;

if (action === 'update') documentTitle = `Edit: ${documentTitle}`;

if (action !== 'create') {

if (model === 'production') documentTitle += ` (${instance.theatre.name})`;

documentTitle += ` (${model})`;

}

return ` | ${documentTitle}`;

};

const getAlertText = (model, instance, action) => {

const instanceText = instance[modelNamingPropMap[model]];
Expand All @@ -31,8 +49,11 @@ module.exports = function (instance, action) {

const isCreateAction = checkIfCreateAction(action);

const title = isCreateAction ? `New ${model}` : getPageTitleText(model, instance);

return {
title: isCreateAction ? `New ${model}` : getPageTitleText(model, instance),
documentTitle: getDocumentTitle(instance, action, model, title),
title,
modelName: model,
modelRoute: `${model}s`,
instanceRoute: `/${model}s/${instance.id}`,
Expand Down
2 changes: 1 addition & 1 deletion spec/server/controllers/productions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ describe('Production controller', () => {
expect(response._getRenderView()).to.eq('productions/list');
expect(response._getRenderData()).to.deep.eq(
Object.assign({
page: { title: 'Productions' },
page: { documentTitle: ' | Home', title: 'Productions' },
productions: dataListFixture,
alert: alertFixture
})
Expand Down
2 changes: 1 addition & 1 deletion spec/server/controllers/theatres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe('Theatre controller', () => {
expect(response._getRenderView()).to.eq('theatres/list');
expect(response._getRenderData()).to.deep.eq(
Object.assign({
page: { title: 'Theatres' },
page: { documentTitle: ' | Theatres', title: 'Theatres' },
theatres: dataListFixture,
alert: alertFixture
})
Expand Down
66 changes: 64 additions & 2 deletions spec/server/lib/get-page-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const stubs = {
const resetStubs = () => {

stubs.Production = sinon.createStubInstance(Production);
stubs.Theatre = sinon.createStubInstance(Theatre);

stubs.Production.theatre = { name: 'Almeida Theatre' };
stubs.Production.errors = {};

stubs.Theatre = sinon.createStubInstance(Theatre);
stubs.Theatre.errors = {};

};
Expand All @@ -29,6 +30,67 @@ beforeEach(function () {

describe('Get Page Data module', () => {

describe('documentTitle property', () => {

context('create action', () => {

it('will read \' | New <model>\'', () => {
const pageData = subject(stubs.Production, 'create');
expect(pageData.documentTitle).to.eq(' | New production');
});

});

context('update action', () => {

context('production instance', () => {

it('will read \' | Edit: <instance> (<theatre name>) (<model>)\'', () => {
stubs.Production.title = 'Hamlet';
const pageData = subject(stubs.Production, 'update');
expect(pageData.documentTitle).to.eq(' | Edit: Hamlet (Almeida Theatre) (production)');
});

});

context('theatre instance', () => {

it('will read \' | Edit: <instance> (<model>)\'', () => {
stubs.Theatre.name = 'Almeida Theatre';
const pageData = subject(stubs.Theatre, 'update');
expect(pageData.documentTitle).to.eq(' | Edit: Almeida Theatre (theatre)');
});

});

});

context('show action', () => {

context('production instance', () => {

it('will read \' | <instance> (<theatre name>) (<model>)\'', () => {
stubs.Production.title = 'Hamlet';
const pageData = subject(stubs.Production, 'show');
expect(pageData.documentTitle).to.eq(' | Hamlet (Almeida Theatre) (production)');
});

});

context('theatre instance', () => {

it('will read \' | <instance> (<model>)\'', () => {
stubs.Theatre.name = 'Almeida Theatre';
const pageData = subject(stubs.Theatre, 'show');
expect(pageData.documentTitle).to.eq(' | Almeida Theatre (theatre)');
});

});

});

});

describe('title property', () => {

context('create action', () => {
Expand Down
2 changes: 1 addition & 1 deletion views/partials/head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<head>
<title>TheatreBase</title>
<title>TheatreBase {{page.documentTitle}}</title>
<link rel="stylesheet" href="/main.css">
</head>

0 comments on commit da03de5

Please sign in to comment.