mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
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
5 changed files with 81 additions and 34 deletions
|
@ -60,6 +60,13 @@ module.exports = {
|
||||||
"created_by": 1,
|
"created_by": 1,
|
||||||
"updated_by": 1,
|
"updated_by": 1,
|
||||||
"type": "general"
|
"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) {
|
knex.Schema.createTable('settings', function (t) {
|
||||||
t.increments().primary();
|
t.increments().primary();
|
||||||
t.string('uuid');
|
t.string('uuid');
|
||||||
t.string('key');
|
t.string('key').unique();
|
||||||
t.text('value');
|
t.text('value');
|
||||||
t.string('type');
|
t.string('type');
|
||||||
t.date('created_at');
|
t.date('created_at');
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
User: require('./user').User,
|
User: require('./user').User,
|
||||||
Role: require('./role').Role,
|
Role: require('./role').Role,
|
||||||
Permission: require('./permission').Permission,
|
Permission: require('./permission').Permission,
|
||||||
Setting: require('./setting').Setting,
|
Settings: require('./settings').Settings,
|
||||||
init: function () {
|
init: function () {
|
||||||
return knex.Schema.hasTable('posts').then(null, function () {
|
return knex.Schema.hasTable('posts').then(null, function () {
|
||||||
// Simple bootstraping of the data model for now.
|
// 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'),
|
helpers = require('./helpers'),
|
||||||
Models = require('../../shared/models');
|
Models = require('../../shared/models');
|
||||||
|
|
||||||
describe('Setting Model', function () {
|
describe('Settings Model', function () {
|
||||||
|
|
||||||
var SettingModel = Models.Setting;
|
var SettingsModel = Models.Settings;
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
helpers.resetData().then(function () {
|
helpers.resetData().then(function () {
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can browse', function (done) {
|
it('can browse', function (done) {
|
||||||
SettingModel.browse().then(function (results) {
|
SettingsModel.browse().then(function (results) {
|
||||||
|
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@
|
||||||
it('can read', function (done) {
|
it('can read', function (done) {
|
||||||
var firstSetting;
|
var firstSetting;
|
||||||
|
|
||||||
SettingModel.browse().then(function (results) {
|
SettingsModel.browse().then(function (results) {
|
||||||
|
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@
|
||||||
|
|
||||||
firstSetting = results.models[0];
|
firstSetting = results.models[0];
|
||||||
|
|
||||||
return SettingModel.read(firstSetting.attributes.key);
|
return SettingsModel.read(firstSetting.attributes.key);
|
||||||
|
|
||||||
}).then(function (found) {
|
}).then(function (found) {
|
||||||
|
|
||||||
|
@ -54,22 +54,21 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can edit single', function (done) {
|
it('can edit single', function (done) {
|
||||||
var firstPost,
|
var firstSetting;
|
||||||
toEdit = {};
|
|
||||||
|
|
||||||
SettingModel.browse().then(function (results) {
|
SettingsModel.browse().then(function (results) {
|
||||||
|
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
|
|
||||||
results.length.should.be.above(0);
|
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
|
// The edit method has been modified to take an object of
|
||||||
// key/value pairs
|
// 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) {
|
}).then(function (edited) {
|
||||||
|
|
||||||
|
@ -79,7 +78,7 @@
|
||||||
|
|
||||||
edited = edited[0];
|
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');
|
edited.attributes.value.should.equal('new value');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
@ -88,26 +87,25 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can edit multiple', function (done) {
|
it('can edit multiple', function (done) {
|
||||||
var firstPost,
|
var model1,
|
||||||
secondPost,
|
model2,
|
||||||
editedPost,
|
editedModel;
|
||||||
toEdit = {};
|
|
||||||
|
|
||||||
SettingModel.browse().then(function (results) {
|
SettingsModel.browse().then(function (results) {
|
||||||
|
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
|
|
||||||
results.length.should.be.above(0);
|
results.length.should.be.above(0);
|
||||||
|
|
||||||
firstPost = results.models[0];
|
model1 = results.models[0];
|
||||||
secondPost = results.models[1];
|
model2 = results.models[1];
|
||||||
|
|
||||||
// The edit method has been modified to take an object of
|
// The edit method has been modified to take an object of
|
||||||
// key/value pairs
|
// key/value pairs
|
||||||
toEdit[firstPost.attributes.key] = "new value1";
|
model1.set('value', 'new value1');
|
||||||
toEdit[secondPost.attributes.key] = "new value2";
|
model2.set('value', 'new value2');
|
||||||
|
|
||||||
return SettingModel.edit(toEdit);
|
return SettingsModel.edit([model1, model2]);
|
||||||
|
|
||||||
}).then(function (edited) {
|
}).then(function (edited) {
|
||||||
|
|
||||||
|
@ -115,15 +113,15 @@
|
||||||
|
|
||||||
edited.length.should.equal(2);
|
edited.length.should.equal(2);
|
||||||
|
|
||||||
editedPost = edited[0];
|
editedModel = edited[0];
|
||||||
|
|
||||||
editedPost.attributes.key.should.equal(firstPost.attributes.key);
|
editedModel.attributes.key.should.equal(model1.attributes.key);
|
||||||
editedPost.attributes.value.should.equal('new value1');
|
editedModel.attributes.value.should.equal('new value1');
|
||||||
|
|
||||||
editedPost = edited[1];
|
editedModel = edited[1];
|
||||||
|
|
||||||
editedPost.attributes.key.should.equal(secondPost.attributes.key);
|
editedModel.attributes.key.should.equal(model2.attributes.key);
|
||||||
editedPost.attributes.value.should.equal('new value2');
|
editedModel.attributes.value.should.equal('new value2');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
||||||
|
@ -136,7 +134,7 @@
|
||||||
value: 'Test Content 1'
|
value: 'Test Content 1'
|
||||||
};
|
};
|
||||||
|
|
||||||
SettingModel.add(newSetting).then(function (createdSetting) {
|
SettingsModel.add(newSetting).then(function (createdSetting) {
|
||||||
|
|
||||||
should.exist(createdSetting);
|
should.exist(createdSetting);
|
||||||
|
|
||||||
|
@ -150,7 +148,7 @@
|
||||||
it('can delete', function (done) {
|
it('can delete', function (done) {
|
||||||
var firstSettingId;
|
var firstSettingId;
|
||||||
|
|
||||||
SettingModel.browse().then(function (results) {
|
SettingsModel.browse().then(function (results) {
|
||||||
|
|
||||||
should.exist(results);
|
should.exist(results);
|
||||||
|
|
||||||
|
@ -158,11 +156,11 @@
|
||||||
|
|
||||||
firstSettingId = results.models[0].id;
|
firstSettingId = results.models[0].id;
|
||||||
|
|
||||||
return SettingModel.destroy(firstSettingId);
|
return SettingsModel.destroy(firstSettingId);
|
||||||
|
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
|
||||||
return SettingModel.browse();
|
return SettingsModel.browse();
|
||||||
|
|
||||||
}).then(function (newResults) {
|
}).then(function (newResults) {
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue