skill-assessment-app/app/controllers/quiz_controller.rb
2016-08-08 07:45:08 -05:00

120 lines
3.5 KiB
Ruby

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
prep_instance_answer @question
end
def update_answer
qid = answer_params[:question_id] || prep_status.current_question_id
@answer = prep_answer qid
send "process_#{prep_question(qid).input_type}"
end
def live_coder
prep_question params[:question_id]
prep_instance_answer @question
prep_answer params[:question_id]
render @question.input_type, layout: false
end
def summary
@quiz = current_candidate.my_quiz
redirect_to :question and return unless prep_status.current_question_id.nil?
end
def update_summary
prep_status
not_completed_error = 'You must complete all questions to submit your test.'
record_error = 'There was a problem with your submission. Please try again later.'
redirect_to :summary, flash: { error: not_completed_error } and return unless @status.can_submit
redirect_to :thankyou and return if current_candidate.complete!
redirect_to :summary, flash: { error: record_error }
end
private
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,
:answer_id,
:radio,
:text,
checkbox: [],
live_code: [:later, :html, :css, :js],
live_code_text: [:later, :html, :css, :js, :text]
)
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
if @answer.errors.present?
prep_status
prep_question answer_params[:question_id]
flash[:answer_error] = answer_params[:question_id].to_i
render :question
else
flash.delete(:answer_error)
# TODO: change params.key? to submit = save/next/summary
# redirect_to :summary and return if params.key?(:update)
redirect_to :saved and return if params.key?(:save)
redirect_to :question
end
end
def process_text
@answer.update(answer: answer_params[:text],
saved: params.key?(:save),
submitted: params.key?(:submit))
route_answer
end
def process_radio
@answer.update(answer: answer_params[:radio],
saved: params.key?(:save),
submitted: params.key?(:submit))
route_answer
end
def process_checkbox
@answer.update(answer: answer_params[:checkbox],
saved: params.key?(:save),
submitted: params.key?(:submit))
route_answer
end
def process_live_code
@answer.update(answer: answer_params[:live_code].to_h,
saved: params.key?(:save),
submitted: params.key?(:submit))
route_answer
end
def process_live_code_text
@answer.update(answer: answer_params[:live_code_text].to_h,
saved: params.key?(:save),
submitted: params.key?(:submit))
route_answer
end
end