forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move pagination formatting into a util
refs TryGhost#2896 - moves repeated code out of models - creates a new file for unit-testable code (this should be moved in future) - adds a default for `page` as that seems sensible - adds 100% test coverage for the new file
- Loading branch information
Showing
5 changed files
with
129 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* # Utils | ||
* Parts of the model code which can be split out and unit tested | ||
*/ | ||
|
||
/** | ||
* Takes the number of items returned and original options and calculates all of the pagination meta data | ||
* TODO: Could be moved to either middleware or a bookshelf plugin? | ||
This comment has been minimized.
Sorry, something went wrong. |
||
* @param {Number} totalItems | ||
* @param {Object} options | ||
* @returns {Object} pagination | ||
*/ | ||
module.exports.paginateResponse = function paginateResponse(totalItems, options) { | ||
var calcPages = Math.ceil(totalItems / options.limit) || 0, | ||
pagination = {}; | ||
|
||
pagination.page = options.page || 1; | ||
pagination.limit = options.limit; | ||
pagination.pages = calcPages === 0 ? 1 : calcPages; | ||
pagination.total = totalItems; | ||
pagination.next = null; | ||
pagination.prev = null; | ||
|
||
if (pagination.pages > 1) { | ||
if (pagination.page === 1) { | ||
pagination.next = pagination.page + 1; | ||
} else if (pagination.page === pagination.pages) { | ||
pagination.prev = pagination.page - 1; | ||
} else { | ||
pagination.next = pagination.page + 1; | ||
pagination.prev = pagination.page - 1; | ||
} | ||
} | ||
|
||
return pagination; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/*globals describe, it*/ | ||
/*jshint expr:true*/ | ||
var should = require('should'), | ||
|
||
// Thing we're testing | ||
utils = require('../../server/models/base/utils'); | ||
|
||
// To stop jshint complaining | ||
should.equal(true, true); | ||
|
||
describe('paginateResponse', function () { | ||
it('returns correct pagination object for single page', function () { | ||
utils.paginateResponse(5, {limit: 10, page: 1}).should.eql({ | ||
limit: 10, | ||
next: null, | ||
page: 1, | ||
pages: 1, | ||
prev: null, | ||
total: 5 | ||
}); | ||
}); | ||
|
||
it('returns correct pagination object for first page of many', function () { | ||
utils.paginateResponse(44, {limit: 5, page: 1}).should.eql({ | ||
limit: 5, | ||
next: 2, | ||
page: 1, | ||
pages: 9, | ||
prev: null, | ||
total: 44 | ||
}); | ||
}); | ||
|
||
it('returns correct pagination object for middle page of many', function () { | ||
utils.paginateResponse(44, {limit: 5, page: 9}).should.eql({ | ||
limit: 5, | ||
next: null, | ||
page: 9, | ||
pages: 9, | ||
prev: 8, | ||
total: 44 | ||
}); | ||
}); | ||
|
||
it('returns correct pagination object for last page of many', function () { | ||
utils.paginateResponse(44, {limit: 5, page: 3}).should.eql({ | ||
limit: 5, | ||
next: 4, | ||
page: 3, | ||
pages: 9, | ||
prev: 2, | ||
total: 44 | ||
}); | ||
}); | ||
|
||
it('returns correct pagination object when page not set', function () { | ||
utils.paginateResponse(5, {limit: 10}).should.eql({ | ||
limit: 10, | ||
next: null, | ||
page: 1, | ||
pages: 1, | ||
prev: null, | ||
total: 5 | ||
}); | ||
}); | ||
|
||
it('returns correct pagination object for limit all', function () { | ||
utils.paginateResponse(5, {limit: 'all'}).should.eql({ | ||
limit: 'all', | ||
next: null, | ||
page: 1, | ||
pages: 1, | ||
prev: null, | ||
total: 5 | ||
}); | ||
}); | ||
}); |
I don't think this will be the final resting place for this code, but as this refactor progresses I think homes for things will become more apparent. In the meantime, I find it useful to split out unit-testable code from the denseness of the model layer.