Ghost/core/server/services/settings/index.js
Katharina Irrgang 727ff183f4 Normalised how we require models
refs #9866

- if I want to do a project search and looks for model usages e.g. `models.`, then I won't find these usages
- normalise how we require models -> consistency
2018-09-21 15:16:19 +02:00

95 lines
3.1 KiB
JavaScript

/**
* Settings Lib
* A collection of utilities for handling settings including a cache
*/
const _ = require('lodash'),
debug = require('ghost-ignition').debug('services:settings:index'),
common = require('../../lib/common'),
models = require('../../models'),
SettingsCache = require('./cache'),
SettingsLoader = require('./loader'),
ensureSettingsFiles = require('./ensure-settings');
module.exports = {
init: function init() {
const knownSettings = this.knownSettings();
debug('init settings service for:', knownSettings);
// Make sure that supported settings files are available
// inside of the `content/setting` directory
return ensureSettingsFiles(knownSettings)
.then(() => {
// Update the defaults
return models.Settings.populateDefaults();
})
.then((settingsCollection) => {
// Initialise the cache with the result
// This will bind to events for further updates
SettingsCache.init(settingsCollection);
});
},
/**
* Global place to switch on more available settings.
*/
knownSettings: function knownSettings() {
return ['routes'];
},
/**
* Getter for YAML settings.
* Example: `settings.get('routes').then(...)`
* will return an Object like this:
* {routes: {}, collections: {}, resources: {}}
* @param {String} setting type of supported setting.
* @returns {Object} settingsFile
* @description Returns settings object as defined per YAML files in
* `/content/settings` directory.
*/
get: function get(setting) {
const knownSettings = this.knownSettings();
// CASE: this should be an edge case and only if internal usage of the
// getter is incorrect.
if (!setting || _.indexOf(knownSettings, setting) < 0) {
throw new common.errors.IncorrectUsageError({
message: `Requested setting is not supported: '${setting}'.`,
help: `Please use only the supported settings: ${knownSettings}.`
});
}
return SettingsLoader(setting);
},
/**
* Getter for all YAML settings.
* Example: `settings.getAll().then(...)`
* will return an Object like this (assuming we're supporting `routes`
* and `globals`):
* {
* routes: {
* routes: null,
* collections: { '/': [Object] },
* resources: { tag: '/tag/{slug}/', author: '/author/{slug}/' }
* },
* globals: {
* config: { url: 'testblog.com' }
* }
* }
* @returns {Object} settingsObject
* @description Returns all settings object as defined per YAML files in
* `/content/settings` directory.
*/
getAll: function getAll() {
const knownSettings = this.knownSettings(),
settingsToReturn = {};
_.each(knownSettings, function (setting) {
settingsToReturn[setting] = SettingsLoader(setting);
});
return settingsToReturn;
}
};