Ghost/core/server/models/benefit.js
Fabien O'Carroll cbac3d1eb0 Added Benefit model
refs https://github.com/TryGhost/Team/issues/806

This is the model to represent the Benefit resource stored in the
`benefits` table. The `onSaving` method has been copied from the Tag
model and ensures that we have a unique slug.
2021-06-29 16:53:15 +01:00

39 lines
1.1 KiB
JavaScript

const ghostBookshelf = require('./base');
const Benefit = ghostBookshelf.Model.extend({
tableName: 'benefits',
async onSaving(model, attr, options) {
ghostBookshelf.Model.prototype.onSaving.call(this, model, attr, options);
// Make sure name is trimmed of extra spaces
let name = this.get('name') && this.get('name').trim();
this.set('name', name);
if (this.hasChanged('slug') || (!this.get('slug') && this.get('name'))) {
// Pass the new slug through the generator to strip illegal characters, detect duplicates
const slug = await ghostBookshelf.Model.generateSlug(
Benefit,
this.get('slug') || this.get('name'),
{transacting: options.transacting}
);
this.set({slug});
}
}
}, {
orderDefaultOptions() {
return {
name: 'ASC',
created_at: 'DESC'
};
}
});
const Benefits = ghostBookshelf.Collection.extend({
model: Benefit
});
module.exports = {
Benefit: ghostBookshelf.model('Benefit', Benefit),
Benefits: ghostBookshelf.collection('Benefits', Benefits)
};