Class: CommentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/comments_controller.rb

Overview

The CommentController can handle comments on polymorphic ‘commentables’ REVIEW: The code to handle comments with different levels of commentable SMELLS!

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#is_current_user?

Instance Method Details

- (Object) create



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/comments_controller.rb', line 21

def create
  if can? :create, Comment
    params[:comment][:from_ip] = request.env['REMOTE_ADDR']
    @comment, errors = Comment::build_and_validate_comment(@commentable,params[:comment])
    if errors
      flash[:error] = t(:comment_could_not_be_saved, :errors => errors).html_safe
    else
      remember_comment
      flash[:notice] = t(:comment_successfully_created_edit_for_minutes,
        :count => CONSTANTS['max_time_to_edit_new_comments'].to_i
      ).html_safe
    end
    notice = nil
  else
    notice = t(:access_denied)
  end
  redirect_to view_context.commentable_show_path(@commentable), :alert => notice
end

- (Object) destroy



72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/comments_controller.rb', line 72

def destroy
  @commentable ||= @comment.commentable
  @comment.destroy
  @commentable.save
  respond_to do |format|
    format.js
    format.html { redirect_to view_context.commentable_show_path(@commentable),
      :notice => t(:comment_successfully_destroyed).html_safe
    }
  end
end

- (Object) edit



40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/comments_controller.rb', line 40

def edit
  if can? :edit, @comment, session[:comments]
    respond_to do |format|
      format.js
      format.html
    end
  else
    redirect_to view_context.commentable_show_path(@comment.commentable),
      :alert => t(:not_authorized)
  end
end

- (Object) index



13
14
15
16
17
18
19
# File 'app/controllers/comments_controller.rb', line 13

def index
  if can? :read, Comment.new
    @comments = Comment.where(:updated_at.gt => Time.now-1.year).desc('created_at')
  else
    redirect_to root_path, :alert => t(:not_authorized)
  end
end

- (Object) update



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/comments_controller.rb', line 52

def update
  if can? :edit, @comment, session[:comments]
    unless params[:commit] == t(:cancel)
      @comment.comment = params[:comment][:comment]
      remember_comment
      @comment.save
    end
    @new_comment = (RedCloth.new(@comment.comment).to_html.html_safe)
    respond_to do |format|
      format.js
      format.html { redirect_to view_context.commentable_show_path(@commentable),
        :notice => t(:comment_successfully_updated).html_safe
      }
    end
  else
    redirect_to view_context.commentable_show_path(@comment.commentable),
      :alert => t(:not_authorized)
  end
end