Class: Commontator::CommentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /threads/1/comments



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/commontator/comments_controller.rb', line 23

def create
  @comment = Comment.new
  @comment.body = params[:comment].nil? ? nil : params[:comment][:body]
  @comment.thread = @thread
  @comment.creator = @user
  
  raise SecurityTransgression unless @comment.can_be_created_by?(@user)
  
  respond_to do |format|
    if @comment.save
      @thread.subscribe(@user) if @thread.config.auto_subscribe_on_comment
      @thread.add_unread_except_for(@user)
      recipients = @thread.active_subscribers.reject{|s| s == @user}
      SubscriptionsMailer.comment_created(@comment, recipients).deliver \
        unless recipients.empty?

      format.html { redirect_to @thread }
      format.js
    else
      format.html { redirect_to @thread }
      format.js { render :new }
    end
  end
end

#deleteObject

PUT /comments/1/delete



76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/commontator/comments_controller.rb', line 76

def delete
  raise SecurityTransgression unless @comment.can_be_deleted_by?(@user)

  @comment.errors.add(:base, 'This comment has already been deleted.') \
    unless @comment.delete_by(@user)

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js { render :delete }
  end
end

#downvoteObject

PUT /comments/1/downvote



114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/commontator/comments_controller.rb', line 114

def downvote
  raise SecurityTransgression unless @comment.can_be_voted_on_by?(@user)
  
  @comment.downvote_from @user

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js { render :vote }
  end
end

#editObject

GET /comments/1/edit



49
50
51
52
53
54
55
56
# File 'app/controllers/commontator/comments_controller.rb', line 49

def edit
  raise SecurityTransgression unless @comment.can_be_edited_by?(@user)

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js
  end
end

#newObject

GET /threads/1/comments/new



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/commontator/comments_controller.rb', line 8

def new
  @comment = Comment.new
  @comment.thread = @thread
  @comment.creator = @user

  raise SecurityTransgression unless @comment.can_be_created_by?(@user)

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js
  end
 
end

#undeleteObject

PUT /comments/1/undelete



89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/commontator/comments_controller.rb', line 89

def undelete
  raise SecurityTransgression unless @comment.can_be_deleted_by?(@user)

  @comment.errors.add(:base, 'This comment is not deleted.') \
    unless @comment.undelete_by(@user)

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js { render :delete }
  end
end

#unvoteObject

PUT /comments/1/unvote



126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/commontator/comments_controller.rb', line 126

def unvote
  raise SecurityTransgression unless @comment.can_be_voted_on_by?(@user)
  
  @comment.unvote :voter => @user

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js { render :vote }
  end
end

#updateObject

PUT /comments/1



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/commontator/comments_controller.rb', line 59

def update
  raise SecurityTransgression unless @comment.can_be_edited_by?(@user)
  @comment.body = params[:comment].nil? ? nil : params[:comment][:body]
  @comment.editor = @user

  respond_to do |format|
    if @comment.save
      format.html { redirect_to @thread }
      format.js
    else
      format.html { redirect_to @thread }
      format.js { render :edit }
    end
  end
end

#upvoteObject

PUT /comments/1/upvote



102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/commontator/comments_controller.rb', line 102

def upvote
  raise SecurityTransgression unless @comment.can_be_voted_on_by?(@user)
  
  @comment.upvote_from @user

  respond_to do |format|
    format.html { redirect_to @thread }
    format.js { render :vote }
  end
end