Changes to Settings Model
- add email default setting to fixture - make settings a single model - create UNIQUE index on setting keys
This commit is contained in:
parent
dc714611a9
commit
c82e5976cc
@ -60,6 +60,13 @@ module.exports = {
|
||||
"created_by": 1,
|
||||
"updated_by": 1,
|
||||
"type": "general"
|
||||
},
|
||||
{
|
||||
"key": "email",
|
||||
"value": "john@onolan.org",
|
||||
"created_by": 1,
|
||||
"updated_by": 1,
|
||||
"type": "general"
|
||||
}
|
||||
],
|
||||
|
||||
|
@ -88,7 +88,7 @@
|
||||
knex.Schema.createTable('settings', function (t) {
|
||||
t.increments().primary();
|
||||
t.string('uuid');
|
||||
t.string('key');
|
||||
t.string('key').unique();
|
||||
t.text('value');
|
||||
t.string('type');
|
||||
t.date('created_at');
|
||||
|
@ -11,7 +11,7 @@
|
||||
User: require('./user').User,
|
||||
Role: require('./role').Role,
|
||||
Permission: require('./permission').Permission,
|
||||
Setting: require('./setting').Setting,
|
||||
Settings: require('./settings').Settings,
|
||||
init: function () {
|
||||
return knex.Schema.hasTable('posts').then(null, function () {
|
||||
// Simple bootstraping of the data model for now.
|
||||
|
42
core/shared/models/settings.js
Normal file
42
core/shared/models/settings.js
Normal file
@ -0,0 +1,42 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var Settings,
|
||||
GhostBookshelf = require('./base'),
|
||||
_ = require('underscore'),
|
||||
when = require('when');
|
||||
|
||||
// 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
|
||||
}, {
|
||||
read: function (_key) {
|
||||
// Allow for just passing the key instead of attributes
|
||||
if (!_.isObject(_key)) {
|
||||
_key = { key: _key };
|
||||
}
|
||||
return GhostBookshelf.Model.read.call(this, _key);
|
||||
},
|
||||
|
||||
edit: function (_data) {
|
||||
var settings = this;
|
||||
if (!Array.isArray(_data)) {
|
||||
_data = [_data];
|
||||
}
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
Settings: Settings
|
||||
};
|
||||
|
||||
}());
|
@ -8,9 +8,9 @@
|
||||
helpers = require('./helpers'),
|
||||
Models = require('../../shared/models');
|
||||
|
||||
describe('Setting Model', function () {
|
||||
describe('Settings Model', function () {
|
||||
|
||||
var SettingModel = Models.Setting;
|
||||
var SettingsModel = Models.Settings;
|
||||
|
||||
beforeEach(function (done) {
|
||||
helpers.resetData().then(function () {
|
||||
@ -19,7 +19,7 @@
|
||||
});
|
||||
|
||||
it('can browse', function (done) {
|
||||
SettingModel.browse().then(function (results) {
|
||||
SettingsModel.browse().then(function (results) {
|
||||
|
||||
should.exist(results);
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
it('can read', function (done) {
|
||||
var firstSetting;
|
||||
|
||||
SettingModel.browse().then(function (results) {
|
||||
SettingsModel.browse().then(function (results) {
|
||||
|
||||
should.exist(results);
|
||||
|
||||
@ -40,7 +40,7 @@
|
||||
|
||||
firstSetting = results.models[0];
|
||||
|
||||
return SettingModel.read(firstSetting.attributes.key);
|
||||
return SettingsModel.read(firstSetting.attributes.key);
|
||||
|
||||
}).then(function (found) {
|
||||
|
||||
@ -54,22 +54,21 @@
|
||||
});
|
||||
|
||||
it('can edit single', function (done) {
|
||||
var firstPost,
|
||||
toEdit = {};
|
||||
var firstSetting;
|
||||
|
||||
SettingModel.browse().then(function (results) {
|
||||
SettingsModel.browse().then(function (results) {
|
||||
|
||||
should.exist(results);
|
||||
|
||||
results.length.should.be.above(0);
|
||||
|
||||
firstPost = results.models[0];
|
||||
firstSetting = results.models[0];
|
||||
|
||||
// The edit method has been modified to take an object of
|
||||
// key/value pairs
|
||||
toEdit[firstPost.attributes.key] = "new value";
|
||||
firstSetting.set('value', 'new value');
|
||||
|
||||
return SettingModel.edit(toEdit);
|
||||
return SettingsModel.edit(firstSetting);
|
||||
|
||||
}).then(function (edited) {
|
||||
|
||||
@ -79,7 +78,7 @@
|
||||
|
||||
edited = edited[0];
|
||||
|
||||
edited.attributes.key.should.equal(firstPost.attributes.key);
|
||||
edited.attributes.key.should.equal(firstSetting.attributes.key);
|
||||
edited.attributes.value.should.equal('new value');
|
||||
|
||||
done();
|
||||
@ -88,26 +87,25 @@
|
||||
});
|
||||
|
||||
it('can edit multiple', function (done) {
|
||||
var firstPost,
|
||||
secondPost,
|
||||
editedPost,
|
||||
toEdit = {};
|
||||
var model1,
|
||||
model2,
|
||||
editedModel;
|
||||
|
||||
SettingModel.browse().then(function (results) {
|
||||
SettingsModel.browse().then(function (results) {
|
||||
|
||||
should.exist(results);
|
||||
|
||||
results.length.should.be.above(0);
|
||||
|
||||
firstPost = results.models[0];
|
||||
secondPost = results.models[1];
|
||||
model1 = results.models[0];
|
||||
model2 = results.models[1];
|
||||
|
||||
// The edit method has been modified to take an object of
|
||||
// key/value pairs
|
||||
toEdit[firstPost.attributes.key] = "new value1";
|
||||
toEdit[secondPost.attributes.key] = "new value2";
|
||||
model1.set('value', 'new value1');
|
||||
model2.set('value', 'new value2');
|
||||
|
||||
return SettingModel.edit(toEdit);
|
||||
return SettingsModel.edit([model1, model2]);
|
||||
|
||||
}).then(function (edited) {
|
||||
|
||||
@ -115,15 +113,15 @@
|
||||
|
||||
edited.length.should.equal(2);
|
||||
|
||||
editedPost = edited[0];
|
||||
editedModel = edited[0];
|
||||
|
||||
editedPost.attributes.key.should.equal(firstPost.attributes.key);
|
||||
editedPost.attributes.value.should.equal('new value1');
|
||||
editedModel.attributes.key.should.equal(model1.attributes.key);
|
||||
editedModel.attributes.value.should.equal('new value1');
|
||||
|
||||
editedPost = edited[1];
|
||||
editedModel = edited[1];
|
||||
|
||||
editedPost.attributes.key.should.equal(secondPost.attributes.key);
|
||||
editedPost.attributes.value.should.equal('new value2');
|
||||
editedModel.attributes.key.should.equal(model2.attributes.key);
|
||||
editedModel.attributes.value.should.equal('new value2');
|
||||
|
||||
done();
|
||||
|
||||
@ -136,7 +134,7 @@
|
||||
value: 'Test Content 1'
|
||||
};
|
||||
|
||||
SettingModel.add(newSetting).then(function (createdSetting) {
|
||||
SettingsModel.add(newSetting).then(function (createdSetting) {
|
||||
|
||||
should.exist(createdSetting);
|
||||
|
||||
@ -150,7 +148,7 @@
|
||||
it('can delete', function (done) {
|
||||
var firstSettingId;
|
||||
|
||||
SettingModel.browse().then(function (results) {
|
||||
SettingsModel.browse().then(function (results) {
|
||||
|
||||
should.exist(results);
|
||||
|
||||
@ -158,11 +156,11 @@
|
||||
|
||||
firstSettingId = results.models[0].id;
|
||||
|
||||
return SettingModel.destroy(firstSettingId);
|
||||
return SettingsModel.destroy(firstSettingId);
|
||||
|
||||
}).then(function () {
|
||||
|
||||
return SettingModel.browse();
|
||||
return SettingsModel.browse();
|
||||
|
||||
}).then(function (newResults) {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user