2021-10-13 19:25:32 +03:00
|
|
|
const _ = require('lodash');
|
2021-09-23 13:51:18 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
const CustomThemeSetting = ghostBookshelf.Model.extend({
|
2021-10-13 19:25:32 +03:00
|
|
|
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;
|
|
|
|
}
|
2021-09-23 13:51:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
CustomThemeSetting: ghostBookshelf.model('CustomThemeSetting', CustomThemeSetting)
|
|
|
|
};
|