mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
15b7485a94
refs https://github.com/TryGhost/Team/issues/586 - Member model now has `products` relation, sorted using `sort_order`, following convention from `labels` - Product model has handling to set `slug` from name, following convention of Label model - Updated filter plugin to handle filtering Member models by their `product` relations e.g. `product:[slug, slug]`
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
|
|
const Product = ghostBookshelf.Model.extend({
|
|
tableName: 'products',
|
|
|
|
async onSaving(model, _attr, options) {
|
|
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
|
|
|
|
if (model.get('name')) {
|
|
model.set('name', model.get('name').trim());
|
|
}
|
|
|
|
if (model.hasChanged('slug') || !model.get('slug')) {
|
|
const slug = model.get('slug') || model.get('name');
|
|
|
|
if (!slug) {
|
|
return;
|
|
}
|
|
|
|
const cleanSlug = await ghostBookshelf.Model.generateSlug(Product, slug, {
|
|
transacting: options.transacting
|
|
});
|
|
|
|
return model.set({slug: cleanSlug});
|
|
}
|
|
},
|
|
|
|
members: function members() {
|
|
return this.belongsToMany('Member', 'members_products', 'product_id', 'member_id');
|
|
}
|
|
});
|
|
|
|
const Products = ghostBookshelf.Collection.extend({
|
|
model: Product
|
|
});
|
|
|
|
module.exports = {
|
|
Product: ghostBookshelf.model('Product', Product),
|
|
Products: ghostBookshelf.collection('Products', Products)
|
|
};
|