Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
// # Base Model
|
|
|
|
// This is the model from which all other Ghost models extend. The model is based on Bookshelf.Model, and provides
|
|
|
|
// several basic behaviours such as UUIDs, as well as a set of Data methods for accessing information from the database.
|
|
|
|
//
|
|
|
|
// The models are internal to Ghost, only the API and some internal functions such as migration and import/export
|
|
|
|
// accesses the models directly. All other parts of Ghost, including the blog frontend, admin UI, and apps are only
|
|
|
|
// allowed to access data via the API.
|
2014-04-27 11:58:34 -05:00
|
|
|
var Bookshelf = require('bookshelf'),
|
|
|
|
when = require('when'),
|
|
|
|
moment = require('moment'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
config = require('../config'),
|
|
|
|
unidecode = require('unidecode'),
|
|
|
|
sanitize = require('validator').sanitize,
|
|
|
|
schema = require('../data/schema'),
|
|
|
|
validation = require('../data/validation'),
|
2014-02-19 08:57:26 -05:00
|
|
|
|
|
|
|
ghostBookshelf;
|
2013-06-25 06:43:15 -05:00
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
// ### ghostBookshelf
|
|
|
|
// Initializes a new Bookshelf instance called ghostBookshelf, for reference elsewhere in Ghost.
|
2013-11-25 22:22:59 -05:00
|
|
|
ghostBookshelf = Bookshelf.ghost = Bookshelf.initialize(config().database);
|
|
|
|
ghostBookshelf.client = config().database.client;
|
2013-06-25 06:43:15 -05:00
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
// ### ghostBookshelf.Model
|
2013-06-25 06:43:15 -05:00
|
|
|
// The Base Model which other Ghost objects will inherit from,
|
|
|
|
// including some convenience functions as static properties on the model.
|
2013-09-22 17:20:08 -05:00
|
|
|
ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
2013-06-25 06:43:15 -05:00
|
|
|
|
2013-09-14 14:01:46 -05:00
|
|
|
hasTimestamps: true,
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
// Get permitted attributes from server/data/schema.js, which is where the DB schema is defined
|
2014-02-19 08:57:26 -05:00
|
|
|
permittedAttributes: function () {
|
|
|
|
return _.keys(schema.tables[this.tableName]);
|
|
|
|
},
|
|
|
|
|
2013-09-14 14:01:46 -05:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
2014-04-27 11:58:34 -05:00
|
|
|
var self = this,
|
|
|
|
options = arguments[1] || {};
|
|
|
|
|
|
|
|
// make options include available for toJSON()
|
|
|
|
if (options.include) {
|
|
|
|
this.include = _.clone(options.include);
|
|
|
|
}
|
|
|
|
|
2013-09-14 14:01:46 -05:00
|
|
|
this.on('creating', this.creating, this);
|
2014-02-19 08:57:26 -05:00
|
|
|
this.on('saving', function (model, attributes, options) {
|
|
|
|
return when(self.saving(model, attributes, options)).then(function () {
|
|
|
|
return self.validate(model, attributes, options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function () {
|
2014-05-05 08:51:21 -05:00
|
|
|
return validation.validateSchema(this.tableName, this.toJSON());
|
2013-09-14 14:01:46 -05:00
|
|
|
},
|
|
|
|
|
2014-04-03 08:03:09 -05:00
|
|
|
creating: function (newObj, attr, options) {
|
2013-09-14 14:01:46 -05:00
|
|
|
if (!this.get('created_by')) {
|
2014-04-03 08:03:09 -05:00
|
|
|
this.set('created_by', options.user);
|
2013-09-14 14:01:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-04-03 08:03:09 -05:00
|
|
|
saving: function (newObj, attr, options) {
|
|
|
|
// Remove any properties which don't belong on the model
|
2014-02-19 08:57:26 -05:00
|
|
|
this.attributes = this.pick(this.permittedAttributes());
|
2014-04-21 20:04:30 -05:00
|
|
|
// Store the previous attributes so we can tell what was updated later
|
|
|
|
this._updatedAttributes = newObj.previousAttributes();
|
|
|
|
|
2014-04-03 08:03:09 -05:00
|
|
|
this.set('updated_by', options.user);
|
2013-09-14 14:01:46 -05:00
|
|
|
},
|
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
// Base prototype properties will go here
|
2013-07-08 06:39:11 -05:00
|
|
|
// Fix problems with dates
|
|
|
|
fixDates: function (attrs) {
|
|
|
|
_.each(attrs, function (value, key) {
|
|
|
|
if (key.substr(-3) === '_at' && value !== null) {
|
2013-08-16 09:11:36 -05:00
|
|
|
attrs[key] = moment(attrs[key]).toDate();
|
2013-07-08 06:39:11 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
2014-04-25 02:55:53 -05:00
|
|
|
// Convert integers to real booleans
|
2014-01-04 17:16:29 -05:00
|
|
|
fixBools: function (attrs) {
|
2014-04-25 02:55:53 -05:00
|
|
|
var self = this;
|
2014-01-04 17:16:29 -05:00
|
|
|
_.each(attrs, function (value, key) {
|
2014-04-25 02:55:53 -05:00
|
|
|
if (schema.tables[self.tableName][key].type === "bool") {
|
|
|
|
attrs[key] = value ? true : false;
|
2014-01-04 17:16:29 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
},
|
|
|
|
|
2014-04-25 02:55:53 -05:00
|
|
|
// format date before writing to DB, bools work
|
2013-07-08 06:39:11 -05:00
|
|
|
format: function (attrs) {
|
2014-04-25 02:55:53 -05:00
|
|
|
return this.fixDates(attrs);
|
|
|
|
},
|
|
|
|
|
|
|
|
// format data and bool when fetching from DB
|
|
|
|
parse: function (attrs) {
|
2014-01-04 17:16:29 -05:00
|
|
|
return this.fixBools(this.fixDates(attrs));
|
2013-07-08 06:39:11 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function (options) {
|
2014-04-25 02:55:53 -05:00
|
|
|
var attrs = _.extend({}, this.attributes),
|
2014-04-27 11:58:34 -05:00
|
|
|
self = this;
|
2013-07-08 06:39:11 -05:00
|
|
|
|
|
|
|
if (options && options.shallow) {
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:58:34 -05:00
|
|
|
if (options && options.idOnly) {
|
|
|
|
return attrs.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(this.relations, function (relation, key) {
|
2013-09-24 05:46:30 -05:00
|
|
|
if (key.substring(0, 7) !== '_pivot_') {
|
2014-04-27 11:58:34 -05:00
|
|
|
// if include is set, expand to full object
|
|
|
|
// toMany relationships are included with ids if not expanded
|
|
|
|
if (_.contains(self.include, key)) {
|
|
|
|
attrs[key] = relation.toJSON();
|
|
|
|
} else if (relation.hasOwnProperty('length')) {
|
|
|
|
attrs[key] = relation.toJSON({idOnly: true});
|
|
|
|
}
|
2013-08-21 07:55:58 -05:00
|
|
|
}
|
2013-07-08 06:39:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return attrs;
|
2013-09-14 14:01:46 -05:00
|
|
|
},
|
|
|
|
|
2013-10-09 13:11:29 -05:00
|
|
|
sanitize: function (attr) {
|
|
|
|
return sanitize(this.get(attr)).xss();
|
2014-04-21 20:04:30 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get attributes that have been updated (values before a .save() call)
|
|
|
|
updatedAttributes: function () {
|
|
|
|
return this._updatedAttributes || {};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get a specific updated attribute value
|
|
|
|
updated: function (attr) {
|
|
|
|
return this.updatedAttributes()[attr];
|
2013-07-08 06:39:11 -05:00
|
|
|
}
|
2013-06-25 06:43:15 -05:00
|
|
|
|
|
|
|
}, {
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
// ## Model Data Functions
|
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* ### Find All
|
|
|
|
* Naive find all fetches all the data for a particular model
|
2014-04-21 13:04:20 -05:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @return {Promise(ghostBookshelf.Collection)} Collection of all Models
|
2013-06-25 06:43:15 -05:00
|
|
|
*/
|
|
|
|
findAll: function (options) {
|
|
|
|
options = options || {};
|
2014-04-27 11:58:34 -05:00
|
|
|
return ghostBookshelf.Collection.forge([], {model: this}).fetch(options).then(function (result) {
|
|
|
|
if (options.include) {
|
|
|
|
_.each(result.models, function (item) {
|
|
|
|
item.include = options.include;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
2013-06-25 06:43:15 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* ### Find One
|
|
|
|
* Naive find one where data determines what to match on
|
|
|
|
* @param {Object} data
|
2014-04-21 13:04:20 -05:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Single Model
|
2013-06-25 06:43:15 -05:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
findOne: function (data, options) {
|
2013-06-25 06:43:15 -05:00
|
|
|
options = options || {};
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return this.forge(data, {include: options.include}).fetch(options);
|
2013-06-25 06:43:15 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* ### Edit
|
2013-06-25 06:43:15 -05:00
|
|
|
* Naive edit
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @param {Object} data
|
2014-04-21 13:04:20 -05:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Edited Model
|
2013-06-25 06:43:15 -05:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
edit: function (data, options) {
|
2013-06-25 06:43:15 -05:00
|
|
|
options = options || {};
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return this.forge({id: data.id}).fetch(options).then(function (object) {
|
|
|
|
if (object) {
|
|
|
|
return object.save(data, options);
|
2014-04-16 05:09:03 -05:00
|
|
|
}
|
2013-06-25 06:43:15 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* ### Add
|
|
|
|
* Naive add
|
|
|
|
* @param {Object} data
|
2014-04-21 13:04:20 -05:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Newly Added Model
|
2013-06-25 06:43:15 -05:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
add: function (data, options) {
|
2013-06-25 06:43:15 -05:00
|
|
|
options = options || {};
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
var instance = this.forge(data);
|
2013-12-25 22:48:16 -05:00
|
|
|
// We allow you to disable timestamps
|
|
|
|
// when importing posts so that
|
|
|
|
// the new posts `updated_at` value
|
|
|
|
// is the same as the import json blob.
|
|
|
|
// More details refer to https://github.com/TryGhost/Ghost/issues/1696
|
|
|
|
if (options.importing) {
|
|
|
|
instance.hasTimestamps = false;
|
|
|
|
}
|
|
|
|
return instance.save(null, options);
|
2013-06-25 06:43:15 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* ### Destroy
|
2013-06-25 06:43:15 -05:00
|
|
|
* Naive destroy
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @param {Object} data
|
2014-04-21 13:04:20 -05:00
|
|
|
* @param {Object} options (optional)
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
* @return {Promise(ghostBookshelf.Model)} Empty Model
|
2013-06-25 06:43:15 -05:00
|
|
|
*/
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
destroy: function (data, options) {
|
2013-06-25 06:43:15 -05:00
|
|
|
options = options || {};
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return this.forge({id: data}).destroy(options);
|
2013-06-25 06:43:15 -05:00
|
|
|
},
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
/**
|
|
|
|
* ### Generate Slug
|
|
|
|
* Create a string to act as the permalink for an object.
|
|
|
|
* @param {ghostBookshelf.Model} Model Model type to generate a slug for
|
|
|
|
* @param {String} base The string for which to generate a slug, usually a title or name
|
|
|
|
* @param {Object} options Options to pass to findOne
|
|
|
|
* @return {Promise(String)} Resolves to a unique slug string
|
|
|
|
*/
|
|
|
|
generateSlug: function (Model, base, options) {
|
2013-12-20 08:36:00 -05:00
|
|
|
var slug,
|
|
|
|
slugTryCount = 1,
|
2014-03-23 13:52:25 -05:00
|
|
|
baseName = Model.prototype.tableName.replace(/s$/, ''),
|
2014-01-05 12:55:59 -05:00
|
|
|
// Look for a post with a matching slug, append an incrementing number if so
|
2014-03-23 13:52:25 -05:00
|
|
|
checkIfSlugExists;
|
2013-12-20 08:36:00 -05:00
|
|
|
|
2014-03-23 13:52:25 -05:00
|
|
|
checkIfSlugExists = function (slugToFind) {
|
|
|
|
var args = {slug: slugToFind};
|
|
|
|
//status is needed for posts
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
if (options && options.status) {
|
|
|
|
args.status = options.status;
|
2014-03-23 13:52:25 -05:00
|
|
|
}
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return Model.findOne(args, options).then(function (found) {
|
2014-03-23 13:52:25 -05:00
|
|
|
var trimSpace;
|
2013-12-20 08:36:00 -05:00
|
|
|
|
2014-03-23 13:52:25 -05:00
|
|
|
if (!found) {
|
|
|
|
return when.resolve(slugToFind);
|
|
|
|
}
|
2013-12-20 08:36:00 -05:00
|
|
|
|
2014-03-23 13:52:25 -05:00
|
|
|
slugTryCount += 1;
|
2013-12-20 08:36:00 -05:00
|
|
|
|
2014-03-23 13:52:25 -05:00
|
|
|
// 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);
|
|
|
|
}
|
2013-12-20 08:36:00 -05:00
|
|
|
|
2014-03-23 13:52:25 -05:00
|
|
|
slugToFind += slugTryCount;
|
|
|
|
|
|
|
|
return checkIfSlugExists(slugToFind);
|
|
|
|
});
|
|
|
|
};
|
2013-12-20 08:36:00 -05:00
|
|
|
|
2014-01-25 23:32:08 -05:00
|
|
|
slug = base.trim();
|
|
|
|
|
|
|
|
// Remove non ascii characters
|
|
|
|
slug = unidecode(slug);
|
|
|
|
|
2013-12-20 08:36:00 -05:00
|
|
|
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
|
2014-01-25 23:32:08 -05:00
|
|
|
slug = slug.replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '')
|
2013-12-20 08:36:00 -05:00
|
|
|
// 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 hyphen
|
|
|
|
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
|
2014-01-25 23:32:08 -05:00
|
|
|
|
2013-12-20 08:36:00 -05:00
|
|
|
// Check the filtered slug doesn't match any of the reserved keywords
|
2014-04-23 16:19:44 -05: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|public|user|users|rss|feed)$/g
|
2014-03-23 13:52:25 -05:00
|
|
|
.test(slug) ? slug + '-' + baseName : slug;
|
2013-12-20 08:36:00 -05:00
|
|
|
|
|
|
|
//if slug is empty after trimming use "post"
|
|
|
|
if (!slug) {
|
2014-03-23 13:52:25 -05:00
|
|
|
slug = baseName;
|
2013-12-20 08:36:00 -05:00
|
|
|
}
|
|
|
|
// Test for duplicate slugs.
|
|
|
|
return checkIfSlugExists(slug);
|
2013-06-25 06:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
// Export ghostBookshelf for use elsewhere
|
2013-09-22 17:20:08 -05:00
|
|
|
module.exports = ghostBookshelf;
|