0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

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.
This commit is contained in:
Fabien O'Carroll 2021-06-24 10:42:27 +01:00
parent 1ff4f6ce7d
commit cbac3d1eb0
3 changed files with 100 additions and 8 deletions

View file

@ -0,0 +1,38 @@
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)
};

View file

@ -29,6 +29,7 @@ const models = [
'mobiledoc-revision',
'member',
'product',
'benefit',
'stripe-product',
'stripe-price',
'member-subscribe-event',

View file

@ -1,4 +1,5 @@
const ghostBookshelf = require('./base');
const _ = require('lodash');
const Product = ghostBookshelf.Model.extend({
tableName: 'products',
@ -13,16 +14,68 @@ const Product = ghostBookshelf.Model.extend({
if (model.hasChanged('slug') || !model.get('slug')) {
const slug = model.get('slug') || model.get('name');
if (!slug) {
return;
if (slug) {
const cleanSlug = await ghostBookshelf.Model.generateSlug(Product, slug, {
transacting: options.transacting
});
model.set({slug: cleanSlug});
}
const cleanSlug = await ghostBookshelf.Model.generateSlug(Product, slug, {
transacting: options.transacting
});
return model.set({slug: cleanSlug});
}
let benefitsToSave = [];
if (_.isUndefined(this.get('benefits'))) {
this.unset('benefits');
return;
}
// CASE: detect lowercase/uppercase label slugs
if (!_.isUndefined(this.get('benefits')) && !_.isNull(this.get('benefits'))) {
benefitsToSave = [];
// and deduplicate upper/lowercase tags
_.each(this.get('benefits'), function each(item) {
item.name = item.name && item.name.trim();
for (let i = 0; i < benefitsToSave.length; i = i + 1) {
if (benefitsToSave[i].name && item.name && benefitsToSave[i].name.toLocaleLowerCase() === item.name.toLocaleLowerCase()) {
return;
}
}
benefitsToSave.push(item);
});
}
const existingBenefits = await ghostBookshelf.model('Benefit').findAll(Object.assign({
columns: ['id', 'name']
}, _.pick(options, 'transacting')));
benefitsToSave.forEach((benefitToSave) => {
const existingBenefitModel = existingBenefits.find((existingBenefit) => {
return benefitToSave.name.toLowerCase() === existingBenefit.get('name').toLowerCase();
});
if (existingBenefitModel) {
benefitToSave.name = existingBenefitModel.get('name');
}
});
model.set('benefits', benefitsToSave);
},
/**
* The base model keeps only the columns, which are defined in the schema.
* We have to add the relations on top, otherwise bookshelf-relations
* has no access to the nested relations, which should be updated.
*/
permittedAttributes: function permittedAttributes() {
let filteredKeys = ghostBookshelf.Model.prototype.permittedAttributes.apply(this, arguments);
this.relationships.forEach((key) => {
filteredKeys.push(key);
});
return filteredKeys;
},
monthlyPrice() {