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'),
|
|
|
|
when = require('when'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
2014-02-19 17:57:26 +04:00
|
|
|
Showdown = require('showdown'),
|
2014-03-20 17:52:16 +04:00
|
|
|
ghostgfm = require('../../shared/lib/showdown/extensions/ghostgfm'),
|
|
|
|
converter = new Showdown.converter({extensions: [ghostgfm]}),
|
2014-07-20 20:32:14 +04:00
|
|
|
Tag = require('./tag').Tag,
|
2014-02-19 17:57:26 +04:00
|
|
|
Tags = require('./tag').Tags,
|
2014-07-20 20:32:14 +04:00
|
|
|
User = require('./user').User,
|
2014-02-19 17:57:26 +04:00
|
|
|
ghostBookshelf = require('./base'),
|
2014-03-15 02:10:50 +04:00
|
|
|
xmlrpc = require('../xmlrpc'),
|
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
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Post = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
tableName: 'posts',
|
|
|
|
|
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 23:01:46 +04:00
|
|
|
status: 'draft'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
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);
|
|
|
|
|
2014-03-15 02:10:50 +04:00
|
|
|
this.on('saved', function (model, attributes, options) {
|
|
|
|
if (model.get('status') === 'published') {
|
|
|
|
xmlrpc.ping(model.attributes);
|
|
|
|
}
|
|
|
|
return self.updateTags(model, attributes, options);
|
|
|
|
});
|
2013-08-20 22:52:44 +04:00
|
|
|
},
|
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
saving: function (newPage, attr, options) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
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
|
|
|
|
2014-03-24 22:08:06 +04:00
|
|
|
_.each(tagsToCheck, function (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-04-03 17:03:09 +04:00
|
|
|
ghostBookshelf.Model.prototype.saving.call(this, newPage, 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
|
|
|
|
//this.set('title', this.sanitize('title').trim());
|
|
|
|
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());
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
// This will need to go elsewhere in the API layer.
|
2014-07-15 15:03:12 +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})
|
2013-09-14 23:01:46 +04:00
|
|
|
.then(function (slug) {
|
|
|
|
self.set({slug: slug});
|
|
|
|
});
|
|
|
|
}
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
creating: function (newPage, attr, options) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
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-04-03 17:03:09 +04:00
|
|
|
ghostBookshelf.Model.prototype.creating.call(this, newPage, attr, options);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04: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.
|
|
|
|
* @param {Object} newPost
|
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
|
|
|
* @param {Object} attr
|
2014-05-10 08:55:46 +04:00
|
|
|
* @param {Object} options
|
|
|
|
* @return {Promise(ghostBookshelf.Models.Post)} Updated Post model
|
|
|
|
*/
|
2014-02-19 17:57:26 +04:00
|
|
|
updateTags: function (newPost, attr, options) {
|
2014-02-22 01:54:56 +04:00
|
|
|
var self = this;
|
2013-11-21 00:36:02 +04:00
|
|
|
options = options || {};
|
2013-09-14 00:38:53 +04:00
|
|
|
|
2014-02-22 01:54:56 +04:00
|
|
|
if (!this.myTags) {
|
2013-08-21 16:55:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
return Post.forge({id: newPost.id}).fetch({withRelated: ['tags'], transacting: options.transacting}).then(function (post) {
|
|
|
|
var tagOps = [];
|
2013-09-14 00:38:53 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
// remove all existing tags from the post
|
|
|
|
// _.omit(options, 'query') is a fix for using bookshelf 0.6.8
|
|
|
|
// (https://github.com/tgriesser/bookshelf/issues/294)
|
|
|
|
tagOps.push(post.tags().detach(null, _.omit(options, 'query')));
|
2013-09-14 00:38:53 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
if (_.isEmpty(self.myTags)) {
|
|
|
|
return when.all(tagOps);
|
2013-08-21 16:55:58 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
return Tags.forge().query('whereIn', 'name', _.pluck(self.myTags, 'name')).fetch(options).then(function (existingTags) {
|
|
|
|
var doNotExist = [],
|
|
|
|
createAndAttachOperation;
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
existingTags = existingTags.toJSON();
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
doNotExist = _.reject(self.myTags, function (tag) {
|
|
|
|
return _.any(existingTags, function (existingTag) {
|
|
|
|
return existingTag.name === tag.name;
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
2014-05-10 08:55:46 +04:00
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
// Create tags that don't exist and attach to post
|
|
|
|
_.each(doNotExist, function (tag) {
|
|
|
|
createAndAttachOperation = Tag.add({name: tag.name}, options).then(function (createdTag) {
|
|
|
|
createdTag = createdTag.toJSON();
|
|
|
|
// _.omit(options, 'query') is a fix for using bookshelf 0.6.8
|
|
|
|
// (https://github.com/tgriesser/bookshelf/issues/294)
|
|
|
|
return post.tags().attach(createdTag.id, createdTag.name, _.omit(options, 'query'));
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
tagOps.push(createAndAttachOperation);
|
|
|
|
});
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
// attach the tags that already existed
|
|
|
|
_.each(existingTags, function (tag) {
|
|
|
|
// _.omit(options, 'query') is a fix for using bookshelf 0.6.8
|
|
|
|
// (https://github.com/tgriesser/bookshelf/issues/294)
|
|
|
|
tagOps.push(post.tags().attach(tag.id, _.omit(options, 'query')));
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2014-05-10 08:55:46 +04:00
|
|
|
return when.all(tagOps);
|
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Relations
|
2014-04-27 20:58:34 +04:00
|
|
|
author_id: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'author_id');
|
2013-08-21 16:55:58 +04:00
|
|
|
},
|
|
|
|
|
2014-04-27 20:58:34 +04:00
|
|
|
created_by: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'created_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
updated_by: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'updated_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
published_by: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsTo('User', 'published_by');
|
2014-04-27 20:58:34 +04:00
|
|
|
},
|
|
|
|
|
2013-08-21 16:55:58 +04:00
|
|
|
tags: function () {
|
2014-07-19 17:17:56 +04:00
|
|
|
return this.belongsToMany('Tag');
|
2014-02-25 00:28:18 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
fields: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.morphMany('AppField', 'relatable');
|
2014-04-22 05:08:11 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function (options) {
|
|
|
|
var attrs = ghostBookshelf.Model.prototype.toJSON.call(this, options);
|
|
|
|
|
|
|
|
attrs.author = attrs.author || attrs.author_id;
|
|
|
|
delete attrs.author_id;
|
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
|
|
|
}
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, {
|
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.
|
|
|
|
*/
|
|
|
|
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 = {
|
|
|
|
findAll: ['withRelated'],
|
2014-07-15 15:03:12 +04:00
|
|
|
findOne: ['importing', 'withRelated'],
|
2014-05-06 05:45:08 +04:00
|
|
|
findPage: ['page', 'limit', 'status', 'staticPages'],
|
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.
|
|
|
|
*/
|
|
|
|
filterData: function (data) {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2013-08-23 13:13:32 +04:00
|
|
|
findAll: function (options) {
|
|
|
|
options = options || {};
|
2014-04-27 20:58:34 +04:00
|
|
|
options.withRelated = _.union([ 'tags', 'fields' ], 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
|
|
|
/**
|
|
|
|
* #### findPage
|
|
|
|
* Find results by page - returns an object containing the
|
|
|
|
* information about the request (page, limit), along with the
|
|
|
|
* info needed for pagination (pages, total).
|
|
|
|
*
|
|
|
|
* **response:**
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* posts: [
|
|
|
|
* {...}, {...}, {...}
|
|
|
|
* ],
|
|
|
|
* page: __,
|
|
|
|
* limit: __,
|
|
|
|
* pages: __,
|
|
|
|
* total: __
|
|
|
|
* }
|
|
|
|
*
|
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
|
|
|
* @params {Object} options
|
2013-06-25 15:43:15 +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
|
|
|
findPage: function (options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
var postCollection = Posts.forge(),
|
2014-07-20 20:32:14 +04:00
|
|
|
tagInstance = options.tag !== undefined ? Tag.forge({slug: options.tag}) : false,
|
|
|
|
authorInstance = options.author !== undefined ? User.forge({slug: options.author}) : false;
|
2013-12-20 02:51:28 +04:00
|
|
|
|
2014-06-20 16:28:01 +04:00
|
|
|
if (options.limit) {
|
|
|
|
options.limit = parseInt(options.limit) || 15;
|
|
|
|
}
|
|
|
|
|
2014-05-06 05:45:08 +04:00
|
|
|
options = this.filterOptions(options, 'findPage');
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// Set default settings for 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 = _.extend({
|
2013-12-20 02:51:28 +04:00
|
|
|
page: 1, // pagination page
|
2013-06-25 15:43:15 +04:00
|
|
|
limit: 15,
|
2013-12-20 02:51:28 +04:00
|
|
|
staticPages: false, // include static pages
|
2014-02-13 07:26:56 +04:00
|
|
|
status: 'published',
|
|
|
|
where: {}
|
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);
|
2013-06-01 23:30:42 +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
|
|
|
if (options.staticPages !== 'all') {
|
2013-12-20 02:51:28 +04:00
|
|
|
// convert string true/false to boolean
|
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 (!_.isBoolean(options.staticPages)) {
|
|
|
|
options.staticPages = options.staticPages === 'true' || options.staticPages === '1' ? true : false;
|
2013-12-20 02:51:28 +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
|
|
|
options.where.page = options.staticPages;
|
2013-10-17 09:21:56 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Unless `all` is passed as an option, filter on
|
|
|
|
// the status provided.
|
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 (options.status !== 'all') {
|
2013-12-20 02:51:28 +04:00
|
|
|
// make sure that status is valid
|
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.status = _.indexOf(['published', 'draft'], options.status) !== -1 ? options.status : 'published';
|
|
|
|
options.where.status = options.status;
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// If there are where conditionals specified, add those
|
|
|
|
// to the query.
|
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 (options.where) {
|
|
|
|
postCollection.query('where', options.where);
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2014-04-27 20:58:34 +04:00
|
|
|
// Add related objects
|
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.withRelated = _.union([ 'tags', 'fields' ], options.include);
|
2013-08-23 13:13:32 +04:00
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// If a query param for a tag is attached
|
|
|
|
// we need to fetch the tag model to find its id
|
|
|
|
function fetchTagQuery() {
|
|
|
|
if (tagInstance) {
|
|
|
|
return tagInstance.fetch();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-20 20:32:14 +04:00
|
|
|
function fetchAuthorQuery() {
|
|
|
|
if (authorInstance) {
|
|
|
|
return authorInstance.fetch();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return when.join(fetchTagQuery(), fetchAuthorQuery())
|
2014-02-13 07:26:56 +04:00
|
|
|
|
|
|
|
// Set the limit & offset for the query, fetching
|
|
|
|
// with the opts (to specify any eager relations, etc.)
|
|
|
|
// Omitting the `page`, `limit`, `where` just to be sure
|
|
|
|
// aren't used for other purposes.
|
|
|
|
.then(function () {
|
|
|
|
// If we have a tag instance we need to modify our query.
|
|
|
|
// We need to ensure we only select posts that contain
|
|
|
|
// the tag given in the query param.
|
|
|
|
if (tagInstance) {
|
|
|
|
postCollection
|
|
|
|
.query('join', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id')
|
|
|
|
.query('where', 'posts_tags.tag_id', '=', tagInstance.id);
|
|
|
|
}
|
2014-07-20 20:32:14 +04:00
|
|
|
|
|
|
|
if (authorInstance) {
|
|
|
|
postCollection
|
|
|
|
.query('where', 'author_id', '=', authorInstance.id);
|
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
return postCollection
|
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
|
|
|
.query('limit', options.limit)
|
|
|
|
.query('offset', options.limit * (options.page - 1))
|
2014-02-13 07:26:56 +04:00
|
|
|
.query('orderBy', 'status', 'ASC')
|
|
|
|
.query('orderBy', 'published_at', 'DESC')
|
|
|
|
.query('orderBy', 'updated_at', 'DESC')
|
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
|
|
|
.fetch(_.omit(options, 'page', 'limit'));
|
2014-02-13 07:26:56 +04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Fetch pagination information
|
|
|
|
.then(function () {
|
|
|
|
var qb,
|
|
|
|
tableName = _.result(postCollection, 'tableName'),
|
|
|
|
idAttribute = _.result(postCollection, 'idAttribute');
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// After we're done, we need to figure out what
|
|
|
|
// the limits are for the pagination values.
|
2014-02-13 07:26:56 +04:00
|
|
|
qb = ghostBookshelf.knex(tableName);
|
2013-06-25 15:43:15 +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
|
|
|
if (options.where) {
|
|
|
|
qb.where(options.where);
|
2013-06-09 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
if (tagInstance) {
|
|
|
|
qb.join('posts_tags', 'posts_tags.post_id', '=', 'posts.id');
|
|
|
|
qb.where('posts_tags.tag_id', '=', tagInstance.id);
|
|
|
|
}
|
2014-07-20 20:32:14 +04:00
|
|
|
if (authorInstance) {
|
|
|
|
qb.where('author_id', '=', authorInstance.id);
|
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
|
|
|
|
return qb.count(tableName + '.' + idAttribute + ' as aggregate');
|
|
|
|
})
|
|
|
|
|
|
|
|
// Format response of data
|
|
|
|
.then(function (resp) {
|
|
|
|
var totalPosts = parseInt(resp[0].aggregate, 10),
|
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
|
|
|
calcPages = Math.ceil(totalPosts / options.limit),
|
2014-04-19 19:03:20 +04:00
|
|
|
pagination = {},
|
|
|
|
meta = {},
|
|
|
|
data = {};
|
|
|
|
|
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
|
|
|
pagination.page = parseInt(options.page, 10);
|
|
|
|
pagination.limit = options.limit;
|
|
|
|
pagination.pages = calcPages === 0 ? 1 : calcPages;
|
|
|
|
pagination.total = totalPosts;
|
|
|
|
pagination.next = null;
|
|
|
|
pagination.prev = null;
|
2014-04-19 19:03:20 +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
|
|
|
// Pass include to each model so that toJSON works correctly
|
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 (options.include) {
|
2014-04-27 20:58:34 +04:00
|
|
|
_.each(postCollection.models, function (item) {
|
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
|
|
|
item.include = options.include;
|
2014-04-27 20:58:34 +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
|
|
|
data.posts = postCollection.toJSON();
|
|
|
|
data.meta = meta;
|
|
|
|
meta.pagination = pagination;
|
2014-04-19 19:03:20 +04:00
|
|
|
|
|
|
|
if (pagination.pages > 1) {
|
|
|
|
if (pagination.page === 1) {
|
|
|
|
pagination.next = pagination.page + 1;
|
|
|
|
} else if (pagination.page === pagination.pages) {
|
|
|
|
pagination.prev = pagination.page - 1;
|
2014-02-13 07:26:56 +04:00
|
|
|
} else {
|
2014-04-19 19:03:20 +04:00
|
|
|
pagination.next = pagination.page + 1;
|
|
|
|
pagination.prev = pagination.page - 1;
|
2013-06-25 19:13:19 +04:00
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tagInstance) {
|
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
|
|
|
meta.filters = {};
|
2014-04-27 21:34:10 +04:00
|
|
|
if (!tagInstance.isNew()) {
|
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
|
|
|
meta.filters.tags = [tagInstance.toJSON()];
|
2014-04-27 21:34:10 +04:00
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 20:32:14 +04:00
|
|
|
if (authorInstance) {
|
|
|
|
meta.filters = {};
|
|
|
|
if (!authorInstance.isNew()) {
|
|
|
|
meta.filters.author = authorInstance.toJSON();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
return data;
|
|
|
|
})
|
|
|
|
.catch(errors.logAndThrowError);
|
2013-06-25 15:43:15 +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)
|
|
|
|
*/
|
|
|
|
findOne: function (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
|
|
|
|
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
|
|
|
data = _.extend({
|
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'
|
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
|
|
|
}, data || {});
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
// Add related objects
|
|
|
|
options.withRelated = _.union([ 'tags', 'fields' ], options.include);
|
|
|
|
|
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 ghostBookshelf.Model.findOne.call(this, data, options);
|
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)
|
|
|
|
*/
|
|
|
|
edit: function (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
|
|
|
|
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 ghostBookshelf.Model.edit.call(this, data, options).then(function (post) {
|
|
|
|
return self.findOne({status: 'all', id: options.id}, options)
|
|
|
|
.then(function (found) {
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
add: function (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
|
|
|
|
|
|
|
return ghostBookshelf.Model.add.call(this, data, options).then(function (post) {
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
destroy: function (options) {
|
|
|
|
var id = options.id;
|
|
|
|
options = this.filterOptions(options, 'destroy');
|
2013-09-27 14:56:20 +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
|
|
|
return this.forge({id: id}).fetch({withRelated: ['tags']}).then(function destroyTagsAndPost(post) {
|
|
|
|
return post.related('tags').detach().then(function () {
|
|
|
|
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-05-14 05:49:07 +04:00
|
|
|
permissable: function (postModelOrId, 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
|
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
|
|
|
return this.findOne({id: postModelOrId, status: 'all'}).then(function (foundPostModel) {
|
2014-05-14 05:49:07 +04:00
|
|
|
// Build up the original args but substitute with actual model
|
|
|
|
var newArgs = [foundPostModel].concat(origArgs);
|
|
|
|
|
|
|
|
return self.permissable.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) {
|
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
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return when.reject();
|
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({
|
2013-06-25 15:43:15 +04:00
|
|
|
model: Post
|
|
|
|
});
|
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
|
|
|
};
|