2013-06-25 15:43:15 +04:00
|
|
|
var Settings,
|
|
|
|
GhostBookshelf = require('./base'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
errors = require('../errorHandling'),
|
|
|
|
when = require('when');
|
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({
|
|
|
|
tableName: 'settings',
|
|
|
|
hasTimestamps: true,
|
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
|
|
|
type: 'general'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
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) {
|
|
|
|
return setting.set('value', item.value).save();
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
|
|
|
Settings: Settings
|
|
|
|
};
|