0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

🦄 Channels: Stored config in res.locals not req (#8884)

refs #5091

- This tiny refactor opens the door for using channel config inside of helpers
- This means that ghost_head, and the next_post/prev_post helpers can be context aware
This commit is contained in:
Hannah Wolfe 2017-08-14 04:21:24 +01:00 committed by Aileen Nowak
parent 137b8bf973
commit bd41dba35b
6 changed files with 45 additions and 37 deletions

View file

@ -35,7 +35,7 @@ function handlePageParam(req, res, next, page) {
rssRouter = function rssRouter(channelConfig) { rssRouter = function rssRouter(channelConfig) {
function rssConfigMiddleware(req, res, next) { function rssConfigMiddleware(req, res, next) {
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
next(); next();
} }
@ -57,7 +57,7 @@ rssRouter = function rssRouter(channelConfig) {
channelRouter = function router() { channelRouter = function router() {
function channelConfigMiddleware(channel) { function channelConfigMiddleware(channel) {
return function doChannelConfig(req, res, next) { return function doChannelConfig(req, res, next) {
req.channelConfig = _.cloneDeep(channel); res.locals.channel = _.cloneDeep(channel);
next(); next();
}; };
} }

View file

@ -14,7 +14,7 @@ var debug = require('debug')('ghost:channels:render'),
function renderChannel(req, res, next) { function renderChannel(req, res, next) {
debug('renderChannel called'); debug('renderChannel called');
// Parse the parameters we need from the URL // Parse the parameters we need from the URL
var channelOpts = req.channelConfig, var channelOpts = res.locals.channel,
pageParam = req.params.page !== undefined ? req.params.page : 1, pageParam = req.params.page !== undefined ? req.params.page : 1,
slugParam = req.params.slug ? safeString(req.params.slug) : undefined; slugParam = req.params.slug ? safeString(req.params.slug) : undefined;

View file

@ -161,16 +161,17 @@ generate = function generate(req, res, next) {
// Initialize RSS // Initialize RSS
var pageParam = req.params.page !== undefined ? req.params.page : 1, var pageParam = req.params.page !== undefined ? req.params.page : 1,
slugParam = req.params.slug, slugParam = req.params.slug,
baseUrl = getBaseUrl(req, slugParam); baseUrl = getBaseUrl(req, slugParam),
channelConfig = res.locals.channel;
// Ensure we at least have an empty object for postOptions // Ensure we at least have an empty object for postOptions
req.channelConfig.postOptions = req.channelConfig.postOptions || {}; channelConfig.postOptions = channelConfig.postOptions || {};
// Set page on postOptions for the query made later // Set page on postOptions for the query made later
req.channelConfig.postOptions.page = pageParam; channelConfig.postOptions.page = pageParam;
req.channelConfig.slugParam = slugParam; channelConfig.slugParam = slugParam;
return getData(req.channelConfig).then(function then(data) { return getData(channelConfig).then(function then(data) {
var maxPage = data.results.meta.pagination.pages; var maxPage = data.results.meta.pagination.pages;
// If page is greater than number of pages we have, redirect to last page // If page is greater than number of pages we have, redirect to last page

View file

@ -29,7 +29,8 @@ describe('Channels', function () {
// Run a test which should result in a render // Run a test which should result in a render
function testChannelRender(props, assertions, done) { function testChannelRender(props, assertions, done) {
res = { res = {
redirect: sandbox.spy() redirect: sandbox.spy(),
locals: {}
}; };
res.render = function (view) { res.render = function (view) {
@ -47,7 +48,8 @@ describe('Channels', function () {
function testChannelRedirect(props, assertions, done) { function testChannelRedirect(props, assertions, done) {
res = { res = {
render: sandbox.spy(), render: sandbox.spy(),
set: sandbox.spy() set: sandbox.spy(),
locals: {}
}; };
res.redirect = function (status, path) { res.redirect = function (status, path) {
@ -66,7 +68,8 @@ describe('Channels', function () {
res = { res = {
redirect: sandbox.spy(), redirect: sandbox.spy(),
render: sandbox.spy(), render: sandbox.spy(),
set: sandbox.spy() set: sandbox.spy(),
locals: {}
}; };
_.extend(req, props); _.extend(req, props);

View file

@ -24,9 +24,13 @@ describe('Render Channel', function () {
describe('Tag config', function () { describe('Tag config', function () {
var req = { var req = {
channelConfig: channelConfig('tag'),
params: {} params: {}
}, },
res = {
locals: {
channel: channelConfig('tag')
}
},
promise = { promise = {
then: function () { then: function () {
return { return {
@ -45,7 +49,7 @@ describe('Render Channel', function () {
return promise; return promise;
}); });
renderChannel(req); renderChannel(req, res);
}); });
}); });
}); });

View file

@ -97,8 +97,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -140,8 +140,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -176,8 +176,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -204,8 +204,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -238,8 +238,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -270,8 +270,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
}); });
@ -327,8 +327,8 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -336,8 +336,8 @@ describe('RSS', function () {
// setup // setup
req.originalUrl = '/tag/magic/rss/'; req.originalUrl = '/tag/magic/rss/';
req.params.slug = 'magic'; req.params.slug = 'magic';
req.channelConfig = channelConfig.get('tag'); res.locals.channel = channelConfig.get('tag');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
// test // test
res.send = function send(xmlData) { res.send = function send(xmlData) {
@ -358,8 +358,8 @@ describe('RSS', function () {
it('should process the data correctly for an author feed', function (done) { it('should process the data correctly for an author feed', function (done) {
req.originalUrl = '/author/joe/rss/'; req.originalUrl = '/author/joe/rss/';
req.params.slug = 'joe'; req.params.slug = 'joe';
req.channelConfig = channelConfig.get('author'); res.locals.channel = channelConfig.get('author');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
// test // test
res.send = function send(xmlData) { res.send = function send(xmlData) {
@ -401,8 +401,8 @@ describe('RSS', function () {
results: {posts: [], meta: {pagination: {pages: 1}}} results: {posts: [], meta: {pagination: {pages: 1}}}
}); });
}); });
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
function secondCall() { function secondCall() {
res.send = function sendFirst(data) { res.send = function sendFirst(data) {
@ -454,8 +454,8 @@ describe('RSS', function () {
req = {params: {page: 4}, route: {path: '/rss/:page/'}}; req = {params: {page: 4}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page); req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, function (err) { rss(req, res, function (err) {
should.exist(err); should.exist(err);
@ -471,8 +471,8 @@ describe('RSS', function () {
req = {params: {page: 4}, route: {path: '/rss/:page/'}}; req = {params: {page: 4}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page); req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.get('index'); res.locals.channel = channelConfig.get('index');
req.channelConfig.isRSS = true; res.locals.channel.isRSS = true;
rss(req, res, function (err) { rss(req, res, function (err) {
should.exist(err); should.exist(err);