Retrospring/app/controllers/ajax/answer_controller.rb

95 lines
2.4 KiB
Ruby
Raw Normal View History

2014-11-26 08:05:46 -08:00
class Ajax::AnswerController < ApplicationController
2015-05-08 16:31:31 -07:00
rescue_from(ActionController::ParameterMissing) do |titanic_param|
@status = :parameter_error
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.parameter_error', parameter: titanic_param.param.capitalize)
2015-05-08 16:31:31 -07:00
@success = false
render partial: "ajax/shared/status"
end
def create
params.require :id
params.require :answer
params.require :share
params.require :inbox
inbox = (params[:inbox] == 'true')
if inbox
inbox_entry = Inbox.find(params[:id])
unless current_user == inbox_entry.user
@status = :fail
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.answer.create.fail')
@success = false
return
end
else
question = Question.find(params[:id])
unless question.user.privacy_allow_stranger_answers
@status = :privacy_stronk
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.answer.create.privacy_stronk')
@success = false
return
end
end
2015-05-08 16:31:31 -07:00
# this should never trigger because empty params throw ParameterMissing
unless params[:answer].length > 0
@status = :peter_dinklage
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.answer.create.peter_dinklage')
2015-05-08 16:31:31 -07:00
@success = false
return
end
answer = nil
begin
answer = if inbox
inbox_entry.answer params[:answer], current_user
else
current_user.answer question, params[:answer]
end
rescue
@status = :err
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.error')
@success = false
return
end
services = JSON.parse params[:share]
ShareWorker.perform_async(current_user.id, answer.id, services)
2015-01-03 10:14:26 -08:00
@status = :okay
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.answer.create.okay')
@success = true
2015-01-03 10:14:26 -08:00
unless inbox
@question = 1
@render = render_to_string(partial: 'shared/answerbox', locals: { a: answer, show_question: false })
end
end
2014-11-26 08:05:46 -08:00
def destroy
params.require :answer
answer = Answer.find(params[:answer])
unless (current_user == answer.user) or (privileged? answer.user)
2014-11-26 08:05:46 -08:00
@status = :nopriv
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.answer.destroy.nopriv')
2014-11-26 08:05:46 -08:00
@success = false
return
end
if answer.user == current_user
Inbox.create!(user: answer.user, question: answer.question, new: true)
end # TODO: decide what happens with the question
2014-12-28 12:34:42 -08:00
answer.destroy
2014-11-26 08:05:46 -08:00
@status = :okay
2015-06-07 09:24:01 -07:00
@message = I18n.t('messages.answer.destroy.okay')
2014-11-26 08:05:46 -08:00
@success = true
end
end