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



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

def create
  @comment = Commontator::Comment.new(
    thread: @commontator_thread, creator: @commontator_user, body: params.dig(:comment, :body)
  )
  parent_id = params.dig(:comment, :parent_id)
  @comment.parent = Commontator::Comment.find(parent_id) unless parent_id.blank?
  security_transgression_unless @comment.can_be_created_by?(@commontator_user)

  respond_to do |format|
    if params[:cancel].blank?
      if @comment.save
        sub = @commontator_thread.config.thread_subscription.to_sym
        @commontator_thread.subscribe(@commontator_user) if sub == :a || sub == :b
        subscribe_mentioned if @commontator_thread.config.mentions_enabled
        Commontator::Subscription.comment_created(@comment)
        @commontator_page = @commontator_thread.new_comment_page(
          @comment.parent_id, @commontator_show_all
        )

        format.js
      else
        format.js { render :new }
      end
    else
      format.js { render :cancel }
    end

    format.html { redirect_to commontable_url }
  end
end

#deleteObject

PUT /comments/1/delete



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

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

  @comment.errors.add(:base, t('commontator.comment.errors.already_deleted')) \
    unless @comment.delete_by(@commontator_user)

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

#downvoteObject

PUT /comments/1/downvote



142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/commontator/comments_controller.rb', line 142

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

  @comment.downvote_from @commontator_user

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

#editObject

GET /comments/1/edit



68
69
70
71
72
73
74
75
76
# File 'app/controllers/commontator/comments_controller.rb', line 68

def edit
  @comment.editor = @commontator_user
  security_transgression_unless @comment.can_be_edited_by?(@commontator_user)

  respond_to do |format|
    format.html { redirect_to commontable_url }
    format.js
  end
end

#newObject

GET /threads/1/comments/new



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/commontator/comments_controller.rb', line 15

def new
  @comment = Commontator::Comment.new(thread: @commontator_thread, creator: @commontator_user)
  parent_id = params.dig(:comment, :parent_id)
  unless parent_id.blank?
    parent = Commontator::Comment.find parent_id
    @comment.parent = parent
    @comment.body = "<blockquote><span class=\"author\">#{
      Commontator.commontator_name(parent.creator)
    }</span>\n#{
      parent.body
    }\n</blockquote>\n" if [ :q, :b ].include? @commontator_thread.config.comment_reply_style
  end
  security_transgression_unless @comment.can_be_created_by?(@commontator_user)

  respond_to do |format|
    format.html { redirect_to commontable_url }
    format.js
  end
end

#showObject

GET /comments/1



7
8
9
10
11
12
# File 'app/controllers/commontator/comments_controller.rb', line 7

def show
  respond_to do |format|
    format.html { redirect_to commontable_url }
    format.js
  end
end

#undeleteObject

PUT /comments/1/undelete



117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/commontator/comments_controller.rb', line 117

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

  @comment.errors.add(:base, t('commontator.comment.errors.not_deleted')) \
    unless @comment.undelete_by(@commontator_user)

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

#unvoteObject

PUT /comments/1/unvote



155
156
157
158
159
160
161
162
163
164
# File 'app/controllers/commontator/comments_controller.rb', line 155

def unvote
  security_transgression_unless @comment.can_be_voted_on_by?(@commontator_user)

  @comment.unvote voter: @commontator_user

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

#updateObject

PUT /comments/1



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/commontator/comments_controller.rb', line 79

def update
  @comment.editor = @commontator_user
  @comment.body = params.dig(:comment, :body)
  security_transgression_unless @comment.can_be_edited_by?(@commontator_user)

  respond_to do |format|
    if params[:cancel].blank?
      if @comment.save
        subscribe_mentioned if @commontator_thread.config.mentions_enabled

        format.js
      else
        format.js { render :edit }
      end
    else
      @comment.restore_attributes

      format.js { render :cancel }
    end

    format.html { redirect_to commontable_url }
  end
end

#upvoteObject

PUT /comments/1/upvote



130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/commontator/comments_controller.rb', line 130

def upvote
  security_transgression_unless @comment.can_be_voted_on_by?(@commontator_user)

  @comment.upvote_from @commontator_user

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