Skip to content

Commit

Permalink
Implementing multiple changes in API posts.browse method
Browse files Browse the repository at this point in the history
    Closes TryGhost#5152, TryGhost#5093, TryGhost#5151
    - Adds `featured` filter option to posts.browse method modifying the model to take it too
    - Implements offset pagination method to posts.browse method modifying the model to work with it too but still supporting the `page` parameter
    - Removes `staticPages` parameter in options filter to allow using the `page` parameter with options `all` `true` or `false
  • Loading branch information
edsadr committed Apr 22, 2015
1 parent ede50f3 commit 6627951
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 42 deletions.
3 changes: 1 addition & 2 deletions core/client/app/routes/editor/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var EditorEditRoute = AuthenticatedRoute.extend(base, {

query = {
id: postId,
status: 'all',
staticPages: 'all'
status: 'all'
};

return self.store.find('post', query).then(function (records) {
Expand Down
1 change: 0 additions & 1 deletion core/client/app/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var paginationSettings,

paginationSettings = {
status: 'all',
staticPages: 'all',
page: 1
};

Expand Down
3 changes: 1 addition & 2 deletions core/client/app/routes/posts/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ var PostsPostRoute = AuthenticatedRoute.extend(loadingIndicator, ShortcutsRoute,

query = {
id: postId,
status: 'all',
staticPages: 'all'
status: 'all'
};

return self.store.find('post', query).then(function (records) {
Expand Down
4 changes: 2 additions & 2 deletions core/server/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var Promise = require('bluebird'),
utils = require('./utils'),

docName = 'posts',
allowedIncludes = ['created_by', 'updated_by', 'published_by', 'author', 'tags', 'fields', 'next', 'previous'],
allowedIncludes = ['created_by', 'updated_by', 'published_by', 'author', 'tags', 'fields', 'next', 'previous', 'featured', 'page', 'limit', 'offset'],
posts;

// ## Helpers
Expand Down Expand Up @@ -45,7 +45,7 @@ posts = {
* Can return posts for a particular tag by passing a tag slug in
*
* @public
* @param {{context, page, limit, status, staticPages, tag}} options (optional)
* @param {{context, page[integer for page number, boolean for filtering static pages], limit, status, tag}} options (optional)
* @returns {Promise(Posts)} Posts Collection with Meta
*/
browse: function browse(options) {
Expand Down
2 changes: 1 addition & 1 deletion core/server/data/xml/sitemap/page-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _.extend(PageMapGenerator.prototype, {
internal: true
},
status: 'published',
staticPages: true,
page: true,
limit: 'all'
}).then(function (resp) {
var homePage = {
Expand Down
2 changes: 1 addition & 1 deletion core/server/data/xml/sitemap/post-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _.extend(PostMapGenerator.prototype, {
internal: true
},
status: 'published',
staticPages: false,
page: false,
limit: 'all'
}).then(function (resp) {
return resp.posts;
Expand Down
49 changes: 32 additions & 17 deletions core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Post = ghostBookshelf.Model.extend({
validOptions = {
findAll: ['withRelated'],
findOne: ['importing', 'withRelated'],
findPage: ['page', 'limit', 'status', 'staticPages'],
findPage: ['page', 'limit', 'offset', 'status', 'featured'],
add: ['importing']
};

Expand Down Expand Up @@ -358,31 +358,42 @@ Post = ghostBookshelf.Model.extend({
var tagInstance = options.tag !== undefined ? ghostBookshelf.model('Tag').forge({slug: options.tag}) : false,
authorInstance = options.author !== undefined ? ghostBookshelf.model('User').forge({slug: options.author}) : false;

if (options.limit && options.limit !== 'all') {
options.limit = parseInt(options.limit, 10) || 15;
if (options.limit) {
if (options.limit !== 'all') {
options.limit = parseInt(options.limit, 10) || 15;
}
} else {
options.limit = 15;
}

if (options.offset && options.limit !== 'all') {
options.offset = parseInt(options.offset, 10) || 0;
}

if (options.page) {
options.page = parseInt(options.page, 10) || 1;
if (options.page !== undefined && !_.isBoolean(options.page) && options.limit !== 'all') {
if (options.page === 'true' || options.page === 'false') {
options.page = options.page === 'true' ? true : false;
} else if (options.page !== 'all') {
options.page = parseInt(options.page, 10) || 1;
options.offset = options.limit * (options.page - 1);
options.offset = isNaN(options.offset) ? 0 : options.offset;
options.page = false;
}
}

options = this.filterOptions(options, 'findPage');

// Set default settings for options
options = _.extend({
page: 1, // pagination page
offset: 0, // pagination page
limit: 15,
staticPages: false, // include static pages
page: false,
status: 'published',
where: {}
}, options);

if (options.staticPages !== 'all') {
// convert string true/false to boolean
if (!_.isBoolean(options.staticPages)) {
options.staticPages = options.staticPages === 'true' || options.staticPages === '1' ? true : false;
}
options.where.page = options.staticPages;
if (_.isBoolean(options.page)) {
options.where.page = options.page;
}

// Unless `all` is passed as an option, filter on
Expand All @@ -393,6 +404,10 @@ Post = ghostBookshelf.Model.extend({
options.where.status = options.status;
}

if (options.featured && _.isBoolean(options.featured)) {
options.where.featured = options.featured;
}

// Add related objects
options.withRelated = _.union(options.withRelated, options.include);

Expand All @@ -415,7 +430,7 @@ Post = ghostBookshelf.Model.extend({
return Promise.join(fetchTagQuery(), fetchAuthorQuery())
// 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
// Omitting the `offset`, `limit`, `where` just to be sure
// aren't used for other purposes.
.then(function () {
var postCollection = Posts.forge(),
Expand Down Expand Up @@ -445,15 +460,15 @@ Post = ghostBookshelf.Model.extend({
if (_.isNumber(options.limit)) {
postCollection
.query('limit', options.limit)
.query('offset', options.limit * (options.page - 1));
.query('offset', options.offset);
}

collectionPromise = postCollection
.query('orderBy', 'status', 'ASC')
.query('orderBy', 'published_at', 'DESC')
.query('orderBy', 'updated_at', 'DESC')
.query('orderBy', 'id', 'DESC')
.fetch(_.omit(options, 'page', 'limit'));
.fetch(_.omit(options, 'offset', 'limit'));

// Find the total number of posts

Expand Down Expand Up @@ -482,7 +497,7 @@ Post = ghostBookshelf.Model.extend({
meta = {},
data = {};

pagination.page = options.page;
pagination.page = options.limit === 'all' ? 1 : (options.offset / options.limit) + 1;
pagination.limit = options.limit;
pagination.pages = calcPages === 0 ? 1 : calcPages;
pagination.total = totalPosts;
Expand Down
2 changes: 1 addition & 1 deletion core/test/functional/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ CasperTest.Routines = (function () {
casper.captureScreenshot('signing_in2.png');
});

casper.waitForResource(/posts\/\?status=all&staticPages=all/, function then() {
casper.waitForResource(/posts\/\?status=all/, function then() {
casper.captureScreenshot('signing_in.png');
}, function timeout() {
casper.test.fail('Unable to signin and load admin panel');
Expand Down
6 changes: 3 additions & 3 deletions core/test/functional/routes/api/posts_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Post API', function () {
});

it('can retrieve all published posts and pages', function (done) {
request.get(testUtils.API.getApiQuery('posts/?staticPages=all'))
request.get(testUtils.API.getApiQuery('posts/?page=all'))
.set('Authorization', 'Bearer ' + accesstoken)
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules['private'])
Expand All @@ -84,7 +84,7 @@ describe('Post API', function () {
// Test bits of the API we don't use in the app yet to ensure the API behaves properly

it('can retrieve all status posts and pages', function (done) {
request.get(testUtils.API.getApiQuery('posts/?staticPages=all&status=all'))
request.get(testUtils.API.getApiQuery('posts/?page=all&status=all'))
.set('Authorization', 'Bearer ' + accesstoken)
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules['private'])
Expand All @@ -106,7 +106,7 @@ describe('Post API', function () {
});

it('can retrieve just published pages', function (done) {
request.get(testUtils.API.getApiQuery('posts/?staticPages=true'))
request.get(testUtils.API.getApiQuery('posts/?page=true'))
.set('Authorization', 'Bearer ' + accesstoken)
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules['private'])
Expand Down
44 changes: 32 additions & 12 deletions core/test/integration/model/model_posts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ describe('Post Model', function () {
testUtils.fixtures.insertMorePosts().then(function () {
return testUtils.fixtures.insertMorePostsTags();
}).then(function () {
return PostModel.findPage({page: 2});
return PostModel.findPage({offset: 15});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(2);
paginationResult.meta.pagination.limit.should.equal(15);
paginationResult.meta.pagination.pages.should.equal(4);
paginationResult.posts.length.should.equal(15);

return PostModel.findPage({page: 5});
return PostModel.findPage({offset: 60});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(5);
paginationResult.meta.pagination.limit.should.equal(15);
Expand All @@ -142,16 +142,8 @@ describe('Post Model', function () {
paginationResult.meta.pagination.pages.should.equal(2);
paginationResult.posts.length.should.equal(30);

// Test both boolean formats
return PostModel.findPage({limit: 10, staticPages: true});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(1);
paginationResult.meta.pagination.limit.should.equal(10);
paginationResult.meta.pagination.pages.should.equal(1);
paginationResult.posts.length.should.equal(1);

// Test both boolean formats
return PostModel.findPage({limit: 10, staticPages: '1'});
// Test both old page format
return PostModel.findPage({limit: 10, page: true});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(1);
paginationResult.meta.pagination.limit.should.equal(10);
Expand All @@ -169,6 +161,34 @@ describe('Post Model', function () {
paginationResult.meta.pagination.pages.should.equal(1);
paginationResult.posts.length.should.equal(107);

return PostModel.findPage({page: 2});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(2);
paginationResult.meta.pagination.limit.should.equal(15);
paginationResult.meta.pagination.pages.should.equal(4);
paginationResult.posts.length.should.equal(15);

return PostModel.findPage({page: 5});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(5);
paginationResult.meta.pagination.limit.should.equal(15);
paginationResult.meta.pagination.pages.should.equal(4);
paginationResult.posts.length.should.equal(0);

return PostModel.findPage({featured: true});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(1);
paginationResult.meta.pagination.limit.should.equal(15);
paginationResult.meta.pagination.pages.should.equal(4);
paginationResult.meta.pagination.total.should.equal(54);

return PostModel.findPage({limit: 30});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(1);
paginationResult.meta.pagination.limit.should.equal(30);
paginationResult.meta.pagination.pages.should.equal(2);
paginationResult.posts.length.should.equal(30);

done();
}).catch(done);
});
Expand Down

0 comments on commit 6627951

Please sign in to comment.