move summary and questions to quiz controller

This commit is contained in:
Mark Moser 2016-08-07 09:36:08 -05:00
parent cc0823e6f9
commit e6358beec8
20 changed files with 194 additions and 163 deletions

View File

@ -46,7 +46,6 @@ Metrics/AbcSize:
# TODO: remove this cop exception after refactor
Metrics/ClassLength:
Exclude:
- app/controllers/candidate_controller.rb
- test/**/*
Metrics/LineLength:

View File

@ -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

View File

@ -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

View File

@ -1 +0,0 @@
<%= render partial: "candidate/live_code", locals: {question: question, form: form} %>

View File

@ -14,4 +14,4 @@
</div>
<% end %>
<%= render partial: "candidate/answer_errors", locals: {question: question, answer: @answer} %>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %>

View File

@ -11,7 +11,7 @@
Please revisit this page with JavaScript enabled to modify your answer.
</div> -->
<%= render partial: "candidate/answer_errors", locals: {question: question, answer: @answer} %>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %>
<div data-id="live-coder-finish-later">
<p class="warning">

View File

@ -0,0 +1 @@
<%= render partial: "quiz/live_code", locals: {question: question, form: form} %>

View File

@ -9,4 +9,4 @@
</div>
<% end %>
<%= render partial: "candidate/answer_errors", locals: {question: question, answer: @answer} %>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %>

View File

@ -5,4 +5,4 @@
<div class="chars <%= hidden %>">Characters remaining: <span></span></div>
<%= render partial: "candidate/answer_errors", locals: {question: question, answer: @answer} %>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %>

View File

@ -21,7 +21,7 @@
<% end %>
<fieldset disabled class="answer-block">
<%= hidden_field_tag 'answer[question_id]', question.question_id %>
<%= render partial: "candidate/#{question.input_type}", locals: {question: question, form: form} %>
<%= render partial: "quiz/#{question.input_type}", locals: {question: question, form: form} %>
</fieldset>
</div>
</article>

View File

@ -1,30 +1,30 @@
Rails.application.routes.draw do
post "/validate", to: "candidate#validate", as: :validate_candidate
get "/login(/:test_id)", to: "candidate#login", as: :login
get "/welcome", to: "candidate#welcome", as: :welcome
get "/saved", to: "candidate#saved", as: :saved
get "/thankyou", to: "candidate#thankyou", as: :thankyou
get "/oops", to: "candidate#oops", as: :oops
post "/validate", to: "candidate#validate", as: :validate_candidate
get "/login(/:test_id)", to: "candidate#login", as: :login
get "/welcome", to: "candidate#welcome", as: :welcome
get "/saved", to: "candidate#saved", as: :saved
get "/thankyou", to: "candidate#thankyou", as: :thankyou
post "/question(/:answer_id)", to: "candidate#update_answer", as: :post_answer
get "/question(/:question_id)", to: "candidate#question", as: :question
get "/live-coder-entry/:question_id", to: "candidate#live_coder", as: :live_coder
get "/oops", to: "candidate#oops", as: :oops
post "/summary", to: "candidate#update_summary", as: :post_summary
get "/summary", to: "candidate#summary", as: :summary
post "/question(/:answer_id)", to: "quiz#update_answer", as: :post_answer
get "/question(/:question_id)", to: "quiz#question", as: :question
get "/live-coder-entry/:question_id", to: "quiz#live_coder", as: :live_coder
post "/summary", to: "quiz#update_summary", as: :post_summary
get "/summary", to: "quiz#summary", as: :summary
get "/review/logout", to: "review#logout", as: :review_logout
post "/review/login", to: "review#auth", as: :review_auth
get "/review/login", to: "review#login", as: :review_login
get "/review", to: "review#index", as: :review
get "/review/:test_hash", to: "review#view", as: :review_test
get "/review/logout", to: "review#logout", as: :review_logout
post "/review/login", to: "review#auth", as: :review_auth
get "/review/login", to: "review#login", as: :review_login
get "/review", to: "review#index", as: :review
get "/review/:test_hash", to: "review#view", as: :review_test
get "/recruiter", to: "recruiter#index", as: :recruiter
get "/recruiter/new-candidate", to: "recruiter#new", as: :new_candidate
post "/recruiter/new-candidate", to: "recruiter#create", as: :create_candidate
get "/recruiter/logout", to: "recruiter#logout", as: :recruiter_logout
get "/recruiter/login", to: "recruiter#login", as: :recruiter_login
post "/recruiter/login", to: "recruiter#auth", as: :recruiter_auth
get "/recruiter", to: "recruiter#index", as: :recruiter
get "/recruiter/new-candidate", to: "recruiter#new", as: :new_candidate
post "/recruiter/new-candidate", to: "recruiter#create", as: :create_candidate
get "/recruiter/logout", to: "recruiter#logout", as: :recruiter_logout
get "/recruiter/login", to: "recruiter#login", as: :recruiter_login
post "/recruiter/login", to: "recruiter#auth", as: :recruiter_auth
root to: "candidate#login"

View File

@ -17,15 +17,6 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest
get thankyou_path
assert_redirected_to login_path
get summary_path
assert_redirected_to login_path
get question_path
assert_redirected_to login_path
get question_path(questions(:fed1).id)
assert_redirected_to login_path
end
test "should auth to welcome" do
@ -71,16 +62,6 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
test "should get flash message on bad radio response" do
setup_auth candidates(:martha)
qid = questions(:fed1).id
post post_answer_path, params: { answer: { question_id: qid, radio: nil } }
assert_response :success
assert session[:test_id].present?
assert_equal qid, flash[:answer_error]
end
test "should NOT send mailers on submission" do
setup_auth candidates(:dawn)

View File

@ -0,0 +1,28 @@
require 'test_helper'
class QuizControllerTest < ActionDispatch::IntegrationTest
def setup_auth candidate
post validate_candidate_url, params: { test_id: candidate.test_hash }
end
test "should require auth and redirect" do
get summary_path
assert_redirected_to login_path
get question_path
assert_redirected_to login_path
get question_path(questions(:fed1).id)
assert_redirected_to login_path
end
test "should get flash message on bad radio response" do
setup_auth candidates(:martha)
qid = questions(:fed1).id
post post_answer_path, params: { answer: { question_id: qid, radio: nil } }
assert_response :success
assert session[:test_id].present?
assert_equal qid, flash[:answer_error]
end
end

View File

@ -1,6 +1,6 @@
require 'test_helper'
class QuestionFeatureTest < ActionDispatch::IntegrationTest
class QuestionAttachmentsTest < ActionDispatch::IntegrationTest
def setup_auth candidate
post validate_candidate_url, params: { test_id: candidate.test_hash }
end

View File

@ -0,0 +1,18 @@
require 'test_helper'
class QuestionLiveCoderTest < ActionDispatch::IntegrationTest
def setup_auth candidate
post validate_candidate_url, params: { test_id: candidate.test_hash }
end
test "can load a live coder question" do
setup_auth candidates(:dawn)
question = questions(:fed7)
get question_path(question.id)
assert_response :success
assert_select '.question-text', question.question
# TODO: add in capybara and test form post
# assert_redirected summary_path
end
end