summaryrefslogtreecommitdiff
path: root/app/controllers/threadreplies_controller.rb
blob: a801fbc633cedbda642089fec2066c6f84e39a92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class ThreadrepliesController < ApplicationController

  def edit
    @reply = Threadreply.find(params[:id])
    if mod? || @reply.author.is?(current_user)
    else
      flash[:alert] = "You are not allowed to edit this reply"
      redirect_to @reply.thread
    end
  end

  def create
    thread = Forumthread.find(params[:forumthread_id])
    if thread.can_write?(current_user)
      @reply = Threadreply.new(reply_params)
      @reply.user_author = current_user
      @reply.forumthread = thread
      if @reply.save
        @reply.send_new_reply_mail
        position = thread.replies.count - 1
        page     = position / Kaminari.config.default_per_page + 1
        redirect_to forumthread_path(@reply.thread, page: page) + "#reply-#{@reply.id}", notice: 'Reply created!'
      else
        flash[:alert] = "Could not create reply."
        redirect_to Forumthread.find(params[:forumthread_id])
      end
    else
      flash[:alert] = "You are not allowed to create replies."
      redirect_to Forumthread.find(params[:forumthread_id])
    end
  end

  def update
    @reply = Threadreply.find(params[:id])
    if mod? || @reply.author.is?(current_user)
      old_content = @reply.content_was
      if @reply.update_attributes(reply_params)
        @reply.send_new_reply_mail(old_content)
        flash[:notice] = "Reply updated!"
        position = @reply.thread.replies.order(:id).index(@reply)
        page     = position / Kaminari.config.default_per_page + 1
        redirect_to forumthread_path(@reply.thread, page: page) + "#reply-#{@reply.id}"
      else
        flash[:alert] = "There was a problem while updating your reply"
        render action: "edit"
      end
    else
      flash[:alert] = "You are not allowed to edit this reply"
      redirect_to @reply.thread
    end
  end

  def destroy
    @reply = Threadreply.find(params[:id])
    if mod? || @reply.author.is?(current_user)
      if @reply.destroy
        flash[:notice] = "Reply deleted!"
      else
        flash[:alert] = "There was a problem while deleting this reply"
      end
    else
      flash[:alert] = "You are not allowed to delete this reply"
    end
    redirect_to @reply.thread
  end

  private

  def reply_params
    params.require(:threadreply).permit(:content)
  end
end