2013-06-25 06:43:15 -05:00
|
|
|
var Settings,
|
2013-09-22 17:20:08 -05:00
|
|
|
ghostBookshelf = require('./base'),
|
2013-09-24 05:46:30 -05:00
|
|
|
uuid = require('node-uuid'),
|
2014-02-05 03:40:30 -05:00
|
|
|
_ = require('lodash'),
|
2013-09-24 05:46:30 -05:00
|
|
|
errors = require('../errorHandling'),
|
|
|
|
when = require('when'),
|
2014-02-19 12:32:23 -05:00
|
|
|
validation = require('../data/validation'),
|
2014-02-19 08:57:26 -05:00
|
|
|
|
2013-09-01 20:49:08 -05: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 00:03:55 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
// Each setting is saved as a separate row in the database,
|
|
|
|
// but the overlying API treats them as a single key:value mapping
|
2013-09-22 17:20:08 -05:00
|
|
|
Settings = ghostBookshelf.Model.extend({
|
2013-08-25 05:49:31 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
tableName: 'settings',
|
2013-08-25 05:49:31 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 13:04:41 -05:00
|
|
|
type: 'core'
|
2013-06-25 06:43:15 -05:00
|
|
|
};
|
2013-08-25 05:49:31 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
validate: function () {
|
2014-05-05 08:51:21 -05:00
|
|
|
var self = this;
|
|
|
|
return when(validation.validateSchema(self.tableName, self.toJSON())).then(function () {
|
|
|
|
return validation.validateSettings(defaultSettings, self);
|
|
|
|
});
|
2013-10-07 12:02:57 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
saving: function () {
|
2014-01-06 15:17:20 -05:00
|
|
|
// disabling sanitization until we can implement a better version
|
|
|
|
// All blog setting keys that need their values to be escaped.
|
|
|
|
// if (this.get('type') === 'blog' && _.contains(['title', 'description', 'email'], this.get('key'))) {
|
|
|
|
// this.set('value', this.sanitize('value'));
|
|
|
|
// }
|
2013-10-07 12:02:57 -05:00
|
|
|
|
2013-09-22 17:20:08 -05:00
|
|
|
return ghostBookshelf.Model.prototype.saving.apply(this, arguments);
|
2013-06-25 06:43:15 -05:00
|
|
|
}
|
2013-10-07 12:02:57 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
}, {
|
2014-05-05 20:45:08 -05:00
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
|
|
*/
|
|
|
|
permittedOptions: function (methodName) {
|
|
|
|
var options = ghostBookshelf.Model.permittedOptions(),
|
|
|
|
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
|
|
validOptions = {
|
|
|
|
add: ['user'],
|
|
|
|
edit: ['user']
|
|
|
|
};
|
|
|
|
|
|
|
|
if (validOptions[methodName]) {
|
|
|
|
options = options.concat(validOptions[methodName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
findOne: function (_key) {
|
2013-06-25 06:43:15 -05:00
|
|
|
// Allow for just passing the key instead of attributes
|
|
|
|
if (!_.isObject(_key)) {
|
|
|
|
_key = { key: _key };
|
2013-06-15 09:10:30 -05:00
|
|
|
}
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return when(ghostBookshelf.Model.findOne.call(this, _key));
|
2013-06-25 06:43:15 -05:00
|
|
|
},
|
2013-06-08 00:03:55 -05:00
|
|
|
|
2014-04-03 08:03:09 -05:00
|
|
|
edit: function (_data, options) {
|
2014-05-05 20:45:08 -05:00
|
|
|
var self = this;
|
|
|
|
options = this.filterOptions(options, 'edit');
|
2014-04-03 08:03:09 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
if (!Array.isArray(_data)) {
|
|
|
|
_data = [_data];
|
2013-06-08 00:03:55 -05:00
|
|
|
}
|
2014-04-03 08:03:09 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
return when.map(_data, function (item) {
|
|
|
|
// Accept an array of models as input
|
|
|
|
if (item.toJSON) { item = item.toJSON(); }
|
2014-04-27 18:28:50 -05:00
|
|
|
if (!(_.isString(item.key) && item.key.length > 0)) {
|
2014-05-05 08:51:21 -05:00
|
|
|
return when.reject({type: 'ValidationError', message: 'Setting key cannot be empty.'});
|
2014-04-27 18:28:50 -05:00
|
|
|
}
|
2014-05-05 20:45:08 -05:00
|
|
|
|
|
|
|
item = self.filterData(item);
|
|
|
|
|
2014-04-03 08:03:09 -05:00
|
|
|
return Settings.forge({ key: item.key }).fetch(options).then(function (setting) {
|
2013-11-20 15:36:02 -05:00
|
|
|
|
2013-09-01 20:49:08 -05:00
|
|
|
if (setting) {
|
2014-04-03 08:03:09 -05:00
|
|
|
return setting.save({value: item.value}, options);
|
2013-09-01 20:49:08 -05:00
|
|
|
}
|
2014-04-03 08:03:09 -05:00
|
|
|
|
2014-05-05 08:51:21 -05:00
|
|
|
return when.reject({type: 'NotFound', message: 'Unable to find setting to update: ' + item.key});
|
2013-09-01 20:49:08 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
});
|
2013-09-01 20:49:08 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
populateDefaults: function () {
|
|
|
|
return this.findAll().then(function (allSettings) {
|
|
|
|
var usedKeys = allSettings.models.map(function (setting) { return setting.get('key'); }),
|
|
|
|
insertOperations = [];
|
|
|
|
|
2013-09-01 20:49:08 -05:00
|
|
|
_.each(defaultSettings, function (defaultSetting, defaultSettingKey) {
|
|
|
|
var isMissingFromDB = usedKeys.indexOf(defaultSettingKey) === -1;
|
2013-09-15 06:11:47 -05:00
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
|
|
|
if (defaultSettingKey === 'databaseVersion' && usedKeys.indexOf('currentVersion') !== -1) {
|
|
|
|
isMissingFromDB = false;
|
|
|
|
}
|
2013-09-01 20:49:08 -05:00
|
|
|
if (isMissingFromDB) {
|
2013-09-14 16:39:31 -05:00
|
|
|
defaultSetting.value = defaultSetting.defaultValue;
|
2014-04-03 08:03:09 -05:00
|
|
|
insertOperations.push(Settings.forge(defaultSetting).save(null, {user: 1}));
|
2013-09-01 20:49:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(insertOperations);
|
|
|
|
});
|
2013-06-25 06:43:15 -05:00
|
|
|
}
|
2013-09-01 20:49:08 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
});
|
2013-06-08 00:03:55 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
module.exports = {
|
|
|
|
Settings: Settings
|
2013-09-01 20:49:08 -05:00
|
|
|
};
|