2013-06-25 15:43:15 +04:00
|
|
|
var Settings,
|
|
|
|
GhostBookshelf = require('./base'),
|
2013-09-02 05:49:08 +04:00
|
|
|
validator = GhostBookshelf.validator,
|
2013-06-25 15:43:15 +04:00
|
|
|
uuid = require('node-uuid'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
errors = require('../errorHandling'),
|
2013-09-02 05:49:08 +04:00
|
|
|
when = require('when'),
|
2013-09-02 05:49:08 +04:00
|
|
|
defaultSettings;
|
|
|
|
|
|
|
|
// For neatness, the defaults file is split into categories.
|
|
|
|
// It's much easier for us to work with it as a single level
|
|
|
|
// instead of iterating those categories every time
|
|
|
|
function parseDefaultSettings() {
|
|
|
|
var defaultSettingsInCategories = require('../data/default-settings.json'),
|
|
|
|
defaultSettingsFlattened = {};
|
|
|
|
|
|
|
|
|
|
|
|
_.each(defaultSettingsInCategories, function (settings, categoryName) {
|
|
|
|
_.each(settings, function (setting, settingName) {
|
|
|
|
setting.type = categoryName;
|
|
|
|
setting.key = settingName;
|
|
|
|
defaultSettingsFlattened[settingName] = setting;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultSettingsFlattened;
|
|
|
|
}
|
|
|
|
defaultSettings = parseDefaultSettings();
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Each setting is saved as a separate row in the database,
|
|
|
|
// but the overlying API treats them as a single key:value mapping
|
|
|
|
Settings = GhostBookshelf.Model.extend({
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
tableName: 'settings',
|
2013-08-25 14:49:31 +04:00
|
|
|
|
|
|
|
permittedAttributes: ['id', 'uuid', 'key', 'value', 'type', 'created_at', 'created_by', 'updated_at', 'update_by'],
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 22:04:41 +04:00
|
|
|
type: 'core'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-08-25 14:49:31 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
// Validate default settings using the validator module.
|
|
|
|
// Each validation's key is a name and its value is an array of options
|
|
|
|
// Use true (boolean) if options aren't applicable
|
|
|
|
//
|
|
|
|
// eg:
|
|
|
|
// validations: { isUrl: true, len: [20, 40] }
|
|
|
|
//
|
|
|
|
// will validate that a setting's length is a URL between 20 and 40 chars,
|
|
|
|
// available validators: https://github.com/chriso/node-validator#list-of-validation-methods
|
2013-08-25 14:49:31 +04:00
|
|
|
validate: function () {
|
2013-09-02 05:49:08 +04:00
|
|
|
validator.check(this.get('key'), "Setting key cannot be blank").notEmpty();
|
|
|
|
validator.check(this.get('type'), "Setting type cannot be blank").notEmpty();
|
|
|
|
|
|
|
|
var matchingDefault = defaultSettings[this.get('key')];
|
|
|
|
|
|
|
|
if (matchingDefault && matchingDefault.validations) {
|
|
|
|
_.each(matchingDefault.validations, function (validationOptions, validationName) {
|
|
|
|
var validation = validator.check(this.get('value'));
|
|
|
|
|
|
|
|
if (validationOptions === true) {
|
|
|
|
validationOptions = null;
|
|
|
|
}
|
|
|
|
if (typeof validationOptions !== 'array') {
|
|
|
|
validationOptions = [validationOptions];
|
|
|
|
}
|
|
|
|
|
|
|
|
// equivalent of validation.isSomething(option1, option2)
|
|
|
|
validation[validationName].apply(validation, validationOptions);
|
|
|
|
}, this);
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
read: function (_key) {
|
|
|
|
// Allow for just passing the key instead of attributes
|
|
|
|
if (!_.isObject(_key)) {
|
|
|
|
_key = { key: _key };
|
2013-06-15 18:10:30 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
return GhostBookshelf.Model.read.call(this, _key);
|
|
|
|
},
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
edit: function (_data) {
|
|
|
|
var settings = this;
|
|
|
|
if (!Array.isArray(_data)) {
|
|
|
|
_data = [_data];
|
2013-06-08 09:03:55 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
return when.map(_data, function (item) {
|
|
|
|
// Accept an array of models as input
|
|
|
|
if (item.toJSON) { item = item.toJSON(); }
|
|
|
|
return settings.forge({ key: item.key }).fetch().then(function (setting) {
|
2013-09-02 05:49:08 +04:00
|
|
|
if (setting) {
|
|
|
|
return setting.set('value', item.value).save();
|
|
|
|
}
|
|
|
|
return settings.forge({ key: item.key, value: item.value }).save();
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
});
|
2013-09-02 05:49:08 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
populateDefaults: function () {
|
|
|
|
return this.findAll().then(function (allSettings) {
|
|
|
|
var usedKeys = allSettings.models.map(function (setting) { return setting.get('key'); }),
|
|
|
|
insertOperations = [];
|
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
_.each(defaultSettings, function (defaultSetting, defaultSettingKey) {
|
|
|
|
var isMissingFromDB = usedKeys.indexOf(defaultSettingKey) === -1;
|
2013-09-15 15:11:47 +04:00
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
|
|
|
// TODO: remove before release
|
|
|
|
if (defaultSettingKey === 'databaseVersion' && usedKeys.indexOf('currentVersion') !== -1) {
|
|
|
|
isMissingFromDB = false;
|
|
|
|
}
|
2013-09-02 05:49:08 +04:00
|
|
|
if (isMissingFromDB) {
|
2013-09-15 01:39:31 +04:00
|
|
|
defaultSetting.value = defaultSetting.defaultValue;
|
2013-09-02 05:49:08 +04:00
|
|
|
insertOperations.push(Settings.forge(defaultSetting).save());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(insertOperations);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-09-02 05:49:08 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
|
|
|
Settings: Settings
|
2013-09-02 05:49:08 +04:00
|
|
|
};
|