0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

🎨 make settings cache available (#7692)

* 🎨  settingsCache is available

- do not destroy the object reference
- added TODO to reconsider the config values for theme
- get one or all cached settings

* 🚨  remove api.init

- this functiion has just wrapped a function to update the settings cache
- if we have multiple tasks todo later, we can re-add
- but for now: this is way easier to read
- adapt test

* 🎨  tests
This commit is contained in:
Katharina Irrgang 2016-11-08 14:37:19 +01:00 committed by Hannah Wolfe
parent 68af2145a1
commit 3aac3ef6de
5 changed files with 36 additions and 21 deletions

View file

@ -33,17 +33,7 @@ var _ = require('lodash'),
cacheInvalidationHeader,
locationHeader,
contentDispositionHeaderExport,
contentDispositionHeaderSubscribers,
init;
/**
* ### Init
* Initialise the API - populate the settings cache
* @return {Promise(Settings)} Resolves to Settings Collection
*/
init = function init() {
return settings.updateSettingsCache();
};
contentDispositionHeaderSubscribers;
function isActiveThemeOverride(method, endpoint, result) {
return method === 'POST' && endpoint === 'themes' && result.themes && result.themes[0] && result.themes[0].active === true;
@ -274,8 +264,6 @@ http = function http(apiMethod) {
* ## Public API
*/
module.exports = {
// Extras
init: init,
http: http,
// API Endpoints
configuration: configuration,

View file

@ -26,7 +26,6 @@ var _ = require('lodash'),
/**
* ## Cache
* Holds cached settings
* @private
* @type {{}}
*/
settingsCache = {};
@ -52,6 +51,9 @@ updateConfigCache = function () {
}
}
// @TODO: why are we putting the settings cache values into config?we could access the cache directly
// @TODO: plus: why do we assign the values to the prefix "theme"?
// @TODO: might be related to https://github.com/TryGhost/Ghost/issues/7488
config.set('theme:title', (settingsCache.title && settingsCache.title.value) || '');
config.set('theme:description', (settingsCache.description && settingsCache.description.value) || '');
config.set('theme:logo', (settingsCache.logo && settingsCache.logo.value) || '');
@ -92,10 +94,9 @@ updateSettingsCache = function (settings, options) {
return dataProvider.Settings.findAll(options)
.then(function (result) {
settingsCache = readSettingsResult(result.models);
// keep reference and update all keys
_.extend(settingsCache, readSettingsResult(result.models));
updateConfigCache();
return settingsCache;
});
};
@ -428,4 +429,21 @@ settings = {
};
module.exports = settings;
/**
* synchronous function to get cached settings value
* returns the value of the settings entry
*/
module.exports.getSettingSync = function getSettingSync(key) {
return settingsCache[key] && settingsCache[key].value;
};
/**
* synchronous function to get all cached settings values
* returns everything for now
*/
module.exports.getSettingsSync = function getSettingsSync() {
return settingsCache;
};
module.exports.updateSettingsCache = updateSettingsCache;

View file

@ -88,10 +88,10 @@ function init(options) {
return models.Settings.populateDefaults();
}).then(function () {
debug('Models & database done');
// Initialize the settings cache
return api.init();
return api.settings.updateSettingsCache();
}).then(function () {
debug('API done');
debug('Update settings cache done');
// Initialize the permissions actions and objects
// NOTE: Must be done before initDbHashAndFirstRun calls
return permissions.init();

View file

@ -115,12 +115,21 @@ describe('Settings API', function () {
});
it('can edit', function () {
var testReference = SettingsAPI.getSettingsSync();
// see default-settings.json
SettingsAPI.getSettingSync('title').should.eql('Ghost');
testReference.title.value.should.eql('Ghost');
return callApiWithContext(defaultContext, 'edit', {settings: [{key: 'title', value: 'UpdatedGhost'}]}, {})
.then(function (response) {
should.exist(response);
testUtils.API.checkResponse(response, 'settings');
response.settings.length.should.equal(1);
testUtils.API.checkResponse(response.settings[0], 'setting');
SettingsAPI.getSettingSync('title').should.eql('UpdatedGhost');
testReference.title.value.should.eql('UpdatedGhost');
});
});

View file

@ -33,7 +33,7 @@ describe('server bootstrap', function () {
populateStub = sandbox.stub(migration, 'populate').returns(Promise.resolve());
sandbox.stub(models.Settings, 'populateDefaults').returns(Promise.resolve());
sandbox.stub(permissions, 'init').returns(Promise.resolve());
sandbox.stub(api, 'init').returns(Promise.resolve());
sandbox.stub(api.settings, 'updateSettingsCache').returns(Promise.resolve());
sandbox.stub(apps, 'init').returns(Promise.resolve());
sandbox.stub(slack, 'listen').returns(Promise.resolve());
sandbox.stub(xmlrpc, 'listen').returns(Promise.resolve());