mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 20:34:02 +03:00
99fde5154f
- this has been a niggle for ages, we shouldn't need to care what order our models are loaded in - this is stopping us from having a built-in model loader and "frameworkizing" models
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
const _ = require('lodash');
|
|
const ghostBookshelf = require('./base');
|
|
|
|
const candidates = [];
|
|
|
|
const Action = ghostBookshelf.Model.extend({
|
|
tableName: 'actions',
|
|
|
|
initialize: function initialize() {
|
|
_.each(ghostBookshelf.registry.models, (model) => {
|
|
candidates.push([model, model.prototype.tableName.replace(/s$/, '')]);
|
|
});
|
|
this.constructor.__super__.initialize.apply(this, arguments);
|
|
},
|
|
|
|
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)
|
|
};
|