From c82e5976ccece32143e2956c53e7432e9db2847e Mon Sep 17 00:00:00 2001 From: Ricardo Tomasi Date: Sat, 8 Jun 2013 02:03:55 -0300 Subject: [PATCH] Changes to Settings Model - add email default setting to fixture - make settings a single model - create UNIQUE index on setting keys --- core/shared/data/fixtures/001.js | 7 ++++ core/shared/data/migration/001.js | 2 +- core/shared/models/index.js | 2 +- core/shared/models/settings.js | 42 +++++++++++++++++++ core/test/ghost/api_settings_spec.js | 62 ++++++++++++++-------------- 5 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 core/shared/models/settings.js diff --git a/core/shared/data/fixtures/001.js b/core/shared/data/fixtures/001.js index 5144091f72..8d69c6266b 100644 --- a/core/shared/data/fixtures/001.js +++ b/core/shared/data/fixtures/001.js @@ -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" } ], diff --git a/core/shared/data/migration/001.js b/core/shared/data/migration/001.js index d1540e0807..a7aada7d6a 100644 --- a/core/shared/data/migration/001.js +++ b/core/shared/data/migration/001.js @@ -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'); diff --git a/core/shared/models/index.js b/core/shared/models/index.js index ec9afd8da8..1cfc8fcd64 100644 --- a/core/shared/models/index.js +++ b/core/shared/models/index.js @@ -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. diff --git a/core/shared/models/settings.js b/core/shared/models/settings.js new file mode 100644 index 0000000000..a3219d7752 --- /dev/null +++ b/core/shared/models/settings.js @@ -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 + }; + +}()); \ No newline at end of file diff --git a/core/test/ghost/api_settings_spec.js b/core/test/ghost/api_settings_spec.js index 082aeac61c..670f1a9a23 100644 --- a/core/test/ghost/api_settings_spec.js +++ b/core/test/ghost/api_settings_spec.js @@ -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) {