2019-09-16 11:45:55 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../shared/url-utils');
|
2019-09-16 11:45:55 +03:00
|
|
|
|
|
|
|
const PostsMeta = ghostBookshelf.Model.extend({
|
2019-10-09 15:38:08 +03:00
|
|
|
tableName: 'posts_meta',
|
|
|
|
|
|
|
|
onSaving: function onSaving() {
|
|
|
|
const urlTransformMap = {
|
2021-03-05 16:54:01 +03:00
|
|
|
og_image: 'toTransformReady',
|
|
|
|
twitter_image: 'toTransformReady'
|
2019-10-09 15:38:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.entries(urlTransformMap).forEach(([attr, transform]) => {
|
|
|
|
let method = transform;
|
|
|
|
let methodOptions = {};
|
|
|
|
|
|
|
|
if (typeof transform === 'object') {
|
|
|
|
method = transform.method;
|
|
|
|
methodOptions = transform.options || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasChanged(attr) && this.get(attr)) {
|
|
|
|
const transformedValue = urlUtils[method](this.get(attr), methodOptions);
|
|
|
|
this.set(attr, transformedValue);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
|
|
|
|
}
|
2019-09-16 11:45:55 +03:00
|
|
|
}, {
|
|
|
|
post() {
|
|
|
|
return this.belongsTo('Post');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
PostsMeta: ghostBookshelf.model('PostsMeta', PostsMeta)
|
|
|
|
};
|