server half of #83, posts are draft by default, browse shows published by default

This commit is contained in:
Tim Griesser 2013-06-01 15:30:42 -04:00
parent 983c171fb0
commit 30bd89587f
6 changed files with 47 additions and 21 deletions

View File

@ -132,12 +132,12 @@
} }
}, },
'content': function (req, res) { 'content': function (req, res) {
api.posts.browse() api.posts.browse({status: req.params.status || 'all'})
.then(function (posts) { .then(function (page) {
res.render('content', { res.render('content', {
bodyClass: 'manage', bodyClass: 'manage',
adminNav: setSelected(adminNavbar, 'content'), adminNav: setSelected(adminNavbar, 'content'),
posts: posts.toJSON() posts: page.posts
}); });
}); });
}, },

View File

@ -14,8 +14,8 @@
frontendControllers = { frontendControllers = {
'homepage': function (req, res) { 'homepage': function (req, res) {
api.posts.browse().then(function (posts) { api.posts.browse().then(function (page) {
ghost.doFilter('prePostsRender', posts.toJSON(), function (posts) { ghost.doFilter('prePostsRender', page.posts, function (posts) {
res.render('index', {posts: posts, ghostGlobals: res.locals.ghostGlobals, navItems: res.locals.navItems}); res.render('index', {posts: posts, ghostGlobals: res.locals.ghostGlobals, navItems: res.locals.navItems});
}); });
}); });

View File

@ -22,9 +22,9 @@
// # Posts // # Posts
posts = { posts = {
// takes filter / pagination parameters // takes filter / pagination parameters
// returns a list of posts in a json response // returns a page of posts in a json response
browse: function (options) { browse: function (options) {
return dataProvider.Post.findAll(options); return dataProvider.Post.findPage(options);
}, },
// takes an identifier (id or slug?) // takes an identifier (id or slug?)
// returns a single post in a json response // returns a single post in a json response

View File

@ -16,6 +16,10 @@
hasTimestamps: true, hasTimestamps: true,
defaults: {
status: 'draft'
},
initialize: function () { initialize: function () {
this.on('creating', this.creating, this); this.on('creating', this.creating, this);
this.on('saving', this.saving, this); this.on('saving', this.saving, this);
@ -69,17 +73,25 @@
var postCollection; var postCollection;
// Allow findPage(n) // Allow findPage(n)
if (!_.isObject(opts)) { if (_.isString(opts) || _.isNumber(opts)) {
opts = {page: opts}; opts = {page: opts};
} }
opts = _.defaults(opts || {}, { opts = _.extend({
page: 1, page: 1,
limit: 15, limit: 15,
where: null where: {},
}); status: 'published'
}, opts);
postCollection = Posts.forge(); postCollection = Posts.forge();
// Unless `all` is passed as an option, filter on
// the status provided.
if (opts.status !== 'all') {
opts.where.status = opts.status;
}
// If there are where conditionals specified, add those // If there are where conditionals specified, add those
// to the query. // to the query.
if (opts.where) { if (opts.where) {
@ -93,7 +105,7 @@
return postCollection return postCollection
.query('limit', opts.limit) .query('limit', opts.limit)
.query('offset', opts.limit * (opts.page - 1)) .query('offset', opts.limit * (opts.page - 1))
.fetch(_.omit(opts, 'page', 'limit', 'where')) .fetch(_.omit(opts, 'page', 'limit', 'where', 'status'))
.then(function (collection) { .then(function (collection) {
var qb; var qb;

View File

@ -72,7 +72,7 @@
}).then(null, done); }).then(null, done);
}); });
it('can add', function (done) { it('can add, defaulting as a draft', function (done) {
var newPost = { var newPost = {
title: 'Test Title 1', title: 'Test Title 1',
content: 'Test Content 1' content: 'Test Content 1'
@ -81,9 +81,10 @@
PostModel.add(newPost).then(function (createdPost) { PostModel.add(newPost).then(function (createdPost) {
should.exist(createdPost); should.exist(createdPost);
createdPost.attributes.title.should.equal(newPost.title, "title is correct"); createdPost.get('status').should.equal('draft');
createdPost.attributes.content.should.equal(newPost.content, "content is correct"); createdPost.get('title').should.equal(newPost.title, "title is correct");
createdPost.attributes.slug.should.equal(newPost.title.toLowerCase().replace(/ /g, '-'), 'slug is correct'); createdPost.get('content').should.equal(newPost.content, "content is correct");
createdPost.get('slug').should.equal(newPost.title.toLowerCase().replace(/ /g, '-'), 'slug is correct');
done(); done();
}).then(null, done); }).then(null, done);
@ -173,6 +174,12 @@
paginationResult.pages.should.equal(3); paginationResult.pages.should.equal(3);
return PostModel.findPage({limit: 10, page: 2, status: 'all'});
}).then(function (paginationResult) {
paginationResult.pages.should.equal(11);
done(); done();
}).then(null, done); }).then(null, done);

File diff suppressed because one or more lines are too long