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

116 lines
2.8 KiB
Ruby
Raw Normal View History

2016-07-27 22:16:12 -05:00
class CandidateController < ApplicationController
2016-07-31 18:54:12 -05:00
before_action :authorize_candidate, except: [:welcome, :validate]
2016-07-27 22:16:12 -05:00
def welcome
end
2016-07-31 18:54:12 -05:00
def saved
end
def thankyou
redirect_to root_path if session[:test_id].nil?
reset_session
end
2016-07-27 22:16:12 -05:00
def question
2016-07-29 13:34:23 -05:00
@status = QuizStatus.new(current_candidate)
qid = @status.current_question_id
redirect_to :summary and return if qid.nil?
2016-07-31 18:54:12 -05:00
prep_question qid
2016-07-30 16:25:09 -05:00
@answer = Answer.new
2016-07-27 22:16:12 -05:00
end
2016-07-31 18:54:12 -05:00
def live_coder
question
render layout: false
2016-07-28 08:47:34 -05:00
end
2016-07-27 22:16:12 -05:00
def summary
2016-07-29 13:34:23 -05:00
@quiz = current_candidate.my_quiz
@status = QuizStatus.new(current_candidate)
redirect_to :question and return unless @status.current_question_id.nil?
2016-07-28 20:54:08 -05:00
end
2016-07-31 18:54:12 -05:00
def update_text
@answer = prep_answer
@answer.update(answer: answer_params[:text],
saved: answer_params[:save],
submitted: answer_params[:next])
validate_answer
2016-07-27 22:16:12 -05:00
end
2016-07-31 18:54:12 -05:00
def update_radio
@answer = prep_answer
@answer.update(answer: answer_params[:radio],
saved: answer_params[:save],
submitted: answer_params[:next])
validate_answer
2016-07-27 22:16:12 -05:00
end
2016-07-31 18:54:12 -05:00
def update_checkbox
@answer = prep_answer
@answer.update(answer: answer_params[:checkbox],
saved: answer_params[:save],
submitted: answer_params[:next])
validate_answer
2016-07-27 22:16:12 -05:00
end
2016-07-28 08:47:34 -05:00
2016-07-31 18:54:12 -05:00
def update_live_code
@answer = prep_answer
@answer.update(answer: answer_params[:live_code],
saved: answer_params[:save],
submitted: answer_params[:next])
validate_answer
end
2016-07-29 11:53:01 -05:00
2016-07-31 18:54:12 -05:00
# TODO
def update_summary
# redirect_to :summary
2016-07-28 08:47:34 -05:00
end
2016-07-28 09:37:46 -05:00
2016-07-31 18:54:12 -05:00
def validate
candidate = Candidate.find_by(test_hash: params['test_id'])
redirect_to(root_path, alert: "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
2016-07-29 11:00:04 -05:00
end
2016-07-29 11:53:01 -05:00
private
2016-07-31 18:54:12 -05:00
def prep_question qid
@question = current_candidate.fetch_question(qid)
end
2016-07-30 16:25:09 -05:00
def answer_params
params.require(:answer).permit(
:question_id, :answer_id,
:save, :next, :summary,
2016-07-31 18:54:12 -05:00
:radio, :text, checkbox: [], live_code: []
2016-07-30 16:25:09 -05:00
)
end
2016-07-31 18:54:12 -05:00
def prep_answer
answer_ids = { question_id: answer_params[:question_id], candidate_id: current_candidate.to_i }
answer = Answer.find_or_create_by(answer_ids)
answer
end
def validate_answer
if @answer.errors.present?
flash[:error] = [answer_params[:question_id]]
prep_question answer_params[:question_id]
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
2016-07-29 11:53:01 -05:00
end
2016-07-27 22:16:12 -05:00
end