2013-06-25 06:43:15 -05:00
|
|
|
var Settings,
|
|
|
|
GhostBookshelf = require('./base'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
errors = require('../errorHandling'),
|
2013-09-01 20:49:08 -05:00
|
|
|
when = require('when'),
|
|
|
|
defaultSettings = require('../data/default-settings.json');
|
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
|
|
|
|
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
|
|
|
hasTimestamps: true,
|
2013-08-25 05:49:31 -05:00
|
|
|
|
|
|
|
permittedAttributes: ['id', 'uuid', 'key', 'value', 'type', 'created_at', 'created_by', 'updated_at', 'update_by'],
|
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
|
|
|
type: 'general'
|
|
|
|
};
|
2013-08-25 05:49:31 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.on('saving', this.saving, this);
|
|
|
|
this.on('saving', this.validate, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function () {
|
|
|
|
// TODO: validate value, check type is one of the allowed values etc
|
|
|
|
GhostBookshelf.validator.check(this.get('key'), "Setting key cannot be blank").notEmpty();
|
|
|
|
GhostBookshelf.validator.check(this.get('type'), "Setting type cannot be blank").notEmpty();
|
|
|
|
},
|
|
|
|
|
|
|
|
saving: function () {
|
|
|
|
// Deal with the related data here
|
|
|
|
|
2013-09-01 20:49:08 -05:00
|
|
|
// Remove any properties which don't belong on the model
|
2013-08-25 05:49:31 -05:00
|
|
|
this.attributes = this.pick(this.permittedAttributes);
|
2013-06-25 06:43:15 -05:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
read: function (_key) {
|
|
|
|
// Allow for just passing the key instead of attributes
|
|
|
|
if (!_.isObject(_key)) {
|
|
|
|
_key = { key: _key };
|
2013-06-15 09:10:30 -05:00
|
|
|
}
|
2013-06-25 06:43:15 -05:00
|
|
|
return GhostBookshelf.Model.read.call(this, _key);
|
|
|
|
},
|
2013-06-08 00:03:55 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
edit: function (_data) {
|
|
|
|
var settings = this;
|
|
|
|
if (!Array.isArray(_data)) {
|
|
|
|
_data = [_data];
|
2013-06-08 00:03:55 -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(); }
|
|
|
|
return settings.forge({ key: item.key }).fetch().then(function (setting) {
|
2013-09-01 20:49:08 -05:00
|
|
|
if (setting) {
|
|
|
|
return setting.set('value', item.value).save();
|
|
|
|
}
|
|
|
|
return settings.forge({ key: item.key, value: item.value }).save();
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
defaultSettings.forEach(function (defaultSetting) {
|
|
|
|
var isMissingFromDB = usedKeys.indexOf(defaultSetting.key) === -1;
|
|
|
|
if (isMissingFromDB) {
|
|
|
|
insertOperations.push(Settings.forge(defaultSetting).save());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
};
|