skill-assessment-app/app/controllers/candidate_controller.rb
2016-08-01 16:43:05 -05:00

120 lines
3.0 KiB
Ruby

class CandidateController < ApplicationController
before_action :authorize_candidate, except: [:welcome, :validate, :live_coder]
def welcome
end
def saved
end
def thankyou
redirect_to root_path if session[:test_id].nil?
reset_session
end
def question
qid = prep_status.current_question_id
redirect_to :summary and return if qid.nil?
prep_question qid
@answer = Answer.new
end
def update_answer
qid = params[:qid] ||= prep_status.current_question_id
@answer = prep_answer qid
send "process_#{prep_question(qid).input_type}"
end
def live_coder
question
render layout: false
end
def summary
@quiz = current_candidate.my_quiz
redirect_to :question and return unless prep_status.current_question_id.nil?
end
# TODO
def update_summary
# redirect_to :summary
end
def validate
candidate = Candidate.find_by(test_hash: params['test_id'])
redirect_to(root_path, flash: { error: "Sorry, incorrect test id" }) and return if candidate.nil?
session[:test_id] = candidate.test_hash
redirect_to :thankyou and return if candidate.completed?
redirect_to :question
end
private
def prep_question qid
@question = current_candidate.fetch_question(qid)
end
def prep_status
@status ||= QuizStatus.new(current_candidate)
end
def answer_params
params.require(:answer).permit(
:question_id, :answer_id,
:radio, :text, checkbox: [], live_code: [:later, :html, :css, :js]
)
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
# 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
end