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
47
48
49
# File 'app/controllers/commontator/comments_controller.rb', line 23

def create
  @comment = Comment.new
  @comment.thread = @thread
  @comment.creator = @user
  @comment.body = params[:comment].nil? ? nil : params[:comment][:body]
  security_transgression_unless @comment.can_be_created_by?(@user)
  subscribe_mentioned if Commontator.mentions_enabled

  respond_to do |format|
    if  !params[:cancel].nil?
      format.html { redirect_to @thread }
      format.js { render :cancel }
    elsif @comment.save
      sub = @thread.config.thread_subscription.to_sym
      @thread.subscribe(@user) if sub == :a || sub == :b
      Subscription.comment_created(@comment)

      @per_page = params[:per_page] || @thread.config.comments_per_page

      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



83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/commontator/comments_controller.rb', line 83

def delete
  security_transgression_unless @comment.can_be_deleted_by?(@user)

  @comment.errors.add(:base, t('commontator.comment.errors.already_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



121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/commontator/comments_controller.rb', line 121

def downvote
  security_transgression_unless @comment.can_be_voted_on_by?(@user) &&\
    @comment.thread.config.comment_voting.to_sym == :ld

  @comment.downvote_from @user

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

#editObject

GET /comments/1/edit



52
53
54
55
56
57
58
59
# File 'app/controllers/commontator/comments_controller.rb', line 52

def edit
  security_transgression_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



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

def new
  @comment = Comment.new
  @comment.thread = @thread
  @comment.creator = @user
  security_transgression_unless @comment.can_be_created_by?(@user)

  @per_page = params[:per_page] || @thread.config.comments_per_page

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

end

#undeleteObject

PUT /comments/1/undelete



96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/commontator/comments_controller.rb', line 96

def undelete
  security_transgression_unless @comment.can_be_deleted_by?(@user)

  @comment.errors.add(:base, t('commontator.comment.errors.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



134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/commontator/comments_controller.rb', line 134

def unvote
  security_transgression_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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/commontator/comments_controller.rb', line 62

def update
  security_transgression_unless @comment.can_be_edited_by?(@user)
  @comment.body = params[:comment].nil? ? nil : params[:comment][:body]
  @comment.editor = @user
  subscribe_mentioned if Commontator.mentions_enabled

  respond_to do |format|
    if !params[:cancel].nil?
      format.html { redirect_to @thread }
      format.js { render :cancel }
    elsif @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



109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/commontator/comments_controller.rb', line 109

def upvote
  security_transgression_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