cbac3d1eb0
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.
39 lines
1.1 KiB
JavaScript
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)
|
|
};
|