Retrospring/app/controllers/ajax/comment_controller.rb

44 lines
1.3 KiB
Ruby
Raw Normal View History

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])
rescue ActiveRecord::RecordInvalid => e
NewRelic::Agent.notice_error(e)
@response[:status] = :rec_inv
@response[:message] = I18n.t('messages.comment.create.rec_inv')
2014-12-07 05:31:20 -08:00
return
end
@response[:status] = :okay
@response[:message] = I18n.t('messages.comment.create.okay')
@response[:success] = true
@response[:render] = render_to_string(partial: 'answerbox/comments', locals: { a: answer })
@response[:count] = answer.comment_count
2014-12-07 05:31:20 -08:00
end
def destroy
params.require :comment
@response[:status] = :err
comment = Comment.find(params[:comment])
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.comment.destroy.nopriv')
return
end
@response[:count] = comment.answer.comment_count - 1
comment.destroy
@response[:status] = :okay
@response[:message] = I18n.t('messages.comment.destroy.okay')
@response[:success] = true
end
2014-12-07 05:31:20 -08:00
end