summaryrefslogtreecommitdiff
path: root/app/controllers/comments_controller.rb
blob: b69053e9c7b8c2726ca2384019cf782b95b7f9c5 (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
73
74
75
class CommentsController < ApplicationController

  include MailerHelper

  def edit
    @comment = Comment.find(params[:id])
    if (mod? && current_user.role >= @comment.author.role) || @comment.author.is?(current_user)
    else
      flash[:alert] = "You are not allowed to edit this comment"
      redirect_to @comment.blogpost
    end
  end

  def create
    if confirmed?
      @comment = Comment.new(comment_params)
      @comment.user_author = current_user
      @comment.blogpost = Blogpost.find(params[:blogpost_id])
      if @comment.save
        @comment.send_new_comment_mail
        position = @comment.blogpost.comments.count - 1
        page     = position / Kaminari.config.default_per_page + 1
        redirect_to blogpost_path(@comment.blogpost, page: page) + "#comment-#{@comment.id}", notice: 'Comment created!'
      else
        flash[:alert] = "Could not create comment."
        redirect_to Blogpost.find(params[:blogpost_id])
      end
    else
      flash[:alert] = "Please confirm your email address first!"
      redirect_to Blogpost.find(params[:blogpost_id])
    end
  end

  def update
    @comment = Comment.find(params[:id])
    if (mod? && current_user.role >= @comment.author.role) || @comment.author.is?(current_user)
      @comment.user_editor = current_user
      @comment.attributes = comment_params
      old_content = @comment.content_was
      if @comment.save
        @comment.send_new_comment_mail(old_content)
        flash[:notice] = "Comment updated!"
        position = @comment.blogpost.comments.index(@comment)
        page     = position / Kaminari.config.default_per_page + 1
        redirect_to blogpost_path(@comment.blogpost, page: page) + "#comment-#{@comment.id}"
      else
        flash[:alert] = "There was a problem while updating your comment"
        render action: "edit"
      end
    else
      flash[:alert] = "You are not allowed to edit this comment"
      redirect_to @comment.blogpost
    end
  end

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

  private

  def comment_params
    params.require(:comment).permit(:content)
  end
end