Ghost/core/shared/models/models.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

/*global require, module */
(function () {
"use strict";
// We should just be able to require bookshelf and have it reference
// the `Knex` instance bootstraped at the app initialization.
var Bookshelf = require('bookshelf'),
Showdown = require('showdown'),
converter = new Showdown.converter(),
Post,
Posts,
User,
2013-05-20 01:52:53 +04:00
Setting,
Settings;
Post = Bookshelf.Model.extend({
tableName: 'posts',
hasTimestamps: true,
initialize: function () {
this.on('creating', this.creating, this);
this.on('saving', this.saving, this);
},
saving: function () {
if (!this.get('title')) {
throw new Error('Post title cannot be blank');
}
this.set('content_html', converter.makeHtml(this.get('content')));
// refactoring of ghost required in order to make these details available here
// this.set('language', this.get('language') || ghost.config().defaultLang);
// this.set('status', this.get('status') || ghost.statuses().draft);
},
creating: function () {
if (!this.get('slug')) {
this.generateSlug();
}
},
generateSlug: function () {
return this.set('slug', this.get('title').replace(/\:/g, '').replace(/\s/g, '-').toLowerCase());
},
user: function () {
return this.belongsTo(User, 'created_by');
}
});
Posts = Bookshelf.Collection.extend({
model: Post
});
User = Bookshelf.Model.extend({
tableName: 'users',
hasTimestamps: true,
posts: function () {
return this.hasMany(Posts, 'created_by');
}
});
Setting = Bookshelf.Model.extend({
2013-05-20 01:52:53 +04:00
tableName: 'settings',
hasTimestamps: true
});
2013-05-20 01:52:53 +04:00
Settings = Bookshelf.Collection.extend({
model: Setting
});
module.exports = {
Post: Post,
Posts: Posts,
User: User,
2013-05-20 01:52:53 +04:00
Setting: Setting,
Settings: Settings
};
}());