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 19:18:38 +04:00
|
|
|
// # Post Model
|
2014-02-19 17:57:26 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
uuid = require('node-uuid'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-12-21 21:45:30 +03:00
|
|
|
sequence = require('../utils/sequence'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
2014-11-15 19:37:35 +03:00
|
|
|
Showdown = require('showdown-ghost'),
|
2015-03-16 22:30:50 +03:00
|
|
|
converter = new Showdown.converter({extensions: ['ghostgfm', 'footnotes', 'highlight']}),
|
2014-02-19 17:57:26 +04:00
|
|
|
ghostBookshelf = require('./base'),
|
2015-03-24 23:23:23 +03:00
|
|
|
events = require('../events'),
|
2015-06-15 17:38:25 +03:00
|
|
|
config = require('../config'),
|
2015-09-01 03:51:56 +03:00
|
|
|
baseUtils = require('./base/utils'),
|
2014-12-10 17:03:39 +03:00
|
|
|
permalinkSetting = '',
|
|
|
|
getPermalinkSetting,
|
2014-02-19 17:57:26 +04:00
|
|
|
Post,
|
2014-02-22 01:54:56 +04:00
|
|
|
Posts;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
// Stores model permalink format
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
getPermalinkSetting = function getPermalinkSetting(model, attributes, options) {
|
2014-12-10 17:03:39 +03:00
|
|
|
/*jshint unused:false*/
|
|
|
|
|
|
|
|
// Transactions are used for bulk deletes and imports which don't need this anyway
|
|
|
|
if (options.transacting) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2015-06-14 18:58:49 +03:00
|
|
|
return ghostBookshelf.model('Settings').findOne({key: 'permalinks'}).then(function then(response) {
|
2014-12-10 17:03:39 +03:00
|
|
|
if (response) {
|
2015-04-18 00:27:04 +03:00
|
|
|
response = response.toJSON(options);
|
2014-12-10 17:03:39 +03:00
|
|
|
permalinkSetting = response.hasOwnProperty('value') ? response.value : '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Post = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
tableName: 'posts',
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
emitChange: function emitChange(event, usePreviousResourceType) {
|
2015-03-24 23:23:23 +03:00
|
|
|
var resourceType = this.get('page') ? 'page' : 'post';
|
|
|
|
if (usePreviousResourceType) {
|
|
|
|
resourceType = this.updated('page') ? 'page' : 'post';
|
|
|
|
}
|
|
|
|
events.emit(resourceType + '.' + event, this);
|
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
defaults: function defaults() {
|
2013-06-25 15:43:15 +04:00
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 23:01:46 +04:00
|
|
|
status: 'draft'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
initialize: function initialize() {
|
2014-02-19 17:57:26 +04:00
|
|
|
var self = this;
|
2014-04-27 20:58:34 +04:00
|
|
|
|
|
|
|
ghostBookshelf.Model.prototype.initialize.apply(this, arguments);
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
this.on('saved', function onSaved(model, response, options) {
|
2014-12-21 21:45:30 +03:00
|
|
|
return self.updateTags(model, response, options);
|
2014-03-15 02:10:50 +04:00
|
|
|
});
|
2014-10-28 03:41:18 +03:00
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
// Ensures local copy of permalink setting is kept up to date
|
|
|
|
this.on('fetching', getPermalinkSetting);
|
2015-06-27 00:01:02 +03:00
|
|
|
this.on('fetching:collection', getPermalinkSetting);
|
2014-12-10 17:03:39 +03:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
this.on('created', function onCreated(model) {
|
2015-03-24 23:23:23 +03:00
|
|
|
model.emitChange('added');
|
|
|
|
|
|
|
|
if (model.get('status') === 'published') {
|
|
|
|
model.emitChange('published');
|
2014-10-28 03:41:18 +03:00
|
|
|
}
|
|
|
|
});
|
2015-03-24 23:23:23 +03:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
this.on('updated', function onUpdated(model) {
|
2015-03-24 23:23:23 +03:00
|
|
|
model.statusChanging = model.get('status') !== model.updated('status');
|
|
|
|
model.isPublished = model.get('status') === 'published';
|
|
|
|
model.wasPublished = model.updated('status') === 'published';
|
|
|
|
model.resourceTypeChanging = model.get('page') !== model.updated('page');
|
|
|
|
|
|
|
|
// Handle added and deleted for changing resource
|
|
|
|
if (model.resourceTypeChanging) {
|
|
|
|
if (model.wasPublished) {
|
|
|
|
model.emitChange('unpublished', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
model.emitChange('deleted', true);
|
|
|
|
model.emitChange('added');
|
|
|
|
|
|
|
|
if (model.isPublished) {
|
|
|
|
model.emitChange('published');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (model.statusChanging) {
|
|
|
|
model.emitChange(model.isPublished ? 'published' : 'unpublished');
|
|
|
|
} else {
|
|
|
|
if (model.isPublished) {
|
|
|
|
model.emitChange('published.edited');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire edited if this wasn't a change between resourceType
|
|
|
|
model.emitChange('edited');
|
2014-10-28 03:41:18 +03:00
|
|
|
}
|
|
|
|
});
|
2015-03-24 23:23:23 +03:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
this.on('destroyed', function onDestroyed(model) {
|
2015-03-24 23:23:23 +03:00
|
|
|
if (model.previous('status') === 'published') {
|
|
|
|
model.emitChange('unpublished');
|
2014-10-28 03:41:18 +03:00
|
|
|
}
|
2015-03-24 23:23:23 +03:00
|
|
|
model.emitChange('deleted');
|
2014-10-28 03:41:18 +03:00
|
|
|
});
|
2013-08-20 22:52:44 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
saving: function saving(model, attr, options) {
|
2014-03-24 22:08:06 +04:00
|
|
|
var self = this,
|
|
|
|
tagsToCheck,
|
2014-07-15 15:03:12 +04:00
|
|
|
i;
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2014-04-03 17:03:09 +04:00
|
|
|
options = options || {};
|
2014-03-24 22:08:06 +04:00
|
|
|
// keep tags for 'saved' event and deduplicate upper/lowercase tags
|
|
|
|
tagsToCheck = this.get('tags');
|
|
|
|
this.myTags = [];
|
2014-04-27 20:58:34 +04:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
_.each(tagsToCheck, function each(item) {
|
2014-05-23 23:32:14 +04:00
|
|
|
for (i = 0; i < self.myTags.length; i = i + 1) {
|
|
|
|
if (self.myTags[i].name.toLocaleLowerCase() === item.name.toLocaleLowerCase()) {
|
|
|
|
return;
|
2014-03-24 22:08:06 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-23 23:32:14 +04:00
|
|
|
|
|
|
|
self.myTags.push(item);
|
2014-03-24 22:08:06 +04:00
|
|
|
});
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2014-12-21 21:45:30 +03:00
|
|
|
ghostBookshelf.Model.prototype.saving.call(this, model, attr, options);
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
this.set('html', converter.makeHtml(this.get('markdown')));
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-01-07 00:17:20 +04:00
|
|
|
// disabling sanitization until we can implement a better version
|
2014-09-10 08:06:24 +04:00
|
|
|
// this.set('title', this.sanitize('title').trim());
|
2014-01-07 00:17:20 +04:00
|
|
|
this.set('title', this.get('title').trim());
|
2013-08-30 08:18:55 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
if ((this.hasChanged('status') || !this.get('published_at')) && this.get('status') === 'published') {
|
2013-09-17 14:39:27 +04:00
|
|
|
if (!this.get('published_at')) {
|
|
|
|
this.set('published_at', new Date());
|
|
|
|
}
|
2015-02-22 00:04:00 +03:00
|
|
|
|
|
|
|
// unless published_by is set and we're importing, set published_by to contextUser
|
|
|
|
if (!(this.get('published_by') && options.importing)) {
|
2014-08-09 23:16:54 +04:00
|
|
|
this.set('published_by', this.contextUser(options));
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-15 20:11:15 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
if (this.hasChanged('slug') || !this.get('slug')) {
|
2013-09-14 23:01:46 +04:00
|
|
|
// Pass the new slug through the generator to strip illegal characters, detect duplicates
|
2014-02-19 17:57:26 +04:00
|
|
|
return ghostBookshelf.Model.generateSlug(Post, this.get('slug') || this.get('title'),
|
|
|
|
{status: 'all', transacting: options.transacting})
|
2015-06-14 18:58:49 +03:00
|
|
|
.then(function then(slug) {
|
2013-09-14 23:01:46 +04:00
|
|
|
self.set({slug: slug});
|
|
|
|
});
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
creating: function creating(model, attr, options) {
|
2014-04-03 17:03:09 +04:00
|
|
|
options = options || {};
|
2013-06-18 02:59:08 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
// set any dynamic default properties
|
2013-06-25 15:43:15 +04:00
|
|
|
if (!this.get('author_id')) {
|
2014-07-15 15:03:12 +04:00
|
|
|
this.set('author_id', this.contextUser(options));
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-26 05:42:51 +04:00
|
|
|
|
2014-12-21 21:45:30 +03:00
|
|
|
ghostBookshelf.Model.prototype.creating.call(this, model, attr, options);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-12-10 17:03:39 +03:00
|
|
|
/**
|
2014-05-10 08:55:46 +04:00
|
|
|
* ### updateTags
|
|
|
|
* Update tags that are attached to a post. Create any tags that don't already exist.
|
2014-12-21 21:45:30 +03:00
|
|
|
* @param {Object} savedModel
|
|
|
|
* @param {Object} response
|
2014-05-10 08:55:46 +04:00
|
|
|
* @param {Object} options
|
|
|
|
* @return {Promise(ghostBookshelf.Models.Post)} Updated Post model
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
updateTags: function updateTags(savedModel, response, options) {
|
2015-09-01 03:51:56 +03:00
|
|
|
var newTags = this.myTags,
|
|
|
|
TagModel = ghostBookshelf.model('Tag');
|
2013-09-14 00:38:53 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
options = options || {};
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
function doTagUpdates(options) {
|
|
|
|
return Promise.props({
|
|
|
|
currentPost: baseUtils.tagUpdate.fetchCurrentPost(Post, savedModel.id, options),
|
|
|
|
existingTags: baseUtils.tagUpdate.fetchMatchingTags(TagModel, newTags, options)
|
|
|
|
}).then(function fetchedData(results) {
|
|
|
|
var currentTags = results.currentPost.related('tags').toJSON(options),
|
|
|
|
existingTags = results.existingTags ? results.existingTags.toJSON(options) : [],
|
|
|
|
tagOps = [],
|
|
|
|
tagsToRemove,
|
|
|
|
tagsToCreate;
|
|
|
|
|
|
|
|
if (baseUtils.tagUpdate.tagSetsAreEqual(newTags, currentTags)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
// Tags from the current tag array which don't exist in the new tag array should be removed
|
|
|
|
tagsToRemove = _.reject(currentTags, function (currentTag) {
|
|
|
|
if (newTags.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _.any(newTags, function (newTag) {
|
|
|
|
return baseUtils.tagUpdate.tagsAreEqual(currentTag, newTag);
|
|
|
|
});
|
|
|
|
});
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
// Tags from the new tag array which don't exist in the DB should be created
|
|
|
|
tagsToCreate = _.pluck(_.reject(newTags, function (newTag) {
|
2014-05-10 08:55:46 +04:00
|
|
|
return _.any(existingTags, function (existingTag) {
|
2015-09-01 03:51:56 +03:00
|
|
|
return baseUtils.tagUpdate.tagsAreEqual(existingTag, newTag);
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
2015-09-01 03:51:56 +03:00
|
|
|
}), 'name');
|
|
|
|
|
|
|
|
// Remove any tags which don't exist anymore
|
|
|
|
_.each(tagsToRemove, function (tag) {
|
|
|
|
tagOps.push(baseUtils.tagUpdate.detachTagFromPost(savedModel, tag, options));
|
2014-05-10 08:55:46 +04:00
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
// Loop through the new tags and either add them, attach them, or update them
|
|
|
|
_.each(newTags, function (newTag, index) {
|
|
|
|
var tag;
|
|
|
|
|
|
|
|
if (tagsToCreate.indexOf(newTag.name) > -1) {
|
|
|
|
tagOps.push(baseUtils.tagUpdate.createTagThenAttachTagToPost(TagModel, savedModel, newTag, index, options));
|
|
|
|
} else {
|
|
|
|
// try to find a tag on the current post which matches
|
|
|
|
tag = _.find(currentTags, function (currentTag) {
|
|
|
|
return baseUtils.tagUpdate.tagsAreEqual(currentTag, newTag);
|
2014-12-21 21:45:30 +03:00
|
|
|
});
|
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
if (tag) {
|
|
|
|
tagOps.push(baseUtils.tagUpdate.updateTagOrderForPost(savedModel, tag, index, options));
|
|
|
|
return;
|
|
|
|
}
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
// else finally, find the existing tag which matches
|
|
|
|
tag = _.find(existingTags, function (existingTag) {
|
|
|
|
return baseUtils.tagUpdate.tagsAreEqual(existingTag, newTag);
|
|
|
|
});
|
2014-12-21 21:45:30 +03:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
if (tag) {
|
|
|
|
tagOps.push(baseUtils.tagUpdate.attachTagToPost(savedModel, tag, index, options));
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2015-09-01 03:51:56 +03:00
|
|
|
return sequence(tagOps);
|
2014-05-10 08:55:46 +04:00
|
|
|
});
|
2015-09-01 03:51:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle updating tags in a transaction, unless we're already in one
|
|
|
|
if (options.transacting) {
|
|
|
|
return doTagUpdates(options);
|
|
|
|
} else {
|
|
|
|
return ghostBookshelf.transaction(function (t) {
|
|
|
|
options.transacting = t;
|
|
|
|
|
|
|
|
return doTagUpdates(options);
|
|
|
|
}).then(function () {
|
|
|
|
// Don't do anything, the transaction processed ok
|
|
|
|
}).catch(function failure(error) {
|
|
|
|
errors.logError(
|
|
|
|
error,
|
|
|
|
'Unable to save tags.',
|
|
|
|
'Your post was saved, but your tags were not updated.'
|
|
|
|
);
|
|
|
|
return Promise.reject(new errors.InternalServerError(
|
|
|
|
'Unable to save tags. Your post was saved, but your tags were not updated. ' + error
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Relations
|
2015-06-24 12:00:24 +03:00
|
|
|
author: function author() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'author_id');
|
2013-08-21 16:55:58 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
created_by: function createdBy() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'created_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
updated_by: function updatedBy() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'updated_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
published_by: function publishedBy() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'published_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
tags: function tags() {
|
2015-09-01 03:51:56 +03:00
|
|
|
return this.belongsToMany('Tag').withPivot('sort_order').query('orderBy', 'sort_order', 'ASC');
|
2014-02-25 00:28:18 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
fields: function fields() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.morphMany('AppField', 'relatable');
|
2014-04-22 05:08:11 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
toJSON: function toJSON(options) {
|
2015-07-04 21:27:23 +03:00
|
|
|
options = options || {};
|
|
|
|
|
2014-04-22 05:08:11 +04:00
|
|
|
var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
|
|
|
|
|
2015-07-04 21:27:23 +03:00
|
|
|
if (!options.columns || (options.columns && options.columns.indexOf('author') > -1)) {
|
|
|
|
attrs.author = attrs.author || attrs.author_id;
|
|
|
|
delete attrs.author_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.columns || (options.columns && options.columns.indexOf('url') > -1)) {
|
|
|
|
attrs.url = config.urlPathForPost(attrs, permalinkSetting);
|
|
|
|
}
|
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 19:18:38 +04:00
|
|
|
|
2014-04-22 05:08:11 +04:00
|
|
|
return attrs;
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
}, {
|
2015-06-17 16:55:39 +03:00
|
|
|
setupFilters: function setupFilters(options) {
|
|
|
|
var filterObjects = {};
|
|
|
|
// Deliberately switch from singular 'tag' to 'tags' and 'role' to 'roles' here
|
|
|
|
// TODO: make this consistent
|
|
|
|
if (options.tag !== undefined) {
|
|
|
|
filterObjects.tags = ghostBookshelf.model('Tag').forge({slug: options.tag});
|
|
|
|
}
|
|
|
|
if (options.author !== undefined) {
|
|
|
|
filterObjects.author = ghostBookshelf.model('User').forge({slug: options.author});
|
|
|
|
}
|
|
|
|
|
|
|
|
return filterObjects;
|
|
|
|
},
|
|
|
|
|
|
|
|
findPageDefaultOptions: function findPageDefaultOptions() {
|
|
|
|
return {
|
|
|
|
staticPages: false, // include static pages
|
|
|
|
status: 'published',
|
|
|
|
where: {}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
orderDefaultOptions: function orderDefaultOptions() {
|
|
|
|
return {
|
|
|
|
status: 'ASC',
|
|
|
|
published_at: 'DESC',
|
|
|
|
updated_at: 'DESC',
|
|
|
|
id: 'DESC'
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
processOptions: function processOptions(itemCollection, options) {
|
|
|
|
// Step 4: Setup filters (where clauses)
|
|
|
|
if (options.staticPages !== 'all') {
|
|
|
|
// convert string true/false to boolean
|
|
|
|
if (!_.isBoolean(options.staticPages)) {
|
|
|
|
options.staticPages = _.contains(['true', '1'], options.staticPages);
|
|
|
|
}
|
|
|
|
options.where.page = options.staticPages;
|
|
|
|
}
|
|
|
|
|
2015-07-29 04:23:04 +03:00
|
|
|
if (_.has(options, 'featured')) {
|
2015-06-17 16:55:39 +03:00
|
|
|
// convert string true/false to boolean
|
|
|
|
if (!_.isBoolean(options.featured)) {
|
|
|
|
options.featured = _.contains(['true', '1'], options.featured);
|
|
|
|
}
|
|
|
|
options.where.featured = options.featured;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unless `all` is passed as an option, filter on
|
|
|
|
// the status provided.
|
|
|
|
if (options.status !== 'all') {
|
|
|
|
// make sure that status is valid
|
|
|
|
options.status = _.contains(['published', 'draft'], options.status) ? options.status : 'published';
|
|
|
|
options.where.status = options.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-05-06 05:45:08 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
permittedOptions: function permittedOptions(methodName) {
|
2014-05-06 05:45:08 +04:00
|
|
|
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 = {
|
|
|
|
findAll: ['withRelated'],
|
2014-07-15 15:03:12 +04:00
|
|
|
findOne: ['importing', 'withRelated'],
|
2015-07-04 21:27:23 +03:00
|
|
|
findPage: ['page', 'limit', 'columns', 'status', 'staticPages', 'featured'],
|
2014-07-15 15:03:12 +04:00
|
|
|
add: ['importing']
|
2014-05-06 05:45:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (validOptions[methodName]) {
|
|
|
|
options = options.concat(validOptions[methodName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters potentially unsafe model attributes, so you can pass them to Bookshelf / Knex.
|
|
|
|
* @param {Object} data Has keys representing the model's attributes/fields in the database.
|
|
|
|
* @return {Object} The filtered results of the passed in data, containing only what's allowed in the schema.
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
filterData: function filterData(data) {
|
2014-05-06 05:45:08 +04:00
|
|
|
var permittedAttributes = this.prototype.permittedAttributes(),
|
|
|
|
filteredData;
|
|
|
|
|
|
|
|
// manually add 'tags' attribute since it's not in the schema
|
|
|
|
permittedAttributes.push('tags');
|
|
|
|
|
|
|
|
filteredData = _.pick(data, permittedAttributes);
|
|
|
|
|
|
|
|
return filteredData;
|
|
|
|
},
|
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 19:18:38 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// ## Model Data Functions
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find All
|
|
|
|
*
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} options
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
findAll: function findAll(options) {
|
2013-08-23 13:13:32 +04:00
|
|
|
options = options || {};
|
2014-11-12 19:12:01 +03:00
|
|
|
|
|
|
|
// fetch relations passed to options.include
|
2014-11-27 03:28:29 +03:00
|
|
|
options.withRelated = _.union(options.withRelated, options.include);
|
2013-09-23 02:20:08 +04:00
|
|
|
return ghostBookshelf.Model.findAll.call(this, options);
|
2013-08-23 13:13:32 +04:00
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Find One
|
|
|
|
* @extends ghostBookshelf.Model.findOne to handle post status
|
|
|
|
* **See:** [ghostBookshelf.Model.findOne](base.js.html#Find%20One)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
findOne: function findOne(data, 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 19:18:38 +04:00
|
|
|
options = options || {};
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2015-01-07 20:25:32 +03:00
|
|
|
var withNext = _.contains(options.include, 'next'),
|
2015-08-09 22:30:04 +03:00
|
|
|
withPrev = _.contains(options.include, 'previous'),
|
|
|
|
nextRelations = _.transform(options.include, function (relations, include) {
|
|
|
|
if (include === 'next.tags') {
|
|
|
|
relations.push('tags');
|
|
|
|
} else if (include === 'next.author') {
|
|
|
|
relations.push('author');
|
|
|
|
}
|
|
|
|
}, []),
|
|
|
|
prevRelations = _.transform(options.include, function (relations, include) {
|
|
|
|
if (include === 'previous.tags') {
|
|
|
|
relations.push('tags');
|
|
|
|
} else if (include === 'previous.author') {
|
|
|
|
relations.push('author');
|
|
|
|
}
|
|
|
|
}, []);
|
2015-01-07 20:25:32 +03:00
|
|
|
|
2015-06-25 21:56:27 +03:00
|
|
|
data = _.defaults(data || {}, {
|
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 19:18:38 +04:00
|
|
|
status: 'published'
|
2015-06-25 21:56:27 +03:00
|
|
|
});
|
2013-06-09 03:39:24 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (data.status === 'all') {
|
|
|
|
delete data.status;
|
2013-06-01 18:47:41 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 20:25:32 +03:00
|
|
|
// Add related objects, excluding next and previous as they are not real db objects
|
2015-08-09 22:30:04 +03:00
|
|
|
options.withRelated = _.union(options.withRelated, _.pull(
|
|
|
|
[].concat(options.include),
|
|
|
|
'next', 'next.author', 'next.tags', 'previous', 'previous.author', 'previous.tags')
|
|
|
|
);
|
2015-01-07 20:25:32 +03:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return ghostBookshelf.Model.findOne.call(this, data, options).then(function then(post) {
|
2015-01-07 20:25:32 +03:00
|
|
|
if ((withNext || withPrev) && post && !post.page) {
|
2015-07-20 17:38:25 +03:00
|
|
|
var publishedAt = post.get('published_at'),
|
2015-01-07 20:25:32 +03:00
|
|
|
prev,
|
|
|
|
next;
|
|
|
|
|
|
|
|
if (withNext) {
|
2015-06-14 18:58:49 +03:00
|
|
|
next = Post.forge().query(function queryBuilder(qb) {
|
2015-01-07 20:25:32 +03:00
|
|
|
qb.where('status', '=', 'published')
|
|
|
|
.andWhere('page', '=', 0)
|
|
|
|
.andWhere('published_at', '>', publishedAt)
|
|
|
|
.orderBy('published_at', 'asc')
|
|
|
|
.limit(1);
|
2015-08-09 22:30:04 +03:00
|
|
|
}).fetch({withRelated: nextRelations});
|
2015-01-07 20:25:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (withPrev) {
|
2015-06-14 18:58:49 +03:00
|
|
|
prev = Post.forge().query(function queryBuilder(qb) {
|
2015-01-07 20:25:32 +03:00
|
|
|
qb.where('status', '=', 'published')
|
|
|
|
.andWhere('page', '=', 0)
|
|
|
|
.andWhere('published_at', '<', publishedAt)
|
|
|
|
.orderBy('published_at', 'desc')
|
|
|
|
.limit(1);
|
2015-08-09 22:30:04 +03:00
|
|
|
}).fetch({withRelated: prevRelations});
|
2015-01-07 20:25:32 +03: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 19:18:38 +04:00
|
|
|
|
2015-01-07 20:25:32 +03:00
|
|
|
return Promise.join(next, prev)
|
2015-06-14 18:58:49 +03:00
|
|
|
.then(function then(nextAndPrev) {
|
2015-01-07 20:25:32 +03:00
|
|
|
if (nextAndPrev[0]) {
|
|
|
|
post.relations.next = nextAndPrev[0];
|
|
|
|
}
|
|
|
|
if (nextAndPrev[1]) {
|
|
|
|
post.relations.previous = nextAndPrev[1];
|
|
|
|
}
|
|
|
|
return post;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
});
|
2013-09-13 17:29:59 +04: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 19:18:38 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Edit
|
|
|
|
* @extends ghostBookshelf.Model.edit to handle returning the full object and manage _updatedAttributes
|
|
|
|
* **See:** [ghostBookshelf.Model.edit](base.js.html#edit)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
edit: function edit(data, options) {
|
2013-11-03 21:13:19 +04:00
|
|
|
var self = this;
|
2014-04-03 17:03:09 +04:00
|
|
|
options = options || {};
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return ghostBookshelf.Model.edit.call(this, data, options).then(function then(post) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return self.findOne({status: 'all', id: options.id}, options)
|
2015-06-14 18:58:49 +03:00
|
|
|
.then(function then(found) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (found) {
|
|
|
|
// Pass along the updated attributes for checking status changes
|
|
|
|
found._updatedAttributes = post._updatedAttributes;
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
});
|
2013-11-03 21:13:19 +04:00
|
|
|
});
|
|
|
|
},
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add
|
|
|
|
* @extends ghostBookshelf.Model.add to handle returning the full object
|
|
|
|
* **See:** [ghostBookshelf.Model.add](base.js.html#add)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
add: function add(data, options) {
|
2013-11-03 21:13:19 +04:00
|
|
|
var self = this;
|
2014-04-03 17:03:09 +04:00
|
|
|
options = options || {};
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return ghostBookshelf.Model.add.call(this, data, options).then(function then(post) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return self.findOne({status: 'all', id: post.id}, options);
|
2013-09-13 17:29:59 +04:00
|
|
|
});
|
2013-09-27 14:56:20 +04:00
|
|
|
},
|
2014-04-03 17:03:09 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Destroy
|
|
|
|
* @extends ghostBookshelf.Model.destroy to clean up tag relations
|
|
|
|
* **See:** [ghostBookshelf.Model.destroy](base.js.html#destroy)
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
destroy: function destroy(options) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
var id = options.id;
|
|
|
|
options = this.filterOptions(options, 'destroy');
|
2013-09-27 14:56:20 +04:00
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return this.forge({id: id}).fetch({withRelated: ['tags']}).then(function destroyTags(post) {
|
|
|
|
return post.related('tags').detach().then(function destroyPosts() {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return post.destroy(options);
|
|
|
|
});
|
2013-09-27 14:56:20 +04: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 19:18:38 +04:00
|
|
|
},
|
|
|
|
|
2014-07-23 12:32:27 +04:00
|
|
|
/**
|
|
|
|
* ### destroyByAuthor
|
|
|
|
* @param {[type]} options has context and id. Context is the user doing the destroy, id is the user to destroy
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
destroyByAuthor: function destroyByAuthor(options) {
|
2014-07-23 12:32:27 +04:00
|
|
|
var postCollection = Posts.forge(),
|
|
|
|
authorId = options.id;
|
|
|
|
|
|
|
|
options = this.filterOptions(options, 'destroyByAuthor');
|
|
|
|
if (authorId) {
|
2015-06-14 18:58:49 +03:00
|
|
|
return postCollection.query('where', 'author_id', '=', authorId).fetch(options).then(function destroyTags(results) {
|
|
|
|
return Promise.map(results.models, function mapper(post) {
|
|
|
|
return post.related('tags').detach(null, options).then(function destroyPosts() {
|
2014-07-23 12:32:27 +04:00
|
|
|
return post.destroy(options);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, function (error) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.InternalServerError(error.message || error));
|
2014-07-23 12:32:27 +04:00
|
|
|
});
|
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NotFoundError('No user found'));
|
2014-07-23 12:32:27 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
permissible: function permissible(postModelOrId, action, context, loadedPermissions, hasUserPermission, hasAppPermission) {
|
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 19:18:38 +04:00
|
|
|
var self = this,
|
2014-05-14 05:49:07 +04:00
|
|
|
postModel = postModelOrId,
|
|
|
|
origArgs;
|
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 19:18:38 +04:00
|
|
|
|
|
|
|
// If we passed in an id instead of a model, get the model
|
|
|
|
// then check the permissions
|
|
|
|
if (_.isNumber(postModelOrId) || _.isString(postModelOrId)) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Grab the original args without the first one
|
|
|
|
origArgs = _.toArray(arguments).slice(1);
|
|
|
|
// Get the actual post model
|
2015-06-14 18:58:49 +03:00
|
|
|
return this.findOne({id: postModelOrId, status: 'all'}).then(function then(foundPostModel) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Build up the original args but substitute with actual model
|
|
|
|
var newArgs = [foundPostModel].concat(origArgs);
|
|
|
|
|
2014-07-23 22:17:29 +04:00
|
|
|
return self.permissible.apply(self, newArgs);
|
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 19:18:38 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
|
2014-05-14 05:49:07 +04:00
|
|
|
if (postModel) {
|
|
|
|
// If this is the author of the post, allow it.
|
|
|
|
hasUserPermission = hasUserPermission || context.user === postModel.get('author_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUserPermission && hasAppPermission) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve();
|
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 19:18:38 +04:00
|
|
|
}
|
|
|
|
|
2015-08-11 17:03:57 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError('You do not have permission to perform this action'));
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Posts = ghostBookshelf.Collection.extend({
|
2014-12-10 17:03:39 +03:00
|
|
|
model: Post,
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
initialize: function initialize() {
|
2014-12-10 17:03:39 +03:00
|
|
|
ghostBookshelf.Collection.prototype.initialize.apply(this, arguments);
|
|
|
|
|
|
|
|
// Ensures local copy of permalink setting is kept up to date
|
|
|
|
this.on('fetching', getPermalinkSetting);
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
2014-07-13 15:17:18 +04:00
|
|
|
Post: ghostBookshelf.model('Post', Post),
|
|
|
|
Posts: ghostBookshelf.collection('Posts', Posts)
|
2014-02-25 14:18:33 +04:00
|
|
|
};
|