Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Represent channels as class instances (#9209)
refs #9192, refs #5091 - Using a class allows for easy shared logic - Loading is designed to work from config right now, but could be DB driven, etc - Provided configuration can be simplified and extended in the constructor / class methods - Update tests, move custom assertions to utils
- Loading branch information
Showing
8 changed files
with
257 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'), | ||
config = require('../../config'), | ||
defaultPostOptions = {}; | ||
|
||
class Channel { | ||
constructor(name, options) { | ||
// Set the name | ||
this.name = name; | ||
|
||
// Store the originally passed in options | ||
this._origOptions = _.cloneDeep(options) || {}; | ||
|
||
// Setup our route | ||
// @TODO should a channel have a route as part of the object? Or should this live elsewhere? | ||
this.route = this._origOptions.route ? this.translateRoute(this._origOptions.route) : '/'; | ||
|
||
// Define context as name, plus any additional contexts, and don't allow duplicates | ||
this.context = _.union([this.name], this._origOptions.context); | ||
|
||
// DATA options | ||
// Options for fetching related posts | ||
this.postOptions = _.defaults({}, defaultPostOptions, this._origOptions.postOptions); | ||
|
||
// RESOURCE!!! | ||
// @TODO figure out a better way to declare relation to resource | ||
if (this._origOptions.data) { | ||
this.data = this._origOptions.data; | ||
} | ||
|
||
// Template options | ||
// @TODO fix these HORRIBLE names | ||
this.slugTemplate = !!this._origOptions.slugTemplate; | ||
if (this._origOptions.frontPageTemplate) { | ||
this.frontPageTemplate = this._origOptions.frontPageTemplate; | ||
} | ||
|
||
if (this._origOptions.editRedirect) { | ||
this.editRedirect = this._origOptions.editRedirect; | ||
} | ||
} | ||
|
||
get isPaged() { | ||
return _.has(this._origOptions, 'paged') ? this._origOptions.paged : true; | ||
} | ||
|
||
get hasRSS() { | ||
return _.has(this._origOptions, 'rss') ? this._origOptions.rss : true; | ||
} | ||
|
||
translateRoute(route) { | ||
// @TODO find this a more general / global home, as part of the Router system, | ||
// so that ALL routes that get registered WITH Ghost can do this | ||
return route.replace(/:t_([a-zA-Z]+)/, function (fullMatch, keyword) { | ||
return config.get('routeKeywords')[keyword]; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Channel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.