2020-04-28 11:27:59 -07:00
|
|
|
class Ajax::CommentController < AjaxController
|
2014-12-07 05:31:20 -08:00
|
|
|
def create
|
|
|
|
params.require :answer
|
|
|
|
params.require :comment
|
|
|
|
|
|
|
|
answer = Answer.find(params[:answer])
|
|
|
|
|
|
|
|
begin
|
|
|
|
current_user.comment(answer, params[:comment])
|
2020-04-28 11:32:36 -07:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2021-12-28 09:32:03 -08:00
|
|
|
Sentry.capture_exception(e)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :rec_inv
|
|
|
|
@response[:message] = I18n.t('messages.comment.create.rec_inv')
|
2014-12-07 05:31:20 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.comment.create.okay')
|
|
|
|
@response[:success] = true
|
2020-04-29 05:01:48 -07:00
|
|
|
@response[:render] = render_to_string(partial: 'answerbox/comments', locals: { a: answer })
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:count] = answer.comment_count
|
2014-12-07 05:31:20 -08:00
|
|
|
end
|
2014-12-28 12:14:01 -08:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require :comment
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :err
|
2014-12-28 12:14:01 -08:00
|
|
|
comment = Comment.find(params[:comment])
|
|
|
|
|
|
|
|
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :nopriv
|
|
|
|
@response[:message] = I18n.t('messages.comment.destroy.nopriv')
|
2014-12-28 12:14:01 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:count] = comment.answer.comment_count - 1
|
2014-12-28 12:14:01 -08:00
|
|
|
comment.destroy
|
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:message] = I18n.t('messages.comment.destroy.okay')
|
|
|
|
@response[:success] = true
|
2014-12-28 12:14:01 -08:00
|
|
|
end
|
2014-12-07 05:31:20 -08:00
|
|
|
end
|