mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
basic pagination, with tests
This commit is contained in:
parent
4d0b2ee0c4
commit
e9696fcec2
3 changed files with 99 additions and 10 deletions
|
@ -4,7 +4,7 @@
|
||||||
var _ = require('underscore'),
|
var _ = require('underscore'),
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
models = require('./models'),
|
models = require('./models'),
|
||||||
Bookshelf = require('./bookshelf'),
|
Bookshelf = require('bookshelf'),
|
||||||
BaseProvider = require('./dataProvider.bookshelf.base'),
|
BaseProvider = require('./dataProvider.bookshelf.base'),
|
||||||
PostsProvider;
|
PostsProvider;
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@
|
||||||
BaseProvider.call(this, models.Post, models.Posts);
|
BaseProvider.call(this, models.Post, models.Posts);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
util.inherits(PostsProvider, BaseProvider);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find results by page - returns an object containing the
|
* Find results by page - returns an object containing the
|
||||||
* information about the request (page, limit), along with the
|
* information about the request (page, limit), along with the
|
||||||
|
@ -34,6 +36,12 @@
|
||||||
*/
|
*/
|
||||||
PostsProvider.prototype.findPage = function (opts) {
|
PostsProvider.prototype.findPage = function (opts) {
|
||||||
var postCollection;
|
var postCollection;
|
||||||
|
|
||||||
|
// Allow findPage(n)
|
||||||
|
if (!_.isObject(opts)) {
|
||||||
|
opts = {page: opts};
|
||||||
|
}
|
||||||
|
|
||||||
opts = _.defaults(opts || {}, {
|
opts = _.defaults(opts || {}, {
|
||||||
page: 1,
|
page: 1,
|
||||||
limit: 15,
|
limit: 15,
|
||||||
|
@ -67,7 +75,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return qb.count(_.result(collection, 'idAttribute')).then(function (resp) {
|
return qb.count(_.result(collection, 'idAttribute')).then(function (resp) {
|
||||||
var totalPosts = resp.aggregate;
|
var totalPosts = resp[0].aggregate;
|
||||||
return {
|
return {
|
||||||
posts: collection.toJSON(),
|
posts: collection.toJSON(),
|
||||||
page: opts.page,
|
page: opts.page,
|
||||||
|
@ -79,7 +87,5 @@
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(PostsProvider, BaseProvider);
|
|
||||||
|
|
||||||
module.exports = PostsProvider;
|
module.exports = PostsProvider;
|
||||||
}());
|
}());
|
|
@ -6,7 +6,8 @@
|
||||||
var _ = require("underscore"),
|
var _ = require("underscore"),
|
||||||
should = require('should'),
|
should = require('should'),
|
||||||
helpers = require('./helpers'),
|
helpers = require('./helpers'),
|
||||||
PostProvider = require('../../shared/models/dataProvider.bookshelf.posts');
|
PostProvider = require('../../shared/models/dataProvider.bookshelf.posts'),
|
||||||
|
Bookshelf = require('bookshelf');
|
||||||
|
|
||||||
describe('Bookshelf PostsProvider', function () {
|
describe('Bookshelf PostsProvider', function () {
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@
|
||||||
helpers.resetData().then(function () {
|
helpers.resetData().then(function () {
|
||||||
posts = new PostProvider();
|
posts = new PostProvider();
|
||||||
done();
|
done();
|
||||||
});
|
}, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can browse', function (done) {
|
it('can browse', function (done) {
|
||||||
|
@ -121,5 +122,64 @@
|
||||||
|
|
||||||
}).then(null, done);
|
}).then(null, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can fetch a paginated set, with various options', function (done) {
|
||||||
|
|
||||||
|
helpers.insertMorePosts().then(function () {
|
||||||
|
|
||||||
|
return posts.findPage({page: 2});
|
||||||
|
|
||||||
|
}).then(function (paginationResult) {
|
||||||
|
|
||||||
|
paginationResult.page.should.equal(2);
|
||||||
|
|
||||||
|
paginationResult.limit.should.equal(15);
|
||||||
|
|
||||||
|
paginationResult.posts.length.should.equal(15);
|
||||||
|
|
||||||
|
paginationResult.pages.should.equal(4);
|
||||||
|
|
||||||
|
return posts.findPage({page: 5});
|
||||||
|
|
||||||
|
}).then(function (paginationResult) {
|
||||||
|
|
||||||
|
paginationResult.page.should.equal(5);
|
||||||
|
|
||||||
|
paginationResult.limit.should.equal(15);
|
||||||
|
|
||||||
|
paginationResult.posts.length.should.equal(0);
|
||||||
|
|
||||||
|
paginationResult.pages.should.equal(4);
|
||||||
|
|
||||||
|
return posts.findPage({limit: 30});
|
||||||
|
|
||||||
|
}).then(function (paginationResult) {
|
||||||
|
|
||||||
|
paginationResult.page.should.equal(1);
|
||||||
|
|
||||||
|
paginationResult.limit.should.equal(30);
|
||||||
|
|
||||||
|
paginationResult.posts.length.should.equal(30);
|
||||||
|
|
||||||
|
paginationResult.pages.should.equal(2);
|
||||||
|
|
||||||
|
return posts.findPage({limit: 10, page: 2, where: {language: 'fr'}});
|
||||||
|
|
||||||
|
}).then(function (paginationResult) {
|
||||||
|
|
||||||
|
paginationResult.page.should.equal(2);
|
||||||
|
|
||||||
|
paginationResult.limit.should.equal(10);
|
||||||
|
|
||||||
|
paginationResult.posts.length.should.equal(10);
|
||||||
|
|
||||||
|
paginationResult.pages.should.equal(3);
|
||||||
|
|
||||||
|
done();
|
||||||
|
|
||||||
|
}).then(null, done);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue