PostsController refactoring.
This commit is contained in:
parent
4c941495e5
commit
ed311eb65a
|
@ -114,17 +114,12 @@ class PostsController < ApplicationController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@post = find_post_from_params
|
@post = find_post_from_params
|
||||||
@post.revert_to(params[:version].to_i) if params[:version].present?
|
display_post(@post)
|
||||||
render_post_json(@post)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def by_number
|
def by_number
|
||||||
finder = Post.where(topic_id: params[:topic_id], post_number: params[:post_number])
|
@post = find_post_from_params_by_number
|
||||||
finder = finder.with_deleted if current_user.try(:staff?)
|
display_post(@post)
|
||||||
@post = finder.first
|
|
||||||
guardian.ensure_can_see!(@post)
|
|
||||||
@post.revert_to(params[:version].to_i) if params[:version].present?
|
|
||||||
render_post_json(@post)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def reply_history
|
def reply_history
|
||||||
|
@ -201,16 +196,6 @@ class PostsController < ApplicationController
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def find_post_from_params
|
|
||||||
finder = Post.where(id: params[:id] || params[:post_id])
|
|
||||||
# Include deleted posts if the user is staff
|
|
||||||
finder = finder.with_deleted if current_user.try(:staff?)
|
|
||||||
|
|
||||||
post = finder.first
|
|
||||||
guardian.ensure_can_see!(post)
|
|
||||||
post
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_post_revision_from_params
|
def find_post_revision_from_params
|
||||||
post_id = params[:id] || params[:post_id]
|
post_id = params[:id] || params[:post_id]
|
||||||
revision = params[:revision].to_i
|
revision = params[:revision].to_i
|
||||||
|
@ -274,4 +259,27 @@ class PostsController < ApplicationController
|
||||||
!guardian.send("can_#{action}?", post) && post.user_id == current_user.id && post.edit_time_limit_expired?
|
!guardian.send("can_#{action}?", post) && post.user_id == current_user.id && post.edit_time_limit_expired?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def display_post(post)
|
||||||
|
post.revert_to(params[:version].to_i) if params[:version].present?
|
||||||
|
render_post_json(post)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_post_from_params
|
||||||
|
by_id_finder = Post.where(id: params[:id] || params[:post_id])
|
||||||
|
find_post_using(by_id_finder)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_post_from_params_by_number
|
||||||
|
by_number_finder = Post.where(topic_id: params[:topic_id], post_number: params[:post_number])
|
||||||
|
find_post_using(by_number_finder)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_post_using(finder)
|
||||||
|
# Include deleted posts if the user is staff
|
||||||
|
finder = finder.with_deleted if current_user.try(:staff?)
|
||||||
|
post = finder.first
|
||||||
|
guardian.ensure_can_see!(post)
|
||||||
|
post
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue