Skip to content

Commit

Permalink
remove ghost.settings and ghost.notifications
Browse files Browse the repository at this point in the history
covers 90% of TryGhost#755
- moved ghost.settings to api.settings
- moved ghost.notifications to api.notifications
- split up api/index.js to notifications.js, posts.js, settings.js,
tags.js and users.js
- added instance.globals as temp workaround for blogglobals (Known
issue: blog title and blog description are updated after restart only)
- added webroot to config() to remove `var root = ...`
- changed `e` and `url` helper to async
- updated tests
  • Loading branch information
sebgie committed Dec 6, 2013
1 parent 1b4b228 commit 078f464
Show file tree
Hide file tree
Showing 24 changed files with 963 additions and 875 deletions.
112 changes: 27 additions & 85 deletions core/ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var config = require('./server/config'),
models = require('./server/models'),
permissions = require('./server/permissions'),
uuid = require('node-uuid'),
api = require('./server/api'),

// Variables
Ghost,
Expand All @@ -27,39 +28,36 @@ Ghost = function () {
if (!instance) {
instance = this;

// Holds the persistent notifications
instance.notifications = [];
instance.globals = {};

// Holds the dbhash (mainly used for cookie secret)
instance.dbHash = undefined;

_.extend(instance, {
// there's no management here to be sure this has loaded
settings: function (key) {
if (key) {
return instance.settingsCache[key].value;
}
return instance.settingsCache;
},
dataProvider: models,
blogGlobals: function () {
var localPath = url.parse(config().url).path;

// Remove trailing slash
if (localPath !== '/') {
localPath = localPath.replace(/\/$/, '');
}

/* this is a bit of a hack until we have a better way to combine settings and config
* this data is what becomes globally available to themes */
return {
url: config().url.replace(/\/$/, ''),
path: localPath,
title: instance.settings('title'),
description: instance.settings('description'),
logo: instance.settings('logo'),
cover: instance.settings('cover')
};
return instance.globals;
},
getGlobals: function () {
return when.all([
api.settings.read('title'),
api.settings.read('description'),
api.settings.read('logo'),
api.settings.read('cover')
]).then(function (globals) {

instance.globals.path = config.paths().path;

instance.globals.url = config().url;
instance.globals.title = globals[0].value;
instance.globals.description = globals[1].value;
instance.globals.logo = globals[2] ? globals[2].value : '';
instance.globals.cover = globals[3] ? globals[3].value : '';
return;
});
}
});
}
Expand All @@ -82,13 +80,12 @@ Ghost.prototype.init = function () {
'See <a href="http://docs.ghost.org/">http://docs.ghost.org</a> for instructions.'
];

self.notifications.push({
return api.notifications.add({
type: 'info',
message: firstRunMessage.join(' '),
status: 'persistent',
id: 'ghost-first-run'
});
return when.resolve();
}

function initDbHashAndFirstRun() {
Expand All @@ -112,16 +109,17 @@ Ghost.prototype.init = function () {
// Initialise the models
self.dataProvider.init(),
// Calculate paths
config.paths.updatePaths()
config.paths.updatePaths(config().url)
).then(function () {
// Populate any missing default settings
return models.Settings.populateDefaults();
}).then(function () {
// Initialize the settings cache
return self.updateSettingsCache();
return api.init();
}).then(function () {

return self.getGlobals();
}).then(function () {
// Update path to activeTheme
config.paths.setActiveTheme(self);
return when.join(
// Check for or initialise a dbHash.
initDbHashAndFirstRun(),
Expand All @@ -131,60 +129,4 @@ Ghost.prototype.init = function () {
}).otherwise(errors.logAndThrowError);
};

// Maintain the internal cache of the settings object
Ghost.prototype.updateSettingsCache = function (settings) {
var self = this;

settings = settings || {};

if (!_.isEmpty(settings)) {
_.map(settings, function (setting, key) {
self.settingsCache[key].value = setting.value;
});
} else {
// TODO: this should use api.browse
return when(models.Settings.findAll()).then(function (result) {
return when(self.readSettingsResult(result)).then(function (s) {
self.settingsCache = s;
});
});
}
};

Ghost.prototype.readSettingsResult = function (result) {
var settings = {};
return when(_.map(result.models, function (member) {
if (!settings.hasOwnProperty(member.attributes.key)) {
var val = {};
val.value = member.attributes.value;
val.type = member.attributes.type;
settings[member.attributes.key] = val;
}
})).then(function () {
return when(config.paths().availableThemes).then(function (themes) {
var themeKeys = Object.keys(themes),
res = [],
i,
item;
for (i = 0; i < themeKeys.length; i += 1) {
//do not include hidden files
if (themeKeys[i].indexOf('.') !== 0) {
item = {};
item.name = themeKeys[i];
//data about files currently not used
//item.details = themes[themeKeys[i]];
if (themeKeys[i] === settings.activeTheme.value) {
item.active = true;
}
res.push(item);
}
}
settings.availableThemes = {};
settings.availableThemes.value = res;
settings.availableThemes.type = 'theme';
return settings;
});
});
};

module.exports = Ghost;
88 changes: 46 additions & 42 deletions core/server/api/db.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Ghost = require('../../ghost'),
dataExport = require('../data/export'),
var dataExport = require('../data/export'),
dataImport = require('../data/import'),
api = require('../api'),
fs = require('fs-extra'),
Expand All @@ -8,8 +7,8 @@ var Ghost = require('../../ghost'),
nodefn = require('when/node/function'),
_ = require('underscore'),
schema = require('../data/schema'),
config = require('../config'),

ghost = new Ghost(),
db;

db = {
Expand All @@ -27,15 +26,16 @@ db = {
res.download(exportedFilePath, 'GhostData.json');
}).otherwise(function (error) {
// Notify of an error if it occurs
var notification = {
type: 'error',
message: error.message || error,
status: 'persistent',
id: 'per-' + (ghost.notifications.length + 1)
};

return api.notifications.add(notification).then(function () {
res.redirect('/ghost/debug/');
return api.notification.browse().then(function (notifications) {
var notification = {
type: 'error',
message: error.message || error,
status: 'persistent',
id: 'per-' + (notifications.length + 1)
};
return api.notifications.add(notification).then(function () {
res.redirect(config.paths().webroot + '/ghost/debug/');
});
});
});
},
Expand All @@ -51,15 +51,16 @@ db = {
* - If the size is 0
* - If the name doesn't have json in it
*/
notification = {
type: 'error',
message: "Must select a .json file to import",
status: 'persistent',
id: 'per-' + (ghost.notifications.length + 1)
};

return api.notifications.add(notification).then(function () {
res.redirect('/ghost/debug/');
return api.notification.browse().then(function (notifications) {
notification = {
type: 'error',
message: "Must select a .json file to import",
status: 'persistent',
id: 'per-' + (notifications.length + 1)
};
return api.notifications.add(notification).then(function () {
res.redirect(config.paths().webroot + '/ghost/debug/');
});
});
}

Expand Down Expand Up @@ -125,32 +126,35 @@ db = {
});
})
.then(function importSuccess() {
notification = {
type: 'success',
message: "Data imported. Log in with the user details you imported",
status: 'persistent',
id: 'per-' + (ghost.notifications.length + 1)
};
return api.notification.browse().then(function (notifications) {
notification = {
type: 'success',
message: "Data imported. Log in with the user details you imported",
status: 'persistent',
id: 'per-' + (notifications.length + 1)
};

return api.notifications.add(notification).then(function () {
req.session.destroy();
res.set({
"X-Cache-Invalidate": "/*"
return api.notifications.add(notification).then(function () {
req.session.destroy();
res.set({
"X-Cache-Invalidate": "/*"
});
res.redirect(config.paths().webroot + '/ghost/signin/');
});
res.redirect('/ghost/signin/');
});

}, function importFailure(error) {
// Notify of an error if it occurs
notification = {
type: 'error',
message: error.message || error,
status: 'persistent',
id: 'per-' + (ghost.notifications.length + 1)
};
return api.notification.browse().then(function (notifications) {
// Notify of an error if it occurs
notification = {
type: 'error',
message: error.message || error,
status: 'persistent',
id: 'per-' + (notifications.length + 1)
};

return api.notifications.add(notification).then(function () {
res.redirect('/ghost/debug/');
return api.notifications.add(notification).then(function () {
res.redirect(config.paths().webroot + '/ghost/debug/');
});
});
});
}
Expand Down
Loading

0 comments on commit 078f464

Please sign in to comment.