Ghost/ghost/admin/mirage/config/posts.js
Kevin Ansfield d37f68adcb 🐛 fix autosave+transition on title blur with empty title (#767)
refs TryGhost/Ghost#8525
- fix `saveTitle` action
	- don't abort title save when we have an empty title
	- force a "dirty" state so that the save actually happens
- add acceptance test for title blur behaviour
- extract multiple instances `"(Untitled)"` into a const
2017-07-10 18:33:05 +07:00

73 lines
2.0 KiB
JavaScript

import {Response} from 'ember-cli-mirage';
import {dasherize} from 'ember-string';
import {isBlank} from 'ember-utils';
import {paginateModelArray} from '../utils';
export default function mockPosts(server) {
server.post('/posts', function ({posts}) {
let attrs = this.normalizedRequestAttrs();
// mirage expects `author` to be a reference but we only have an ID
attrs.authorId = attrs.author;
delete attrs.author;
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
attrs.slug = dasherize(attrs.title);
}
return posts.create(attrs);
});
// TODO: handle author filter
server.get('/posts/', function ({posts}, {queryParams}) {
let page = +queryParams.page || 1;
let limit = +queryParams.limit || 15;
let {status, staticPages} = queryParams;
let query = {};
let models;
if (status && status !== 'all') {
query.status = status;
}
if (staticPages === 'false') {
query.page = false;
}
if (staticPages === 'true') {
query.page = true;
}
models = posts.where(query).models;
return paginateModelArray('posts', models, page, limit);
});
server.get('/posts/:id/', function ({posts}, {params}) {
let {id} = params;
let post = posts.find(id);
return post || new Response(404, {}, {
errors: [{
errorType: 'NotFoundError',
message: 'Post not found.'
}]
});
});
// Handle embedded author in post
server.put('/posts/:id/', ({posts}, request) => {
let {posts: [post]} = JSON.parse(request.requestBody);
let {author} = post;
delete post.author;
let savedPost = posts.find(request.params.id).update(post);
savedPost.authorId = author;
savedPost.save();
return savedPost;
});
server.del('/posts/:id/');
}