rethinking question CRUD
This commit is contained in:
@ -28,4 +28,8 @@ class ApplicationController < ActionController::Base
|
||||
def authorize_reviewer
|
||||
redirect_to review_login_path unless current_reviewer
|
||||
end
|
||||
|
||||
def authorize_candidate
|
||||
redirect_to welcome_path unless current_candidate
|
||||
end
|
||||
end
|
||||
|
@ -1,33 +1,30 @@
|
||||
class CandidateController < ApplicationController
|
||||
before_action :authorize_candidate, except: [:welcome, :validate]
|
||||
|
||||
def welcome
|
||||
end
|
||||
|
||||
def saved
|
||||
end
|
||||
|
||||
def thankyou
|
||||
redirect_to root_path if session[:test_id].nil?
|
||||
reset_session
|
||||
end
|
||||
|
||||
def question
|
||||
@status = QuizStatus.new(current_candidate)
|
||||
qid = @status.current_question_id
|
||||
|
||||
redirect_to :summary and return if qid.nil?
|
||||
|
||||
@question = current_candidate.fetch_question(qid)
|
||||
prep_question qid
|
||||
@answer = Answer.new
|
||||
end
|
||||
|
||||
def update_answer
|
||||
answer_ids = { question_id: answer_params[:question_id], candidate_id: current_candidate.to_i }
|
||||
@answer = Answer.find_or_create_by!(answer_ids)
|
||||
@answer.answer = answer_for_type
|
||||
@answer.saved = answer_params[:save]
|
||||
@answer.submitted = answer_params[:next]
|
||||
|
||||
if @answer.save
|
||||
redirect_to :summary and return if params.key?(:update)
|
||||
redirect_to :saved and return if params.key?(:save)
|
||||
redirect_to :question
|
||||
else
|
||||
flash[:error] = [answer_params[:question_id]]
|
||||
@question = current_candidate.fetch_question(qid)
|
||||
render :question
|
||||
end
|
||||
def live_coder
|
||||
question
|
||||
render layout: false
|
||||
end
|
||||
|
||||
def summary
|
||||
@ -37,50 +34,82 @@ class CandidateController < ApplicationController
|
||||
redirect_to :question and return unless @status.current_question_id.nil?
|
||||
end
|
||||
|
||||
def update_text
|
||||
@answer = prep_answer
|
||||
@answer.update(answer: answer_params[:text],
|
||||
saved: answer_params[:save],
|
||||
submitted: answer_params[:next])
|
||||
validate_answer
|
||||
end
|
||||
|
||||
def update_radio
|
||||
@answer = prep_answer
|
||||
@answer.update(answer: answer_params[:radio],
|
||||
saved: answer_params[:save],
|
||||
submitted: answer_params[:next])
|
||||
validate_answer
|
||||
end
|
||||
|
||||
def update_checkbox
|
||||
@answer = prep_answer
|
||||
@answer.update(answer: answer_params[:checkbox],
|
||||
saved: answer_params[:save],
|
||||
submitted: answer_params[:next])
|
||||
validate_answer
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
# TODO
|
||||
def update_summary
|
||||
redirect_to :summary
|
||||
end
|
||||
|
||||
def thankyou
|
||||
redirect_to root_path if session[:test_id].nil?
|
||||
reset_session
|
||||
end
|
||||
|
||||
def saved
|
||||
# redirect_to :summary
|
||||
end
|
||||
|
||||
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?
|
||||
|
||||
if candidate.nil?
|
||||
reset_session
|
||||
redirect_to root_path, alert: "Sorry, incorrect test id"
|
||||
else
|
||||
session[:test_id] = candidate.test_hash
|
||||
redirect_to :question
|
||||
end
|
||||
end
|
||||
|
||||
def live_coder
|
||||
@question = Question.find(params[:question_id])
|
||||
@answer = @question.answers.order("RAND()").first.answer
|
||||
render layout: false
|
||||
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 answer_params
|
||||
params.require(:answer).permit(
|
||||
:question_id, :answer_id,
|
||||
:save, :next, :summary,
|
||||
:radio, :text, checkbox: [], live_coder: []
|
||||
:radio, :text, checkbox: [], live_code: []
|
||||
)
|
||||
end
|
||||
|
||||
def answer_for_type
|
||||
return answer_params[:radio] unless answer_params[:radio].nil?
|
||||
return answer_params[:text] unless answer_params[:text].nil?
|
||||
return answer_params[:checkbox] unless answer_params[:checkbox].nil?
|
||||
return answer_params[:live_coder] unless answer_params[:live_coder].nil?
|
||||
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
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user