2020-10-16 20:02:58 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
2021-03-05 16:54:01 +03:00
|
|
|
const urlUtils = require('../../shared/url-utils');
|
2020-10-16 20:02:58 +03:00
|
|
|
|
|
|
|
const Snippet = ghostBookshelf.Model.extend({
|
2021-03-05 16:54:01 +03:00
|
|
|
tableName: 'snippets',
|
|
|
|
|
|
|
|
onSaving: function onSaving() {
|
|
|
|
const urlTransformMap = {
|
|
|
|
mobiledoc: 'mobiledocToTransformReady'
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-10-16 20:02:58 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const Snippets = ghostBookshelf.Collection.extend({
|
|
|
|
model: Snippet
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Snippet: ghostBookshelf.model('Snippet', Snippet),
|
|
|
|
Snippets: ghostBookshelf.collection('Snippets', Snippets)
|
|
|
|
};
|