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

Make channel config dynamic

refs #5091, #6166

- fetch channel config via an internal function
- prevents channel config from being statically cached at runtime
- means that labs & other settings can be used to change these values
This commit is contained in:
Hannah Wolfe 2015-12-07 20:06:35 +00:00
parent 22e3c54ed1
commit a956d595f2
3 changed files with 67 additions and 61 deletions

View file

@ -1,42 +1,47 @@
var config = require('../../config'), var _ = require('lodash'),
defaults; config = require('../../config'),
getConfig;
defaults = { getConfig = function getConfig(name) {
index: { var defaults = {
name: 'index', index: {
route: '/', name: 'index',
frontPageTemplate: 'home' route: '/',
}, frontPageTemplate: 'home'
tag: {
name: 'tag',
route: '/' + config.routeKeywords.tag + '/:slug/',
postOptions: {
filter: 'tags:%s'
}, },
data: { tag: {
tag: { name: 'tag',
type: 'read', route: '/' + config.routeKeywords.tag + '/:slug/',
resource: 'tags', postOptions: {
options: {slug: '%s'} filter: 'tags:%s'
} },
data: {
tag: {
type: 'read',
resource: 'tags',
options: {slug: '%s'}
}
},
slugTemplate: true
}, },
slugTemplate: true author: {
}, name: 'author',
author: { route: '/' + config.routeKeywords.author + '/:slug/',
name: 'author', postOptions: {
route: '/' + config.routeKeywords.author + '/:slug/', filter: 'author:%s'
postOptions: { },
filter: 'author:%s' data: {
}, author: {
data: { type: 'read',
author: { resource: 'users',
type: 'read', options: {slug: '%s'}
resource: 'users', }
options: {slug: '%s'} },
} slugTemplate: true
}, }
slugTemplate: true };
}
return _.cloneDeep(defaults[name]);
}; };
module.exports = defaults; module.exports = getConfig;

View file

@ -41,10 +41,11 @@ function renderPost(req, res) {
}; };
} }
function renderChannel(channelOpts) { function renderChannel(name) {
return function renderChannel(req, res, next) { return function renderChannel(req, res, next) {
// Parse the parameters we need from the URL // Parse the parameters we need from the URL
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1, var channelOpts = channelConfig(name),
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
slugParam = req.params.slug ? safeString(req.params.slug) : undefined; slugParam = req.params.slug ? safeString(req.params.slug) : undefined;
// Ensure we at least have an empty object for postOptions // Ensure we at least have an empty object for postOptions
@ -102,20 +103,20 @@ function renderChannel(channelOpts) {
} }
frontendControllers = { frontendControllers = {
index: renderChannel(_.cloneDeep(channelConfig.index)), index: renderChannel('index'),
tag: renderChannel(_.cloneDeep(channelConfig.tag)), tag: renderChannel('tag'),
author: renderChannel(_.cloneDeep(channelConfig.author)), author: renderChannel('author'),
rss: function (req, res, next) { rss: function (req, res, next) {
// Temporary hack, channels will allow us to resolve this better eventually // Temporary hack, channels will allow us to resolve this better eventually
var tagPattern = new RegExp('^\\/' + config.routeKeywords.tag + '\\/.+'), var tagPattern = new RegExp('^\\/' + config.routeKeywords.tag + '\\/.+'),
authorPattern = new RegExp('^\\/' + config.routeKeywords.author + '\\/.+'); authorPattern = new RegExp('^\\/' + config.routeKeywords.author + '\\/.+');
if (tagPattern.test(res.locals.relativeUrl)) { if (tagPattern.test(res.locals.relativeUrl)) {
req.channelConfig = _.cloneDeep(channelConfig.tag); req.channelConfig = channelConfig('tag');
} else if (authorPattern.test(res.locals.relativeUrl)) { } else if (authorPattern.test(res.locals.relativeUrl)) {
req.channelConfig = _.cloneDeep(channelConfig.author); req.channelConfig = channelConfig('author');
} else { } else {
req.channelConfig = _.cloneDeep(channelConfig.index); req.channelConfig = channelConfig('index');
} }
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;

View file

@ -104,7 +104,7 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -146,7 +146,7 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -175,7 +175,7 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -209,7 +209,7 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -241,7 +241,7 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -297,7 +297,7 @@ describe('RSS', function () {
done(); done();
}; };
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)); rss(req, res, failTest(done));
}); });
@ -306,7 +306,7 @@ 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.tag; req.channelConfig = channelConfig('tag');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
// test // test
@ -325,7 +325,7 @@ 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.author; req.channelConfig = channelConfig('author');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
// test // test
@ -369,7 +369,7 @@ describe('RSS', function () {
results: {posts: [], meta: {pagination: {pages: 1}}} results: {posts: [], meta: {pagination: {pages: 1}}}
}); });
}); });
req.channelConfig = channelConfig.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
function secondCall() { function secondCall() {
@ -420,7 +420,7 @@ describe('RSS', function () {
it('Redirects to /rss/ if page number is -1', function () { it('Redirects to /rss/ if page number is -1', function () {
req = {params: {page: -1}, route: {path: '/rss/:page/'}}; req = {params: {page: -1}, 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, null); rss(req, res, null);
@ -433,7 +433,7 @@ describe('RSS', function () {
it('Redirects to /rss/ if page number is 0', function () { it('Redirects to /rss/ if page number is 0', function () {
req = {params: {page: 0}, route: {path: '/rss/:page/'}}; req = {params: {page: 0}, 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, null); rss(req, res, null);
@ -446,7 +446,7 @@ describe('RSS', function () {
it('Redirects to /rss/ if page number is 1', function () { it('Redirects to /rss/ if page number is 1', function () {
req = {params: {page: 1}, route: {path: '/rss/:page/'}}; req = {params: {page: 1}, 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, null); rss(req, res, null);
@ -461,7 +461,7 @@ describe('RSS', function () {
req = {params: {page: 0}, route: {path: '/rss/:page/'}}; req = {params: {page: 0}, 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, null); rss(req, res, null);
@ -476,7 +476,7 @@ describe('RSS', function () {
req = {params: {page: 1}, route: {path: '/rss/:page/'}}; req = {params: {page: 1}, 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, null); rss(req, res, null);
@ -491,7 +491,7 @@ 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)).then(function () { rss(req, res, failTest(done)).then(function () {
@ -507,7 +507,7 @@ 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.index; req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true; req.channelConfig.isRSS = true;
rss(req, res, failTest(done)).then(function () { rss(req, res, failTest(done)).then(function () {