Ghost/core/server/models/label.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

135 lines
4.2 KiB
JavaScript

const ghostBookshelf = require('./base');
const common = require('../lib/common');
let Label;
let Labels;
Label = ghostBookshelf.Model.extend({
tableName: 'labels',
emitChange: function emitChange(event, options) {
const eventToTrigger = 'label' + '.' + event;
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
},
onCreated: function onCreated(model, attrs, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
},
onUpdated: function onUpdated(model, attrs, options) {
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
model.emitChange('edited', options);
},
onDestroyed: function onDestroyed(model, options) {
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
model.emitChange('deleted', options);
},
onSaving: function onSaving(newLabel, attr, options) {
const self = this;
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
// 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
return ghostBookshelf.Model.generateSlug(Label, this.get('slug') || this.get('name'),
{transacting: options.transacting})
.then(function then(slug) {
self.set({slug: slug});
});
}
},
members: function members() {
return this.belongsToMany('Member', 'members_labels', 'label_id', 'member_id');
},
toJSON: function toJSON(unfilteredOptions) {
const options = Label.filterOptions(unfilteredOptions, 'toJSON');
const attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
return attrs;
},
getAction(event, options) {
const actor = this.getActor(options);
// @NOTE: we ignore internal updates (`options.context.internal`) for now
if (!actor) {
return;
}
// @TODO: implement context
return {
event: event,
resource_id: this.id || this.previous('id'),
resource_type: 'label',
actor_id: actor.id,
actor_type: actor.type
};
}
}, {
orderDefaultOptions: function orderDefaultOptions() {
return {
name: 'ASC',
created_at: 'DESC'
};
},
permittedOptions: function permittedOptions(methodName) {
let options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
// whitelists for the `options` hash argument on methods, by method name.
// these are the only options that can be passed to Bookshelf / Knex.
const validOptions = {
findAll: ['columns'],
findOne: ['columns'],
destroy: ['destroyAll']
};
if (validOptions[methodName]) {
options = options.concat(validOptions[methodName]);
}
return options;
},
destroy: function destroy(unfilteredOptions) {
const options = this.filterOptions(unfilteredOptions, 'destroy', {extraAllowedProperties: ['id']});
options.withRelated = ['members'];
return this.forge({id: options.id})
.fetch(options)
.then(function destroyLabelsAndMember(label) {
if (!label) {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.labels.labelNotFound')
}));
}
return label.related('members')
.detach(null, options)
.then(function destroyLabels() {
return label.destroy(options);
});
});
}
});
Labels = ghostBookshelf.Collection.extend({
model: Label
});
module.exports = {
Label: ghostBookshelf.model('Label', Label),
Labels: ghostBookshelf.collection('Labels', Labels)
};