mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 13:54:35 +03:00
b03ecd9ebc
Refs #2170 This removes the circular dependency problem from our models thanks to https://github.com/tgriesser/bookshelf/issues/181 - add the registry plugin - switch all models and collections to be registered - switch relationships to be defined using a string, which calls from the registry
65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
var ghostBookshelf = require('./base'),
|
|
App,
|
|
Apps;
|
|
|
|
|
|
|
|
App = ghostBookshelf.Model.extend({
|
|
tableName: 'apps',
|
|
|
|
saving: function (newPage, attr, options) {
|
|
/*jshint unused:false*/
|
|
var self = this;
|
|
|
|
ghostBookshelf.Model.prototype.saving.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(App, this.get('slug') || this.get('name'),
|
|
{transacting: options.transacting})
|
|
.then(function (slug) {
|
|
self.set({slug: slug});
|
|
});
|
|
}
|
|
},
|
|
|
|
permissions: function () {
|
|
// Have to use the require here because of circular dependencies
|
|
return this.belongsToMany('Permission', 'permissions_apps');
|
|
},
|
|
|
|
settings: function () {
|
|
return this.belongsToMany('AppSetting', 'app_settings');
|
|
}
|
|
}, {
|
|
/**
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
*/
|
|
permittedOptions: function (methodName) {
|
|
var options = ghostBookshelf.Model.permittedOptions(),
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
validOptions = {
|
|
findOne: ['withRelated']
|
|
};
|
|
|
|
if (validOptions[methodName]) {
|
|
options = options.concat(validOptions[methodName]);
|
|
}
|
|
|
|
return options;
|
|
}
|
|
});
|
|
|
|
Apps = ghostBookshelf.Collection.extend({
|
|
model: App
|
|
});
|
|
|
|
module.exports = {
|
|
App: ghostBookshelf.model('App', App),
|
|
Apps: ghostBookshelf.collection('Apps', Apps)
|
|
};
|