Skip to content

Commit

Permalink
Moved Channels module -> services
Browse files Browse the repository at this point in the history
refs #9192, refs #5091, refs #9178

- moved channels from controllers to a service
- split out the parent router from the remaining individual router logic
- moved the tests to match
  • Loading branch information
ErisDS committed Nov 8, 2017
1 parent f0f0735 commit 90cfdbe
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 34 deletions.
1 change: 0 additions & 1 deletion core/server/controllers/channels/index.js

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions core/server/services/channels/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.router = require('./parent-router');
File renamed without changes.
14 changes: 14 additions & 0 deletions core/server/services/channels/parent-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var express = require('express'),
channelLoader = require('./loader'),
channelRouter = require('./router');

module.exports = function channelsRouter() {
var channelsRouter = express.Router({mergeParams: true});

channelLoader.list().forEach(function (channel) {
// Mount this channel router on the parent channels router
channelsRouter.use(channel.route, channelRouter(channel));
});

return channelsRouter;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ var express = require('express'),
errors = require('../../errors'),
i18n = require('../../i18n'),
utils = require('../../utils'),
channelLoader = require('./loader'),
channelController = require('../channel'),
rssController = require('../rss'),
channelController = require('../../controllers/channel'),
rssController = require('../../controllers/rss'),
rssRouter,
channelRouter,
channelsRouter;
channelRouter;

function handlePageParam(req, res, next, page) {
var pageRegex = new RegExp('/' + config.get('routeKeywords').page + '/(.*)?/'),
Expand Down Expand Up @@ -90,15 +88,4 @@ channelRouter = function channelRouter(channel) {
return channelRouter;
};

channelsRouter = function channelsRouter() {
var channelsRouter = express.Router({mergeParams: true});

_.each(channelLoader.list(), function (channel) {
// Mount this channel router on the parent channels router
channelsRouter.use(channel.route, channelRouter(channel));
});

return channelsRouter;
};

module.exports = channelsRouter;
module.exports = channelRouter;
8 changes: 4 additions & 4 deletions core/server/site/routes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var express = require('express'),
config = require('../config'),
controllers = require('../controllers'),
channels = require('../controllers/channels'),
apps = require('../services/route').appRouter,
channelService = require('../services/channels/'),
appRouter = require('../services/route').appRouter,
utils = require('../utils');

module.exports = function siteRouter() {
Expand All @@ -13,10 +13,10 @@ module.exports = function siteRouter() {
router.get(utils.url.urlJoin('/', routeKeywords.preview, ':uuid', ':options?'), controllers.preview);

// Channels - register sub-router
router.use(channels.router());
router.use(channelService.router());

// Apps - register sub-router
router.use(apps.router);
router.use(appRouter.router);

// Default - register entry controller as route
router.get('*', controllers.entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var should = require('should'), // jshint ignore:line
_ = require('lodash'),

// Stuff we are testing
channelLoader = require('../../../../server/controllers/channels/loader'),
channels = require('../../../../server/controllers/channels'),
channelLoader = require('../../../../server/services/channels/loader'),
channels = require('../../../../server/services/channels'),
channelUtils = require('../../../utils/channelUtils'),
Channel = channelUtils.Channel,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var should = require('should'), // jshint ignore:line
_ = require('lodash'),
rewire = require('rewire'),
channelUtils = require('../../../utils/channelUtils'),
channelLoader = rewire('../../../../server/controllers/channels/loader');
channelLoader = rewire('../../../../server/services/channels/loader');

describe('Channels', function () {
describe('Loader', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ var should = require('should'),
_ = require('lodash'),

// Stuff we are testing
channels = require('../../../../server/controllers/channels'),
channelService = require('../../../../server/services/channels'),
api = require('../../../../server/api'),
themes = require('../../../../server/themes'),
sandbox = sinon.sandbox.create();

/**
* Note: this tests the following all in one go:
* - Channel Router controllers/channels/router
* - ChannelS Router services/route/channels-router.js
* - Channel Router services/channel/router.js
* - Channel Controller controllers/channel.js
* - Channel Renderer controllers/frontend/render-channel.js
* This is because the refactor is in progress!
*/
describe('Channels', function () {
var channelRouter, req, res, hasTemplateStub, themeConfigStub;
var channelsRouter, req, res, hasTemplateStub, themeConfigStub;

// Initialise 'req' with the bare minimum properties
function setupRequest() {
Expand Down Expand Up @@ -55,7 +56,7 @@ describe('Channels', function () {

_.extend(req, props);

channelRouter(req, res, failTest(done));
channelsRouter(req, res, failTest(done));
}

// Run a test which should result in a redirect
Expand All @@ -81,7 +82,7 @@ describe('Channels', function () {

_.extend(req, props);

channelRouter(req, res, failTest(done));
channelsRouter(req, res, failTest(done));
}

// Run a test which should result in next() being called
Expand All @@ -98,7 +99,7 @@ describe('Channels', function () {

_.extend(req, props);

channelRouter(req, res, function (empty) {
channelsRouter(req, res, function (empty) {
try {
assertions.call(this, empty);
res.redirect.called.should.be.false();
Expand Down Expand Up @@ -141,7 +142,7 @@ describe('Channels', function () {

before(function () {
// We don't overwrite this, so only do it once
channelRouter = channels.router();
channelsRouter = channelService.router();
});

afterEach(function () {
Expand Down
4 changes: 2 additions & 2 deletions core/test/utils/channelUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var _ = require('lodash'),
should = require('should'),
defaultChannels = require('../../server/controllers/channels/config.channels.json'),
Channel = require('../../server/controllers/channels/Channel');
defaultChannels = require('../../server/services/channels/config.channels.json'),
Channel = require('../../server/services/channels/Channel');

// This is a function to get a fake or test channel
// It's currently based on the default config in Ghost itself
Expand Down

0 comments on commit 90cfdbe

Please sign in to comment.