Skip to content

Commit

Permalink
Merge pull request #6182 from ErisDS/channel-config-dynamic
Browse files Browse the repository at this point in the history
Make channel config dynamic
  • Loading branch information
sebgie committed Dec 7, 2015
2 parents adfad93 + a956d59 commit 4524898
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 61 deletions.
79 changes: 42 additions & 37 deletions core/server/controllers/frontend/channel-config.js
@@ -1,42 +1,47 @@
var config = require('../../config'),
defaults;
var _ = require('lodash'),
config = require('../../config'),
getConfig;

defaults = {
index: {
name: 'index',
route: '/',
frontPageTemplate: 'home'
},
tag: {
name: 'tag',
route: '/' + config.routeKeywords.tag + '/:slug/',
postOptions: {
filter: 'tags:%s'
getConfig = function getConfig(name) {
var defaults = {
index: {
name: 'index',
route: '/',
frontPageTemplate: 'home'
},
data: {
tag: {
type: 'read',
resource: 'tags',
options: {slug: '%s'}
}
tag: {
name: 'tag',
route: '/' + config.routeKeywords.tag + '/:slug/',
postOptions: {
filter: 'tags:%s'
},
data: {
tag: {
type: 'read',
resource: 'tags',
options: {slug: '%s'}
}
},
slugTemplate: true
},
slugTemplate: true
},
author: {
name: 'author',
route: '/' + config.routeKeywords.author + '/:slug/',
postOptions: {
filter: 'author:%s'
},
data: {
author: {
type: 'read',
resource: 'users',
options: {slug: '%s'}
}
},
slugTemplate: true
}
author: {
name: 'author',
route: '/' + config.routeKeywords.author + '/:slug/',
postOptions: {
filter: 'author:%s'
},
data: {
author: {
type: 'read',
resource: 'users',
options: {slug: '%s'}
}
},
slugTemplate: true
}
};

return _.cloneDeep(defaults[name]);
};

module.exports = defaults;
module.exports = getConfig;
17 changes: 9 additions & 8 deletions core/server/controllers/frontend/index.js
Expand Up @@ -41,10 +41,11 @@ function renderPost(req, res) {
};
}

function renderChannel(channelOpts) {
function renderChannel(name) {
return function renderChannel(req, res, next) {
// 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;

// Ensure we at least have an empty object for postOptions
Expand Down Expand Up @@ -102,20 +103,20 @@ function renderChannel(channelOpts) {
}

frontendControllers = {
index: renderChannel(_.cloneDeep(channelConfig.index)),
tag: renderChannel(_.cloneDeep(channelConfig.tag)),
author: renderChannel(_.cloneDeep(channelConfig.author)),
index: renderChannel('index'),
tag: renderChannel('tag'),
author: renderChannel('author'),
rss: function (req, res, next) {
// Temporary hack, channels will allow us to resolve this better eventually
var tagPattern = new RegExp('^\\/' + config.routeKeywords.tag + '\\/.+'),
authorPattern = new RegExp('^\\/' + config.routeKeywords.author + '\\/.+');

if (tagPattern.test(res.locals.relativeUrl)) {
req.channelConfig = _.cloneDeep(channelConfig.tag);
req.channelConfig = channelConfig('tag');
} else if (authorPattern.test(res.locals.relativeUrl)) {
req.channelConfig = _.cloneDeep(channelConfig.author);
req.channelConfig = channelConfig('author');
} else {
req.channelConfig = _.cloneDeep(channelConfig.index);
req.channelConfig = channelConfig('index');
}

req.channelConfig.isRSS = true;
Expand Down
32 changes: 16 additions & 16 deletions core/test/unit/rss_spec.js
Expand Up @@ -104,7 +104,7 @@ describe('RSS', function () {
done();
};

req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;
rss(req, res, failTest(done));
});
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('RSS', function () {
done();
};

req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;
rss(req, res, failTest(done));
});
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('RSS', function () {
done();
};

req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;
rss(req, res, failTest(done));
});
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('RSS', function () {
done();
};

req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;
rss(req, res, failTest(done));
});
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('RSS', function () {
done();
};

req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;
rss(req, res, failTest(done));
});
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('RSS', function () {
done();
};

req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;
rss(req, res, failTest(done));
});
Expand All @@ -306,7 +306,7 @@ describe('RSS', function () {
// setup
req.originalUrl = '/tag/magic/rss/';
req.params.slug = 'magic';
req.channelConfig = channelConfig.tag;
req.channelConfig = channelConfig('tag');
req.channelConfig.isRSS = true;

// test
Expand All @@ -325,7 +325,7 @@ describe('RSS', function () {
it('should process the data correctly for an author feed', function (done) {
req.originalUrl = '/author/joe/rss/';
req.params.slug = 'joe';
req.channelConfig = channelConfig.author;
req.channelConfig = channelConfig('author');
req.channelConfig.isRSS = true;

// test
Expand Down Expand Up @@ -369,7 +369,7 @@ describe('RSS', function () {
results: {posts: [], meta: {pagination: {pages: 1}}}
});
});
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

function secondCall() {
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('RSS', function () {
it('Redirects to /rss/ if page number is -1', function () {
req = {params: {page: -1}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, null);
Expand All @@ -433,7 +433,7 @@ describe('RSS', function () {
it('Redirects to /rss/ if page number is 0', function () {
req = {params: {page: 0}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, null);
Expand All @@ -446,7 +446,7 @@ describe('RSS', function () {
it('Redirects to /rss/ if page number is 1', function () {
req = {params: {page: 1}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, null);
Expand All @@ -461,7 +461,7 @@ describe('RSS', function () {

req = {params: {page: 0}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, null);
Expand All @@ -476,7 +476,7 @@ describe('RSS', function () {

req = {params: {page: 1}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, null);
Expand All @@ -491,7 +491,7 @@ describe('RSS', function () {

req = {params: {page: 4}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, failTest(done)).then(function () {
Expand All @@ -507,7 +507,7 @@ describe('RSS', function () {

req = {params: {page: 4}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
req.channelConfig = channelConfig.index;
req.channelConfig = channelConfig('index');
req.channelConfig.isRSS = true;

rss(req, res, failTest(done)).then(function () {
Expand Down

0 comments on commit 4524898

Please sign in to comment.