diff --git a/core/shared/models/dataProvider.bookshelf.base.js b/core/shared/models/dataProvider.bookshelf.base.js index 058dda0e3a..6f44c41771 100644 --- a/core/shared/models/dataProvider.bookshelf.base.js +++ b/core/shared/models/dataProvider.bookshelf.base.js @@ -21,26 +21,31 @@ /** * Naive find all * @param args (optional) + * @param opts (optional) */ - BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args) { - args = args || {}; - return this.collection.forge(args).fetch(); + BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (opts) { + opts = opts || {}; + return this.collection.forge().fetch(opts); }; /** * Naive find one where args match * @param args + * @param opts (optional) */ - BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args) { - return this.model.forge(args).fetch(); + BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args, opts) { + opts = opts || {}; + return this.model.forge(args).fetch(opts); }; /** * Naive edit * @param editedObj + * @param opts (optional) */ - BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj) { - return this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) { + BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj, opts) { + opts = opts || {}; + return this.model.forge({id: editedObj.id}).fetch(opts).then(function (foundObj) { return foundObj.set(editedObj).save(); }); }; @@ -48,17 +53,21 @@ /** * Naive add * @param newObj + * @param opts (optional) */ - BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj) { - return this.model.forge(newObj).save(); + BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj, opts) { + opts = opts || {}; + return this.model.forge(newObj).save(opts); }; /** * Naive destroy * @param _identifier + * @param opts (optional) */ - BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier) { - return this.model.forge({id: _identifier}).destroy(); + BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier, opts) { + opts = opts || {}; + return this.model.forge({id: _identifier}).destroy(opts); }; module.exports = BookshelfBase; diff --git a/core/shared/models/dataProvider.bookshelf.posts.js b/core/shared/models/dataProvider.bookshelf.posts.js index 8dcbc52eaa..e7c02c7f7c 100644 --- a/core/shared/models/dataProvider.bookshelf.posts.js +++ b/core/shared/models/dataProvider.bookshelf.posts.js @@ -1,8 +1,10 @@ (function () { "use strict"; - var util = require('util'), + var _ = require('underscore'), + util = require('util'), models = require('./models'), + Bookshelf = require('bookshelf'), BaseProvider = require('./dataProvider.bookshelf.base'), PostsProvider; @@ -15,5 +17,75 @@ util.inherits(PostsProvider, BaseProvider); + /** + * Find results by page - returns an object containing the + * information about the request (page, limit), along with the + * info needed for pagination (pages, total). + * + * { + * posts: [ + * {...}, {...}, {...} + * ], + * page: __, + * limit: __, + * pages: __, + * total: __ + * } + * + * @params opts + */ + PostsProvider.prototype.findPage = function (opts) { + var postCollection; + + // Allow findPage(n) + if (!_.isObject(opts)) { + opts = {page: opts}; + } + + opts = _.defaults(opts || {}, { + page: 1, + limit: 15, + where: null + }); + postCollection = this.collection.forge(); + + // If there are where conditionals specified, add those + // to the query. + if (opts.where) { + postCollection.query('where', opts.where); + } + + // 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. + return postCollection + .query('limit', opts.limit) + .query('offset', opts.limit * (opts.page - 1)) + .fetch(_.omit(opts, 'page', 'limit', 'where')) + .then(function (collection) { + var qb; + + // After we're done, we need to figure out what + // the limits are for the pagination values. + qb = Bookshelf.Knex(_.result(collection, 'tableName')); + + if (opts.where) { + qb.where(opts.where); + } + + return qb.count(_.result(collection, 'idAttribute')).then(function (resp) { + var totalPosts = resp[0].aggregate; + return { + posts: collection.toJSON(), + page: opts.page, + limit: opts.limit, + pages: Math.ceil(totalPosts / opts.limit), + total: totalPosts + }; + }); + }); + }; + module.exports = PostsProvider; }()); \ No newline at end of file diff --git a/core/test/ghost/api_posts_spec.js b/core/test/ghost/api_posts_spec.js index c4f64c1927..d19f4377d0 100644 --- a/core/test/ghost/api_posts_spec.js +++ b/core/test/ghost/api_posts_spec.js @@ -6,7 +6,8 @@ var _ = require("underscore"), should = require('should'), helpers = require('./helpers'), - PostProvider = require('../../shared/models/dataProvider.bookshelf.posts'); + PostProvider = require('../../shared/models/dataProvider.bookshelf.posts'), + Bookshelf = require('bookshelf'); describe('Bookshelf PostsProvider', function () { @@ -16,7 +17,7 @@ helpers.resetData().then(function () { posts = new PostProvider(); done(); - }); + }, done); }); it('can browse', function (done) { @@ -121,5 +122,64 @@ }).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); + + }); + }); }()); \ No newline at end of file diff --git a/core/test/ghost/helpers.js b/core/test/ghost/helpers.js index 5481dfa057..3a87bc97cc 100644 --- a/core/test/ghost/helpers.js +++ b/core/test/ghost/helpers.js @@ -4,18 +4,41 @@ // Use 'testing' Ghost config process.env.NODE_ENV = 'testing'; - var migrations = { + var knex = require('knex'), + migrations = { one: require("../../shared/data/migration/001") }, - helpers; + helpers, + samplePost; + + samplePost = function (i, lang) { + return { + title: "Test Post " + i, + slug: "ghost-from-fiction-to-function-" + i, + content: "Three days ago I released a concept page<\/a> for a lite version of WordPress that I've been thinking about for a long time, called Ghost. I think it's fair to say that I didn't quite anticipate how strong the reaction would be - and I've hardly had time to catch my breath in the last 72 hours.\n\nThe response was overwhelming, and overwhelmingly positive. In the first 6 hours my site got 35,000 page views after hitting the number 1 slot on Hacker News<\/a>. As of right now, the traffic count is just over 91,000 page views<\/a> - and Ghost has been featured all over the place. Notable mentions so far include Christina Warren from Mashable, who wrote about it<\/a>. Michael Carney from PandoDaily interviewed me about it<\/a>. Someone even wrote about it in Chinese<\/a>. That's pretty cool.\n\n\nThe feedback has been amazing, and while it's impossible to reply to all of the messages individually, I'm getting to as many of them as I can and I want to thank each and every one of you who took the time to send me a message or share the concept because you liked it. Now that the initial storm has died down a bit, I wanted to take some time to answer some of the more common questions and talk about what's next.\n

FAQ - Continued...<\/h2>\n\nThe most common question, bizarrely:\n

Oh my god, why is that whole page made of images? What's wrong with you? \/\/ I can't take you seriously \/\/ Don't you know anything about the web? \/\/ You are literally Satan re-incarnate.<\/strong><\/em><\/h5>\n\nThis was really the only negativity I got in response to the post, and it surprised me. I put together the concept page as... just that... a concept. It was a way for me to get the ideas out of my head and \"down on paper\" - or so to speak. I used photoshop as a tool<\/em> to write down my idea with text and images. If I used a sketchbook as a tool <\/em>to create images and handwritten notes, then uploaded scans of it, I doubt anyone would complain. The concept page was never supposed to be a finished product because I had no idea if there would be any interest in it. I had no motivation to waste hours coding a custom layout for something might only ever be read by a few people and then forgotten.\n\nHardware manufacturers make hundreds of foam cutout prototypes of products before they build one with working buttons and screens. I'm aware of all the usability problems with a web page made of images, and equally, foam cutouts without buttons or screens aren't particularly user friendly either. They're not supposed to be.\n\nLet's move on.\n
What? Why no comments? I need comments.<\/strong><\/em><\/h5>\n\nBecause comments add a layer of complexity that is beyond the core focus of this platform, which is publishing. Again, that's not to say you couldn't have any comments. This could easily be added with a dedicated plugin where you own the data or (as mentioned) there are third party providers such as Disqus, IntenseDebate, Livefyre and Facebook who all have great platforms. The point of this isn't to say \"you can't have comments\" - it's to say \"comments aren't on by default\". It's about simplicity, more than anything else.\n
Yeah, but WordPress are already going to revise their dashboard, WordPress.com is experimenting with a potential simplified version... so why bother with this?<\/strong><\/em><\/h5>\n\n\"\"<\/a>\n\nSorry, but Tumblr already did this - it's not the future of blogging, it's the past.\n\nGhost isn't about sharing \"Fuck Yeah [Dogs<\/a>\/Sharks<\/a>\/Girls with Tattoos<\/a>]\" - it's about publishing - which means writing - rather than mashing a few buttons to make sure that everyone can see and appreciate your latest funny picture\/status, which is surely the most funny picture\/status you've ever posted.\n\nTumblr, Pinterest and Facebook already have this locked down. It's not the future.\n
So... are you actually going to build this thing?<\/strong><\/em><\/h5>\n\nThe concept page was a way for me to test demand and interest. To see if anyone actually agreed with my frustrations and, more importantly, my solutions. I plucked a random figure of \"10,000 pageviews\" out of the air before I hit the publish button. If it got less than 10,000 pageviews, I would surrender to the fact that it would only ever be an idea. I've now exceeded that goal 9 times over, so yes, I'm looking at how Ghost can now be made into a reality.\n
How can I find out when it's done? \/\/ SHUT UP AND TAKE MY MONEY<\/strong><\/em><\/h5>\n\nOk, ok - there's a holding page up on http:\/\/TryGhost.org<\/a> - put your email address in.\n
\n

How are you going to do this?<\/h3>\n\nThere's three main ways of going about this, each has merits as well as drawbacks.\n\n1.) Build it from scratch<\/strong><\/em> - Many people (particularly the Hacker News crowd) expressed the sentiment that there was little point in forking WordPress. When you're going to strip out so much, you get to a point where you might as well start from scratch anyway. Take away the crutches of being stuck with older technologies and put together something which is as sophisticated in code as it is in UI\/UX.\n