0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

Ensured consistency for event handlers in the model layer

no issue

- the event chain works like this:
  - if a model registers an event, it get's triggered, because it's stronger than the base model
- but you have to call the base model to agree on a contract, because base model implements generic logic in event handlers
- this was inconsistently used
This commit is contained in:
kirrg001 2019-02-07 10:59:37 +01:00
parent 46bf5270df
commit 865366c7c8
9 changed files with 47 additions and 4 deletions

View file

@ -13,6 +13,7 @@ Accesstoken = Basetoken.extend({
},
onCreated: function onCreated(model, attrs, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
}
});

View file

@ -292,6 +292,8 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
this.attributes = this.pick(this.permittedAttributes());
},
onDestroying() {},
/**
* Adding resources implies setting these properties on the server side
* - set `created_by` based on the context
@ -397,6 +399,8 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
addAction(model, 'deleted', options);
},
onSaved() {},
/**
* before we insert dates into the database, we have to normalize
* date format is now in each db the same

View file

@ -48,7 +48,9 @@ const Integration = ghostBookshelf.Model.extend({
return editIntegration();
},
onSaving(newIntegration, attr, options) {
onSaving(integration, attrs, options) {
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
if (this.hasChanged('slug') || !this.get('slug')) {
// Pass the new slug through the generator to strip illegal characters, detect duplicates
return ghostBookshelf.Model.generateSlug(Integration, this.get('slug') || this.get('name'),

View file

@ -95,6 +95,8 @@ Post = ghostBookshelf.Model.extend({
* We ensure that we are catching the event after bookshelf relations.
*/
onSaved: function onSaved(model, response, options) {
ghostBookshelf.Model.prototype.onSaved.apply(this, arguments);
if (options.method !== 'insert') {
return;
}
@ -109,7 +111,7 @@ Post = ghostBookshelf.Model.extend({
},
onUpdated: function onUpdated(model, attrs, options) {
ghostBookshelf.Model.prototype.onUpdated.call(this, model, attrs, options);
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
model.statusChanging = model.get('status') !== model.previous('status');
model.isPublished = model.get('status') === 'published';
@ -181,7 +183,7 @@ Post = ghostBookshelf.Model.extend({
},
onDestroyed: function onDestroyed(model, options) {
ghostBookshelf.Model.prototype.onDestroyed.call(this, model, options);
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
if (model.previous('status') === 'published') {
model.emitChange('unpublished', Object.assign({usePreviousAttribute: true}, options));
@ -191,6 +193,8 @@ Post = ghostBookshelf.Model.extend({
},
onDestroying: function onDestroyed(model) {
ghostBookshelf.Model.prototype.onDestroying.apply(this, arguments);
this.handleAttachedModels(model);
},
@ -319,7 +323,7 @@ Post = ghostBookshelf.Model.extend({
this.handleAttachedModels(model);
ghostBookshelf.Model.prototype.onSaving.call(this, model, attr, options);
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
// do not allow generated fields to be overridden via the API
if (!options.migrating) {

View file

@ -71,16 +71,22 @@ Settings = ghostBookshelf.Model.extend({
},
onDestroyed: function onDestroyed(model, options) {
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
model.emitChange('deleted', options);
model.emitChange(model._previousAttributes.key + '.' + 'deleted', options);
},
onCreated: function onCreated(model, response, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
model.emitChange(model.attributes.key + '.' + 'added', options);
},
onUpdated: function onUpdated(model, response, options) {
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
model.emitChange('edited', options);
model.emitChange(model.attributes.key + '.' + 'edited', options);
},

View file

@ -20,14 +20,20 @@ Subscriber = ghostBookshelf.Model.extend({
},
onCreated: function onCreated(model, response, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
},
onUpdated: function onUpdated(model, response, 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);
}
}, {

View file

@ -18,14 +18,20 @@ Tag = ghostBookshelf.Model.extend({
},
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);
},

View file

@ -44,12 +44,16 @@ User = ghostBookshelf.Model.extend({
* Therefor we have to remove the relations manually.
*/
onDestroying(model, options) {
ghostBookshelf.Model.prototype.onDestroying.apply(this, arguments);
return (options.transacting || ghostBookshelf.knex)('roles_users')
.where('user_id', model.id)
.del();
},
onDestroyed: function onDestroyed(model, options) {
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
if (_.includes(activeStates, model.previous('status'))) {
model.emitChange('deactivated', options);
}
@ -58,6 +62,8 @@ User = ghostBookshelf.Model.extend({
},
onCreated: function onCreated(model, attrs, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
// active is the default state, so if status isn't provided, this will be an active user
@ -67,6 +73,8 @@ User = ghostBookshelf.Model.extend({
},
onUpdated: function onUpdated(model, response, options) {
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
model.statusChanging = model.get('status') !== model.previous('status');
model.isActive = _.includes(activeStates, model.get('status'));

View file

@ -24,14 +24,20 @@ Webhook = ghostBookshelf.Model.extend({
},
onCreated: function onCreated(model, response, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
},
onUpdated: function onUpdated(model, response, 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);
}
}, {