Skip to content

Commit 107c960

Browse files
committedMay 13, 2015
API: Adding featured filter option to posts.browse
Closes #5152 - Adds `featured` filter option to posts.browse method modifying the model to take it too
1 parent 209bd52 commit 107c960

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed
 

Diff for: ‎core/server/api/posts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ posts = {
4545
* Can return posts for a particular tag by passing a tag slug in
4646
*
4747
* @public
48-
* @param {{context, page, limit, status, staticPages, tag}} options (optional)
48+
* @param {{context, page, limit, status, staticPages, tag, featured}} options (optional)
4949
* @returns {Promise(Posts)} Posts Collection with Meta
5050
*/
5151
browse: function browse(options) {

Diff for: ‎core/server/models/post.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Post = ghostBookshelf.Model.extend({
288288
validOptions = {
289289
findAll: ['withRelated'],
290290
findOne: ['importing', 'withRelated'],
291-
findPage: ['page', 'limit', 'status', 'staticPages'],
291+
findPage: ['page', 'limit', 'status', 'staticPages', 'featured'],
292292
add: ['importing']
293293
};
294294

@@ -385,6 +385,14 @@ Post = ghostBookshelf.Model.extend({
385385
options.where.page = options.staticPages;
386386
}
387387

388+
if (options.featured) {
389+
// convert string true/false to boolean
390+
if (!_.isBoolean(options.featured)) {
391+
options.featured = options.featured === 'true' || options.featured === '1' ? true : false;
392+
}
393+
options.where.featured = options.featured;
394+
}
395+
388396
// Unless `all` is passed as an option, filter on
389397
// the status provided.
390398
if (options.status !== 'all') {

Diff for: ‎core/test/functional/routes/api/posts_spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ describe('Post API', function () {
127127
});
128128
});
129129

130+
it('can retrieve just featured posts', function (done) {
131+
request.get(testUtils.API.getApiQuery('posts/?featured=true'))
132+
.set('Authorization', 'Bearer ' + accesstoken)
133+
.expect('Content-Type', /json/)
134+
.expect('Cache-Control', testUtils.cacheRules['private'])
135+
.expect(200)
136+
.end(function (err, res) {
137+
if (err) {
138+
return done(err);
139+
}
140+
141+
should.not.exist(res.headers['x-cache-invalidate']);
142+
var jsonResponse = res.body;
143+
jsonResponse.posts.should.exist;
144+
testUtils.API.checkResponse(jsonResponse, 'posts');
145+
jsonResponse.posts.should.have.length(4);
146+
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
147+
testUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
148+
done();
149+
});
150+
});
151+
130152
it('can retrieve just draft posts', function (done) {
131153
request.get(testUtils.API.getApiQuery('posts/?status=draft'))
132154
.set('Authorization', 'Bearer ' + accesstoken)

Diff for: ‎core/test/integration/model/model_posts_spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ describe('Post Model', function () {
158158
paginationResult.meta.pagination.pages.should.equal(1);
159159
paginationResult.posts.length.should.equal(1);
160160

161+
// Test featured pages
162+
return PostModel.findPage({limit: 10, featured: true});
163+
}).then(function (paginationResult) {
164+
paginationResult.meta.pagination.page.should.equal(1);
165+
paginationResult.meta.pagination.limit.should.equal(10);
166+
paginationResult.meta.pagination.pages.should.equal(6);
167+
paginationResult.posts.length.should.equal(10);
168+
169+
// Test both boolean formats for featured pages
170+
return PostModel.findPage({limit: 10, featured: '1'});
171+
}).then(function (paginationResult) {
172+
paginationResult.meta.pagination.page.should.equal(1);
173+
paginationResult.meta.pagination.limit.should.equal(10);
174+
paginationResult.meta.pagination.pages.should.equal(6);
175+
paginationResult.posts.length.should.equal(10);
176+
161177
return PostModel.findPage({limit: 10, page: 2, status: 'all'});
162178
}).then(function (paginationResult) {
163179
paginationResult.meta.pagination.pages.should.equal(11);

0 commit comments

Comments
 (0)
Please sign in to comment.