skill-assessment-app/app/controllers/quiz_controller.rb

107 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class QuizController < ApplicationController
before_action :authorize_candidate
def question
qid = prep_status.current_question_id || params[:question_id]
redirect_to :summary and return if qid.nil?
prep_question qid
2016-08-08 17:08:20 -05:00
@answer = prep_answer qid
prep_instance_answer @question
end
def update_answer
2016-08-09 23:17:35 -05:00
@answer = prep_answer answer_params[:question_id]
prep_status
2016-09-02 17:51:35 -05:00
prep_question(answer_params[:question_id])
@answer.update(process_answer_params)
route_answer_xhr and return if request.xhr?
route_answer_html
end
def summary
@quiz = current_candidate.my_quiz
redirect_to :question and return unless prep_status.current_question_id.nil?
end
def submit_summary
not_completed_error = 'You must complete all questions to submit your test.'
redirect_to :summary, flash: { error: not_completed_error } and return unless prep_status.can_submit
2016-09-14 17:29:09 -05:00
complete_and_email
redirect_to :thankyou
end
private
2016-09-14 14:38:26 -05:00
def complete_and_email
if current_candidate.update_attributes(completed: true)
CandidateMailer.submitted(current_candidate).deliver_later
RecruiterMailer.candidate_submitted(current_candidate).deliver_later
ReviewerMailer.candidate_submission(current_candidate).deliver_later
return true
end
end
def prep_question qid
@question = current_candidate.fetch_question(qid)
end
def prep_status
@status ||= QuizStatus.new(current_candidate)
end
def prep_instance_answer question
@answer = question.answer.nil? ? Answer.new : Answer.find(question.answer_id)
end
def answer_params
params.require(:answer).permit(
:question_id,
2016-09-02 17:51:35 -05:00
:answer,
2016-09-07 17:45:50 -05:00
:answer_id,
2016-09-02 17:51:35 -05:00
answer_array: [],
answer_hash: [:later, :html, :css, :js, :text, :other, options: []]
)
end
2016-09-02 17:51:35 -05:00
def process_answer_params
answer = answer_params
answer[:saved] = params.key?(:save)
answer[:submitted] = params.key?(:submit)
answer[:answer] = answer_params[:answer_array] unless answer_params[:answer_array].nil?
answer[:answer] = answer_params[:answer_hash].to_h unless answer_params[:answer_hash].nil?
answer
end
def prep_answer qid = answer_params[:question_id]
answer_ids = { question_id: qid, candidate_id: current_candidate.to_i }
answer = Answer.find_or_create_by(answer_ids)
answer
end
def route_answer_html
if @answer.errors.present?
prep_question answer_params[:question_id]
2016-08-09 23:17:35 -05:00
flash[:error] = answer_params[:question_id].to_i
render :question
else
redirect_to :saved and return if params.key?(:save)
redirect_to :question
end
end
def route_answer_xhr
2016-08-09 23:17:35 -05:00
if @answer.errors.present?
render json: @answer.errors["answer"].to_json, status: 400
else
results = {
message: "Your answer has been updated successfully!",
can_submit: prep_status.can_submit,
progress: prep_status.progress
}
render json: results.to_json
2016-08-09 23:17:35 -05:00
end
end
end