mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
80fa1d903e
- as per 5a5a5d162e
, the Bookshelf registry plugin is now in core
- we no longer need to explicitly load the plugin, and it displays a
warning if you do
- this change also turns `._models` into `.registry.models`, so our code has
been updated to reflect that
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
const _ = require('lodash');
|
|
const ghostBookshelf = require('./base');
|
|
|
|
const candidates = [];
|
|
|
|
_.each(ghostBookshelf.registry.models, (model) => {
|
|
candidates.push([model, model.prototype.tableName.replace(/s$/, '')]);
|
|
});
|
|
|
|
const Action = ghostBookshelf.Model.extend({
|
|
tableName: 'actions',
|
|
|
|
actor() {
|
|
return this.morphTo('actor', ['actor_type', 'actor_id'], ...candidates);
|
|
},
|
|
|
|
resource() {
|
|
return this.morphTo('resource', ['resource_type', 'resource_id'], ...candidates);
|
|
},
|
|
|
|
toJSON(unfilteredOptions) {
|
|
const options = Action.filterOptions(unfilteredOptions, 'toJSON');
|
|
const attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
|
|
|
|
// @TODO: context is not implemented yet
|
|
delete attrs.context;
|
|
|
|
return attrs;
|
|
}
|
|
}, {
|
|
orderDefaultOptions: function orderDefaultOptions() {
|
|
return {
|
|
created_at: 'DESC'
|
|
};
|
|
},
|
|
|
|
add(data, unfilteredOptions = {}) {
|
|
const options = this.filterOptions(unfilteredOptions, 'add');
|
|
return ghostBookshelf.Model.add.call(this, data, options);
|
|
}
|
|
});
|
|
|
|
module.exports = {
|
|
Action: ghostBookshelf.model('Action', Action)
|
|
};
|