Ghost/ghost/admin/app/routes/posts/post.js
Kevin P. Kucharczyk 20adbcde87 Fix posts.post route not redirecting to 404
closes #6385
- redirect to 404 when post is not found
- replace replaceRoute calls with replaceWith calls in routes (replaceRoute is only available on controllers)
- add post mirage factory
- add acceptance test for post 404 redirection
2016-01-26 15:25:53 +01:00

77 lines
2.0 KiB
JavaScript

import AuthenticatedRoute from 'ghost/routes/authenticated';
import ShortcutsRoute from 'ghost/mixins/shortcuts-route';
import isNumber from 'ghost/utils/isNumber';
import isFinite from 'ghost/utils/isFinite';
export default AuthenticatedRoute.extend(ShortcutsRoute, {
model(params) {
let post,
postId,
query;
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
postId = Number(params.post_id);
if (!isNumber(postId) || !isFinite(postId) || postId % 1 !== 0 || postId <= 0) {
return this.transitionTo('error404', params.post_id);
}
/* jscs:enable requireCamelCaseOrUpperCaseIdentifiers */
post = this.store.peekRecord('post', postId);
if (post) {
return post;
}
query = {
id: postId,
status: 'all',
staticPages: 'all'
};
return this.store.queryRecord('post', query).then((records) => {
let post = records.get('firstObject');
if (post) {
return post;
}
this.transitionTo('error404', postId);
});
},
afterModel(post) {
return this.get('session.user').then((user) => {
if (user.get('isAuthor') && !post.isAuthoredByUser(user)) {
return this.replaceWith('posts.index');
}
});
},
setupController(controller, model) {
this._super(controller, model);
this.controllerFor('posts').set('currentPost', model);
},
shortcuts: {
'enter, o': 'openEditor',
'command+backspace, ctrl+backspace': 'deletePost'
},
actions: {
openEditor(post) {
post = post || this.get('controller.model');
if (!post) {
return;
}
this.transitionTo('editor.edit', post.get('id'));
},
deletePost() {
this.controllerFor('posts').send('toggleDeletePostModal');
}
}
});