Skip to content

Commit

Permalink
Split up the responsibilities of the PostsController
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed May 9, 2013
1 parent 7447c7e commit bb0d7aa
Showing 1 changed file with 74 additions and 37 deletions.
111 changes: 74 additions & 37 deletions app/controllers/forem/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,101 @@ module Forem
class PostsController < Forem::ApplicationController
before_filter :authenticate_forem_user
before_filter :find_topic
before_filter :reject_locked_topic!, :only => [:create]
before_filter :block_spammers, :only => [:new, :create]
before_filter :authorize_reply_for_topic!, :only => [:new, :create]
before_filter :authorize_edit_post_for_forum!, :only => [:edit, :update]
before_filter :find_post_for_topic, :only => [:edit, :update, :destroy]
before_filter :ensure_post_ownership!, :only => [:destroy]

def new
authorize! :reply, @topic
@post = @topic.posts.build
@reply_to_post = @topic.posts.find_by_id(params[:reply_to_id])
find_reply_to_post

if params[:quote]
@post.text = view_context.forem_quote(@reply_to_post.text)
end
@post.text = view_context.forem_quote(@reply_to_post.text) if params[:quote]
end

def create
authorize! :reply, @topic
if @topic.locked?
flash.alert = t("forem.post.not_created_topic_locked")
redirect_to [@topic.forum, @topic] and return
end
@post = @topic.posts.build(params[:post])
@post.user = forem_user

if @post.save
flash[:notice] = t("forem.post.created")
redirect_to forum_topic_url(@topic.forum, @topic, :page => @topic.last_page)
create_successful
else
params[:reply_to_id] = params[:post][:reply_to_id]
flash.now.alert = t("forem.post.not_created")
render :action => "new"
create_failed
end
end

def edit
authorize! :edit_post, @topic.forum
@post = Forem::Post.find(params[:id])
end

def update
authorize! :edit_post, @topic.forum
@post = Forem::Post.find(params[:id])
if @post.owner_or_admin?(forem_user) and @post.update_attributes(params[:post])
redirect_to [@topic.forum, @topic], :notice => t('edited', :scope => 'forem.post')
if @post.owner_or_admin?(forem_user) && @post.update_attributes(params[:post])
update_successful
else
flash.now.alert = t("forem.post.not_edited")
render :action => "edit"
update_failed
end
end

def destroy
@post = @topic.posts.find(params[:id])
if @post.owner_or_admin?(forem_user)
@post.destroy
if @post.topic.posts.count == 0
@post.topic.destroy
flash[:notice] = t("forem.post.deleted_with_topic")
redirect_to [@topic.forum]
else
flash[:notice] = t("forem.post.deleted")
redirect_to [@topic.forum, @topic]
end
@post.destroy
destroy_successful
end

private

def authorize_reply_for_topic!
authorize! :reply, @topic
end

def authorize_edit_post_for_forum!
authorize! :edit_post, @topic.forum
end

def create_successful
flash[:notice] = t("forem.post.created")
redirect_to forum_topic_url(@topic.forum, @topic, :page => @topic.last_page)
end

def create_failed
params[:reply_to_id] = params[:post][:reply_to_id]
flash.now.alert = t("forem.post.not_created")
render :action => "new"
end

def destroy_successful
if @post.topic.posts.count == 0
@post.topic.destroy
flash[:notice] = t("forem.post.deleted_with_topic")
redirect_to [@topic.forum]
else
flash[:alert] = t("forem.post.cannot_delete")
flash[:notice] = t("forem.post.deleted")
redirect_to [@topic.forum, @topic]
end
end

def update_successful
redirect_to [@topic.forum, @topic], :notice => t('edited', :scope => 'forem.post')
end

private
def update_failed
flash.now.alert = t("forem.post.not_edited")
render :action => "edit"
end

def ensure_post_ownership!
unless @post.owner_or_admin? forem_user
flash[:alert] = t("forem.post.cannot_delete")
redirect_to [@topic.forum, @topic] and return
end
end

def find_topic
@topic = Forem::Topic.find(params[:topic_id])
@topic = Forem::Topic.find params[:topic_id]
end

def find_post_for_topic
@post = @topic.posts.find params[:id]
end

def block_spammers
Expand All @@ -79,5 +105,16 @@ def block_spammers
redirect_to :back
end
end

def reject_locked_topic!
if @topic.locked?
flash.alert = t("forem.post.not_created_topic_locked")
redirect_to [@topic.forum, @topic] and return
end
end

def find_reply_to_post
@reply_to_post = @topic.posts.find_by_id(params[:reply_to_id])
end
end
end

0 comments on commit bb0d7aa

Please sign in to comment.