Renamed single / post -> entry

refs #9192

- an entry is a post or a page, represented by a post model
This commit is contained in:
Hannah Wolfe 2017-11-08 10:00:25 +00:00
parent 187c38991e
commit f0f0735437
11 changed files with 99 additions and 99 deletions

View File

@ -2,12 +2,13 @@ var utils = require('../utils'),
filters = require('../filters'),
handleError = require('./frontend/error'),
postLookup = require('./frontend/post-lookup'),
renderPost = require('./frontend/render-post'),
renderEntry = require('./frontend/render-entry'),
setRequestIsSecure = require('./frontend/secure');
// This here is a controller.
// It renders entries = individual posts or pages
// The "route" is handled in site/routes.js
module.exports = function singleController(req, res, next) {
module.exports = function entryController(req, res, next) {
// Query database to find post
return postLookup(req.path).then(function then(lookup) {
// Format data 1
@ -35,6 +36,6 @@ module.exports = function singleController(req, res, next) {
setRequestIsSecure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(renderPost(req, res));
.then(renderEntry(req, res));
}).catch(handleError(next));
};

View File

@ -27,7 +27,7 @@ function formatPageResponse(result) {
}
/**
* similar to formatPageResponse, but for single post pages
* similar to formatPageResponse, but for entries (post or page)
* @return {Object} containing page variables
*/
function formatResponse(post) {
@ -38,5 +38,5 @@ function formatResponse(post) {
module.exports = {
channel: formatPageResponse,
single: formatResponse
entry: formatResponse
};

View File

@ -0,0 +1,28 @@
var debug = require('ghost-ignition').debug('channels:render-post'),
templates = require('./templates'),
formatResponse = require('./format-response'),
setResponseContext = require('./context');
/*
* Sets the response context around an entry (post or page)
* and renders it with the correct template.
* Used by post preview and entry methods.
* Returns a function that takes the entry to be rendered.
*/
module.exports = function renderEntry(req, res) {
debug('renderEntry called');
return function renderEntry(entry) {
// Renderer begin
// Format data 2 - 1 is in preview/entry
var response = formatResponse.entry(entry);
// Context
setResponseContext(req, res, response);
// Template
res.template = templates.entry(entry);
// Render Call
debug('Rendering view: ' + res.template);
res.render(res.template, response);
};
};

View File

@ -1,29 +0,0 @@
var debug = require('ghost-ignition').debug('channels:render-post'),
templates = require('./templates'),
formatResponse = require('./format-response'),
setResponseContext = require('./context');
/*
* Sets the response context around a post and renders it
* with the current theme's post view. Used by post preview
* and single post methods.
* Returns a function that takes the post to be rendered.
*/
module.exports = function renderPost(req, res) {
debug('renderPost called');
return function renderPost(post) {
// Renderer begin
// Format data 2 - 1 is in preview/single
var response = formatResponse.single(post);
// Context
setResponseContext(req, res, response);
// Template
res.template = templates.single(post);
// Render Call
debug('Rendering view: ' + res.template);
res.render(res.template, response);
};
};

View File

@ -62,7 +62,7 @@ function getChannelTemplateHierarchy(channelOpts) {
}
/**
* ## Get Single Template Hierarchy
* ## Get Entry Template Hierarchy
*
* Fetch the ordered list of templates that can be used to render this request.
* 'post' is the default / fallback
@ -72,7 +72,7 @@ function getChannelTemplateHierarchy(channelOpts) {
* @param {Object} postObject
* @returns {String[]}
*/
function getSingleTemplateHierarchy(postObject) {
function getEntryTemplateHierarchy(postObject) {
var templateList = ['post'],
slugTemplate = 'post-' + postObject.slug;
@ -121,8 +121,8 @@ function pickTemplate(templateList, fallback) {
return template;
}
function getTemplateForSingle(postObject) {
var templateList = getSingleTemplateHierarchy(postObject),
function getTemplateForEntry(postObject) {
var templateList = getEntryTemplateHierarchy(postObject),
fallback = templateList[templateList.length - 1];
return pickTemplate(templateList, fallback);
}
@ -141,7 +141,7 @@ function getTemplateForError(statusCode) {
module.exports = {
channel: getTemplateForChannel,
single: getTemplateForSingle,
entry: getTemplateForEntry,
error: getTemplateForError,
pickTemplate: pickTemplate
};

View File

@ -1,4 +1,4 @@
module.exports = {
preview: require('./preview'),
single: require('./single')
entry: require('./entry')
};

View File

@ -2,7 +2,7 @@ var api = require('../api'),
utils = require('../utils'),
filters = require('../filters'),
handleError = require('./frontend/error'),
renderPost = require('./frontend/render-post'),
renderEntry = require('./frontend/render-entry'),
setRequestIsSecure = require('./frontend/secure');
// This here is a controller.
@ -37,6 +37,6 @@ module.exports = function previewController(req, res, next) {
setRequestIsSecure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(renderPost(req, res));
.then(renderEntry(req, res));
}).catch(handleError(next));
};

View File

@ -18,8 +18,8 @@ module.exports = function siteRouter() {
// Apps - register sub-router
router.use(apps.router);
// Default - register single controller as route
router.get('*', controllers.single);
// Default - register entry controller as route
router.get('*', controllers.entry);
return router;
};

View File

@ -65,7 +65,7 @@ describe('Controllers', function () {
};
}
describe('single', function () {
describe('entry', function () {
var req, res, mockPosts = [{
posts: [{
status: 'published',
@ -150,7 +150,7 @@ describe('Controllers', function () {
};
mockPosts[2].posts[0].url = req.path;
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('it will use page.hbs if it exists and no page-slug template is present', function (done) {
@ -165,7 +165,7 @@ describe('Controllers', function () {
};
mockPosts[2].posts[0].url = req.path;
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('defaults to post.hbs without a page.hbs or page-slug template', function (done) {
@ -181,7 +181,7 @@ describe('Controllers', function () {
};
mockPosts[2].posts[0].url = req.path;
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
});
@ -195,13 +195,13 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT render static page via /YYY/MM/DD/:slug', function (done) {
req.path = '/' + ['2012/12/30', mockPosts[0].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -210,7 +210,7 @@ describe('Controllers', function () {
it('will NOT render static page via /:author/:slug', function (done) {
req.path = '/' + ['test', mockPosts[0].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -224,13 +224,13 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT redirect static page to admin edit page via /YYYY/MM/DD/:slug/edit', function (done) {
req.path = '/' + ['2012/12/30', mockPosts[0].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -240,7 +240,7 @@ describe('Controllers', function () {
it('will NOT redirect static page to admin edit page via /:author/:slug/edit', function (done) {
req.path = '/' + ['test', mockPosts[0].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -262,14 +262,14 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT render static page via /YYYY/MM/DD/:slug', function (done) {
req.path = '/' + ['2012/12/30', mockPosts[0].posts[0].slug].join('/') + '/';
res.render = sinon.spy();
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -284,7 +284,7 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT redirect static page to admin edit page via /YYYY/MM/DD/:slug/edit', function (done) {
@ -292,7 +292,7 @@ describe('Controllers', function () {
res.render = sinon.spy();
res.redirect = sinon.spy();
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -317,13 +317,13 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT render post via /YYYY/MM/DD/:slug', function (done) {
req.path = '/' + ['2012/12/30', mockPosts[1].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -332,7 +332,7 @@ describe('Controllers', function () {
it('will NOT render post via /:author/:slug', function (done) {
req.path = '/' + ['test', mockPosts[1].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -347,13 +347,13 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT redirect post to admin edit page via /YYYY/MM/DD/:slug/edit', function (done) {
req.path = '/' + ['2012/12/30', mockPosts[1].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -363,7 +363,7 @@ describe('Controllers', function () {
it('will NOT redirect post to admin edit page via /:author/:slug/edit', function (done) {
req.path = '/' + ['test', mockPosts[1].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -373,7 +373,7 @@ describe('Controllers', function () {
it('should call next if post is not found', function (done) {
req.path = '/unknown/';
controllers.single(req, res, function (err) {
controllers.entry(req, res, function (err) {
if (err) {
return done(err);
}
@ -406,13 +406,13 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT render post via /:slug/', function (done) {
req.path = '/' + mockPosts[1].posts[0].slug + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -421,7 +421,7 @@ describe('Controllers', function () {
it('will NOT render post via /:author/:slug/', function (done) {
req.path = '/' + ['test', mockPosts[1].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -438,13 +438,13 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT redirect post to admin edit page via /:slug/edit/', function (done) {
req.path = '/' + [mockPosts[1].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -454,7 +454,7 @@ describe('Controllers', function () {
it('will NOT redirect post to admin edit page via /:author/:slug/edit/', function (done) {
req.path = '/' + ['test', mockPosts[1].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -480,14 +480,14 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT render post via /YYYY/MM/DD/:slug/', function (done) {
var date = moment(mockPosts[1].posts[0].published_at).format('YYYY/MM/DD');
req.path = '/' + [date, mockPosts[1].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -496,7 +496,7 @@ describe('Controllers', function () {
it('will NOT render post via /:author/:slug/ when author does not match post author', function (done) {
req.path = '/' + ['test-2', mockPosts[1].posts[0].slug].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -505,7 +505,7 @@ describe('Controllers', function () {
it('will NOT render post via /:slug/', function (done) {
req.path = '/' + mockPosts[1].posts[0].slug + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -521,14 +521,14 @@ describe('Controllers', function () {
done();
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT redirect post to admin edit page via /YYYY/MM/DD/:slug/edit/', function (done) {
var date = moment(mockPosts[1].posts[0].published_at).format('YYYY/MM/DD');
req.path = '/' + [date, mockPosts[1].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -538,7 +538,7 @@ describe('Controllers', function () {
it('will NOT redirect post to admin edit page /:slug/edit/', function (done) {
req.path = '/' + [mockPosts[1].posts[0].slug, 'edit'].join('/') + '/';
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -570,7 +570,7 @@ describe('Controllers', function () {
}
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT render post via /YYYY/MM/DD/:slug/', function (done) {
@ -583,7 +583,7 @@ describe('Controllers', function () {
render: sinon.spy()
};
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -599,7 +599,7 @@ describe('Controllers', function () {
render: sinon.spy()
};
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -614,7 +614,7 @@ describe('Controllers', function () {
render: sinon.spy()
};
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
done();
});
@ -636,7 +636,7 @@ describe('Controllers', function () {
}
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
it('will NOT redirect post to admin edit page /:slug/edit/', function (done) {
@ -649,7 +649,7 @@ describe('Controllers', function () {
redirect: sinon.spy()
};
controllers.single(req, res, function () {
controllers.entry(req, res, function () {
res.render.called.should.be.false();
res.redirect.called.should.be.false();
done();
@ -681,7 +681,7 @@ describe('Controllers', function () {
}
};
controllers.single(req, res, failTest(done));
controllers.entry(req, res, failTest(done));
});
});
});

View File

@ -4,12 +4,12 @@ var should = require('should'), // jshint ignore:line
formatResponse = require('../../../../server/controllers/frontend/format-response');
describe('formatResponse', function () {
describe('single', function () {
describe('entry', function () {
it('should return the post object wrapped in a post key', function () {
var formatted,
postObject = {slug: 'sluggy-thing'};
formatted = formatResponse.single(postObject);
formatted = formatResponse.entry(postObject);
formatted.should.be.an.Object().with.property('post');
formatted.post.should.eql(postObject);

View File

@ -107,7 +107,7 @@ describe('templates', function () {
});
});
describe('single', function () {
describe('entry', function () {
beforeEach(function () {
hasTemplateStub = sandbox.stub().returns(false);
@ -127,7 +127,7 @@ describe('templates', function () {
});
it('post without custom slug template', function () {
var view = templates.single({
var view = templates.entry({
page: 0,
slug: 'test-post'
});
@ -137,7 +137,7 @@ describe('templates', function () {
it('post with custom slug template', function () {
hasTemplateStub.withArgs('post-welcome-to-ghost').returns(true);
var view = templates.single({
var view = templates.entry({
page: 0,
slug: 'welcome-to-ghost'
});
@ -146,7 +146,7 @@ describe('templates', function () {
});
it('page without custom slug template', function () {
var view = templates.single({
var view = templates.entry({
page: 1,
slug: 'contact'
});
@ -155,7 +155,7 @@ describe('templates', function () {
});
it('page with custom slug template', function () {
var view = templates.single({
var view = templates.entry({
page: 1,
slug: 'about'
});
@ -166,7 +166,7 @@ describe('templates', function () {
it('post with custom template', function () {
hasTemplateStub.withArgs('custom-about').returns(true);
var view = templates.single({
var view = templates.entry({
page: 0,
custom_template: 'custom-about'
});
@ -177,7 +177,7 @@ describe('templates', function () {
it('page with custom template', function () {
hasTemplateStub.withArgs('custom-about').returns(true);
var view = templates.single({
var view = templates.entry({
page: 1,
custom_template: 'custom-about'
});
@ -188,7 +188,7 @@ describe('templates', function () {
it('post with custom template configured, but the template is missing', function () {
hasTemplateStub.withArgs('custom-about').returns(false);
var view = templates.single({
var view = templates.entry({
page: 0,
custom_template: 'custom-about'
});
@ -199,7 +199,7 @@ describe('templates', function () {
it('page with custom template configured, but the template is missing', function () {
hasTemplateStub.withArgs('custom-about').returns(false);
var view = templates.single({
var view = templates.entry({
page: 1,
custom_template: 'custom-about'
});
@ -211,7 +211,7 @@ describe('templates', function () {
hasTemplateStub.withArgs('custom-about').returns(true);
hasTemplateStub.withArgs('post-about').returns(true);
var view = templates.single({
var view = templates.entry({
page: 0,
slug: 'about',
custom_template: 'custom-about'
@ -224,7 +224,7 @@ describe('templates', function () {
hasTemplateStub.withArgs('custom-about').returns(false);
hasTemplateStub.withArgs('post-about').returns(false);
var view = templates.single({
var view = templates.entry({
page: 0,
slug: 'about',
custom_template: 'custom-about'
@ -237,7 +237,7 @@ describe('templates', function () {
it('will fall back to post even if no index.hbs', function () {
hasTemplateStub.returns(false);
var view = templates.single({page: 1});
var view = templates.entry({page: 1});
should.exist(view);
view.should.eql('post');
});