Retrospring/app/controllers/ajax/comment_controller.rb

53 lines
1.4 KiB
Ruby
Raw Normal View History

2014-12-07 05:31:20 -08:00
class Ajax::CommentController < ApplicationController
2015-05-08 16:31:31 -07:00
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
2015-05-08 16:31:31 -07:00
@success = false
render partial: "ajax/shared/status"
end
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
@status = :rec_inv
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.comment.create.rec_inv')
2014-12-07 05:31:20 -08:00
@success = false
return
end
@status = :okay
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.comment.create.okay')
2014-12-07 05:31:20 -08:00
@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
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
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.comment.destroy.nopriv')
@success = false
return
end
2014-12-28 12:58:11 -08:00
@count = comment.answer.comment_count - 1
comment.destroy
@status = :okay
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.comment.destroy.okay')
@success = true
end
2014-12-07 05:31:20 -08:00
end