refactor quiz processing

completes #60
This commit is contained in:
Mark Moser
2016-09-02 17:51:35 -05:00
parent 3f8d089701
commit a977c0ceb3
10 changed files with 62 additions and 87 deletions

View File

@ -12,7 +12,8 @@ class QuizController < ApplicationController
def update_answer
@answer = prep_answer answer_params[:question_id]
prep_status
send "process_#{prep_question(answer_params[:question_id]).input_type}"
prep_question(answer_params[:question_id])
@answer.update(process_answer_params)
route_answer_xhr and return if request.xhr?
route_answer_html
end
@ -48,14 +49,23 @@ class QuizController < ApplicationController
params.require(:answer).permit(
:question_id,
:answer_id,
:radio,
:text,
checkbox: [],
with_other: [:other, options: []],
live_code: [:later, :html, :css, :js, :text]
:answer,
answer_array: [],
answer_hash: [:later, :html, :css, :js, :text, :other, options: []]
)
end
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.delete(:answer_array)
answer.delete(:answer_hash)
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)
@ -68,7 +78,6 @@ class QuizController < ApplicationController
flash[:error] = answer_params[:question_id].to_i
render :question
else
# TODO: change params.key? to submit = save/next/summary
redirect_to :saved and return if params.key?(:save)
redirect_to :question
end
@ -86,36 +95,4 @@ class QuizController < ApplicationController
render json: results.to_json
end
end
# TODO: maybe a better way to do this. See Admin/QuestionController#process_question_params
def process_text
@answer.update(answer: answer_params[:text],
saved: params.key?(:save),
submitted: params.key?(:submit))
end
def process_radio
@answer.update(answer: answer_params[:radio],
saved: params.key?(:save),
submitted: params.key?(:submit))
end
def process_checkbox
@answer.update(answer: answer_params[:checkbox],
saved: params.key?(:save),
submitted: params.key?(:submit))
end
def process_live_code
@answer.update(answer: answer_params[:live_code].to_h,
saved: params.key?(:save),
submitted: params.key?(:submit))
end
def process_radio_other
@answer.update(answer: answer_params[:with_other].to_h,
saved: params.key?(:save),
submitted: params.key?(:submit))
end
alias process_checkbox_other process_radio_other
end