2013-06-25 15:43:15 +04:00
|
|
|
var GhostBookshelf,
|
|
|
|
Bookshelf = require('bookshelf'),
|
2013-09-14 23:01:46 +04:00
|
|
|
when = require('when'),
|
2013-08-16 18:11:36 +04:00
|
|
|
moment = require('moment'),
|
2013-07-08 15:39:11 +04:00
|
|
|
_ = require('underscore'),
|
2013-09-14 23:01:46 +04:00
|
|
|
uuid = require('node-uuid'),
|
2013-08-20 22:52:44 +04:00
|
|
|
config = require('../../../config'),
|
|
|
|
Validator = require('validator').Validator;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// Initializes Bookshelf as its own instance, so we can modify the Models and not mess up
|
|
|
|
// others' if they're using the library outside of ghost.
|
2013-09-11 19:23:07 +04:00
|
|
|
GhostBookshelf = Bookshelf.Initialize('ghost', config[process.env.NODE_ENV || 'development'].database);
|
2013-09-15 20:04:42 +04:00
|
|
|
GhostBookshelf.client = config[process.env.NODE_ENV].database.client;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-08-20 22:52:44 +04:00
|
|
|
GhostBookshelf.validator = new Validator();
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// The Base Model which other Ghost objects will inherit from,
|
|
|
|
// including some convenience functions as static properties on the model.
|
|
|
|
GhostBookshelf.Model = GhostBookshelf.Model.extend({
|
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
hasTimestamps: true,
|
|
|
|
|
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.on('creating', this.creating, this);
|
|
|
|
this.on('saving', this.saving, this);
|
|
|
|
this.on('saving', this.validate, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
creating: function () {
|
|
|
|
if (!this.get('created_by')) {
|
|
|
|
this.set('created_by', 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
saving: function () {
|
|
|
|
// Remove any properties which don't belong on the post model
|
|
|
|
this.attributes = this.pick(this.permittedAttributes);
|
|
|
|
|
|
|
|
this.set('updated_by', 1);
|
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Base prototype properties will go here
|
2013-07-08 15:39:11 +04:00
|
|
|
// Fix problems with dates
|
|
|
|
fixDates: function (attrs) {
|
|
|
|
_.each(attrs, function (value, key) {
|
|
|
|
if (key.substr(-3) === '_at' && value !== null) {
|
2013-08-16 18:11:36 +04:00
|
|
|
attrs[key] = moment(attrs[key]).toDate();
|
2013-07-08 15:39:11 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
|
|
|
format: function (attrs) {
|
|
|
|
return this.fixDates(attrs);
|
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function (options) {
|
|
|
|
var attrs = this.fixDates(_.extend({}, this.attributes)),
|
|
|
|
relations = this.relations;
|
|
|
|
|
|
|
|
if (options && options.shallow) {
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:53:46 +04:00
|
|
|
_.each(relations, function (relation, key) {
|
2013-08-21 16:55:58 +04:00
|
|
|
if (key.substring(0, 7) !== "_pivot_") {
|
|
|
|
attrs[key] = relation.toJSON ? relation.toJSON() : relation;
|
|
|
|
}
|
2013-07-08 15:39:11 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
2013-09-14 23:01:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// #### generateSlug
|
|
|
|
// Create a string act as the permalink for an object.
|
|
|
|
generateSlug: function (Model, base) {
|
|
|
|
var slug,
|
|
|
|
slugTryCount = 1,
|
|
|
|
// Look for a post with a matching slug, append an incrementing number if so
|
|
|
|
checkIfSlugExists = function (slugToFind) {
|
|
|
|
return Model.read({slug: slugToFind}).then(function (found) {
|
|
|
|
var trimSpace;
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return when.resolve(slugToFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
slugTryCount += 1;
|
|
|
|
|
|
|
|
// If this is the first time through, add the hyphen
|
|
|
|
if (slugTryCount === 2) {
|
|
|
|
slugToFind += '-';
|
|
|
|
} else {
|
|
|
|
// Otherwise, trim the number off the end
|
|
|
|
trimSpace = -(String(slugTryCount - 1).length);
|
|
|
|
slugToFind = slugToFind.slice(0, trimSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
slugToFind += slugTryCount;
|
|
|
|
|
|
|
|
return checkIfSlugExists(slugToFind);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
|
|
|
|
slug = base.trim().replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '')
|
|
|
|
// Replace dots and spaces with a dash
|
|
|
|
.replace(/(\s|\.)/g, '-')
|
|
|
|
// Convert 2 or more dashes into a single dash
|
|
|
|
.replace(/-+/g, '-')
|
|
|
|
// Make the whole thing lowercase
|
|
|
|
.toLowerCase();
|
|
|
|
|
|
|
|
// Remove trailing hypen
|
|
|
|
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
|
|
|
|
// Check the filtered slug doesn't match any of the reserved keywords
|
2013-09-17 00:17:14 +04:00
|
|
|
slug = /^(ghost|ghost\-admin|admin|wp\-admin|wp\-login|dashboard|logout|login|signin|signup|signout|register|archive|archives|category|categories|tag|tags|page|pages|post|posts|user|users)$/g
|
2013-09-14 23:01:46 +04:00
|
|
|
.test(slug) ? slug + '-post' : slug;
|
|
|
|
|
|
|
|
//if slug is empty after trimming use "post"
|
|
|
|
if (!slug) {
|
|
|
|
slug = "post";
|
|
|
|
}
|
|
|
|
// Test for duplicate slugs.
|
|
|
|
return checkIfSlugExists(slug);
|
2013-07-08 15:39:11 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
}, {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive find all
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
findAll: function (options) {
|
|
|
|
options = options || {};
|
|
|
|
return GhostBookshelf.Collection.forge([], {model: this}).fetch(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
browse: function () {
|
|
|
|
return this.findAll.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive find one where args match
|
|
|
|
* @param args
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
findOne: function (args, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge(args).fetch(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
read: function () {
|
|
|
|
return this.findOne.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive edit
|
|
|
|
* @param editedObj
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
edit: function (editedObj, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge({id: editedObj.id}).fetch(options).then(function (foundObj) {
|
2013-08-09 05:22:49 +04:00
|
|
|
return foundObj.save(editedObj);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function () {
|
|
|
|
return this.edit.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive create
|
2013-08-20 22:52:44 +04:00
|
|
|
* @param newObj
|
2013-06-25 15:43:15 +04:00
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
add: function (newObj, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge(newObj).save(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function () {
|
|
|
|
return this.add.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naive destroy
|
|
|
|
* @param _identifier
|
|
|
|
* @param options (optional)
|
|
|
|
*/
|
|
|
|
destroy: function (_identifier, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge({id: _identifier}).destroy(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
'delete': function () {
|
|
|
|
return this.destroy.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-08-09 05:22:49 +04:00
|
|
|
module.exports = GhostBookshelf;
|