2014-12-07 05:31:20 -08:00
|
|
|
class Ajax::CommentController < ApplicationController
|
|
|
|
def create
|
|
|
|
params.require :answer
|
|
|
|
params.require :comment
|
|
|
|
|
|
|
|
answer = Answer.find(params[:answer])
|
|
|
|
|
|
|
|
begin
|
|
|
|
current_user.comment(answer, params[:comment])
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
@status = :rec_inv
|
|
|
|
@message = "Your comment is too long."
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@status = :okay
|
|
|
|
@message = "Comment posted successfully."
|
|
|
|
@success = true
|
|
|
|
@render = render_to_string(partial: 'shared/comments', locals: { a: answer })
|
2014-12-07 06:05:44 -08:00
|
|
|
@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
|
|
|
|
|
|
|
|
@status = :err
|
|
|
|
@success = false
|
|
|
|
comment = Comment.find(params[:comment])
|
|
|
|
|
|
|
|
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
|
|
|
|
@status = :nopriv
|
|
|
|
@message = "can't delete other people's comments"
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
comment.user.decrement! :commented_count
|
|
|
|
comment.answer.decrement! :comment_count
|
|
|
|
Notification.denotify comment.answer.user, comment
|
|
|
|
@count = comment.answer.comment_count
|
|
|
|
comment.destroy
|
|
|
|
|
|
|
|
@status = :okay
|
|
|
|
@message = "Successfully deleted comment."
|
|
|
|
@success = true
|
|
|
|
end
|
2014-12-07 05:31:20 -08:00
|
|
|
end
|