diff --git a/.rubocop.yml b/.rubocop.yml index f214d03..e67f01d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -46,7 +46,6 @@ Metrics/AbcSize: # TODO: remove this cop exception after refactor Metrics/ClassLength: Exclude: - - app/controllers/candidate_controller.rb - test/**/* Metrics/LineLength: diff --git a/app/controllers/candidate_controller.rb b/app/controllers/candidate_controller.rb index 59d1e77..60e93e7 100644 --- a/app/controllers/candidate_controller.rb +++ b/app/controllers/candidate_controller.rb @@ -25,40 +25,6 @@ class CandidateController < ApplicationController reset_session end - 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 - 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? @@ -80,84 +46,4 @@ class CandidateController < ApplicationController def send_to_oops redirect_to oops_path if current_candidate end - - 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 diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb new file mode 100644 index 0000000..078b029 --- /dev/null +++ b/app/controllers/quiz_controller.rb @@ -0,0 +1,119 @@ +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 diff --git a/app/views/candidate/_live_code_text.html.erb b/app/views/candidate/_live_code_text.html.erb deleted file mode 100644 index 11d5458..0000000 --- a/app/views/candidate/_live_code_text.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render partial: "candidate/live_code", locals: {question: question, form: form} %> diff --git a/app/views/candidate/_answer_errors.html.erb b/app/views/quiz/_answer_errors.html.erb similarity index 100% rename from app/views/candidate/_answer_errors.html.erb rename to app/views/quiz/_answer_errors.html.erb diff --git a/app/views/candidate/_checkbox.html.erb b/app/views/quiz/_checkbox.html.erb similarity index 83% rename from app/views/candidate/_checkbox.html.erb rename to app/views/quiz/_checkbox.html.erb index ad86f2a..e63e23c 100644 --- a/app/views/candidate/_checkbox.html.erb +++ b/app/views/quiz/_checkbox.html.erb @@ -14,4 +14,4 @@ <% end %> -<%= render partial: "candidate/answer_errors", locals: {question: question, answer: @answer} %> +<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %> diff --git a/app/views/candidate/_live_code.html.erb b/app/views/quiz/_live_code.html.erb similarity index 93% rename from app/views/candidate/_live_code.html.erb rename to app/views/quiz/_live_code.html.erb index f64a39f..a1df21f 100644 --- a/app/views/candidate/_live_code.html.erb +++ b/app/views/quiz/_live_code.html.erb @@ -11,7 +11,7 @@ Please revisit this page with JavaScript enabled to modify your answer. --> -<%= render partial: "candidate/answer_errors", locals: {question: question, answer: @answer} %> +<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %>
diff --git a/app/views/quiz/_live_code_text.html.erb b/app/views/quiz/_live_code_text.html.erb new file mode 100644 index 0000000..1349b48 --- /dev/null +++ b/app/views/quiz/_live_code_text.html.erb @@ -0,0 +1 @@ +<%= render partial: "quiz/live_code", locals: {question: question, form: form} %> diff --git a/app/views/candidate/_radio.html.erb b/app/views/quiz/_radio.html.erb similarity index 78% rename from app/views/candidate/_radio.html.erb rename to app/views/quiz/_radio.html.erb index 3d1cd7d..89a2cf6 100644 --- a/app/views/candidate/_radio.html.erb +++ b/app/views/quiz/_radio.html.erb @@ -9,4 +9,4 @@