Ghost/core/server/models/custom-theme-setting.js
Kevin Ansfield 7cb93be60b Added boolean as allowed custom theme setting type
refs https://github.com/TryGhost/Team/issues/1106

- updated schema validation to add `'boolean'` as an allowed `type` value
- added `format()` and `parse()` methods to `CustomThemeSetting` model to match `Settings` model behaviour for boolean-type settings
2021-10-13 17:25:32 +01:00

51 lines
1.6 KiB
JavaScript

const _ = require('lodash');
const ghostBookshelf = require('./base');
const CustomThemeSetting = ghostBookshelf.Model.extend({
tableName: 'custom_theme_settings',
parse() {
const attrs = ghostBookshelf.Model.prototype.parse.apply(this, arguments);
const settingType = attrs.type;
// transform "0" to false for boolean type
if (settingType === 'boolean' && (attrs.value === '0' || attrs.value === '1')) {
attrs.value = !!+attrs.value;
}
// transform "false" to false for boolean type
if (settingType === 'boolean' && (attrs.value === 'false' || attrs.value === 'true')) {
attrs.value = JSON.parse(attrs.value);
}
return attrs;
},
format() {
const attrs = ghostBookshelf.Model.prototype.format.apply(this, arguments);
const settingType = attrs.type;
if (settingType === 'boolean') {
// CASE: Ensure we won't forward strings, otherwise model events or model interactions can fail
if (attrs.value === '0' || attrs.value === '1') {
attrs.value = !!+attrs.value;
}
// CASE: Ensure we won't forward strings, otherwise model events or model interactions can fail
if (attrs.value === 'false' || attrs.value === 'true') {
attrs.value = JSON.parse(attrs.value);
}
if (_.isBoolean(attrs.value)) {
attrs.value = attrs.value.toString();
}
}
return attrs;
}
});
module.exports = {
CustomThemeSetting: ghostBookshelf.model('CustomThemeSetting', CustomThemeSetting)
};