From a977c0ceb345982ce37e9d7d42deaf2dfe15fcc0 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Fri, 2 Sep 2016 17:51:35 -0500 Subject: [PATCH] refactor quiz processing completes #60 --- Guardfile | 4 +- app/controllers/quiz_controller.rb | 55 ++++++-------------- app/views/quiz/_checkbox.html.erb | 21 ++++---- app/views/quiz/_checkbox_other.html.erb | 6 +-- app/views/quiz/_live_code.html.erb | 16 +++--- app/views/quiz/_radio.html.erb | 17 +++--- app/views/quiz/_radio_other.html.erb | 6 +-- app/views/quiz/_text.html.erb | 8 ++- test/controllers/quiz_controller_test.rb | 14 ++--- test/integration/question_live_coder_test.rb | 2 +- 10 files changed, 62 insertions(+), 87 deletions(-) diff --git a/Guardfile b/Guardfile index a05c48d..73cc130 100644 --- a/Guardfile +++ b/Guardfile @@ -54,7 +54,7 @@ guard 'livereload' do watch(%r{config/locales/.+\.yml}) end -guard :minitest, spring: "bin/rails test" do # all_after_pass: true +guard :minitest, spring: "bin/rails test", all_after_pass: true do watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" } watch(%r{^app/controllers/(admin|application)_controller\.rb$}) { 'test/controllers' } watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" } @@ -71,7 +71,7 @@ end # ESLint guard :shell, all_on_start: true do watch %r{app/assets/javascripts/*/.*} do |file| - system %(echo "ESLinting \033[32m#{file[0]}\033[0m") + system %(echo "ESLint:\033[32m #{file[0]}\033[0m") system %(eslint #{file[0]}) end end diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb index 4f57023..ea7e41c 100644 --- a/app/controllers/quiz_controller.rb +++ b/app/controllers/quiz_controller.rb @@ -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 diff --git a/app/views/quiz/_checkbox.html.erb b/app/views/quiz/_checkbox.html.erb index f1062b3..bda75be 100644 --- a/app/views/quiz/_checkbox.html.erb +++ b/app/views/quiz/_checkbox.html.erb @@ -1,18 +1,15 @@ -<% question.input_options.each_with_index do | option, i | - option_id = "#{question.question_id}_#{i}" +<% answers = Array(question.answer) %> +<%= form.collection_check_boxes(:answer_array, question.input_options, :to_s, :to_s, {}, {class: 'checkbox'}) do | option | %> + <% + option_id = "#{question.question_id}#{sanitize_to_id(option.value)}" + checked = answers.include?(option.value) ? 'checked' : '' + %> - checkbox_html = {class: 'checkbox', - id: "answer_#{option_id}", - name: "answer[checkbox][]", - checked: Array(question.answer).include?(option), - data: { last: Array(question.answer).include?(option) ? 'checked' : '' } - } - answers = answer.try(:answer) || answer - %>
- <%= form.check_box(:answer, checkbox_html, option, '') %> - <%= form.label(option_id, option) %> + <%= option.check_box( id: option_id, checked: checked, data: { last: checked } ) %> + <%= option.label(for: option_id) %>
+ <% end %> <%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %> diff --git a/app/views/quiz/_checkbox_other.html.erb b/app/views/quiz/_checkbox_other.html.erb index 49a04f1..04d11cf 100644 --- a/app/views/quiz/_checkbox_other.html.erb +++ b/app/views/quiz/_checkbox_other.html.erb @@ -7,7 +7,7 @@ checkbox_html = {class: 'checkbox', id: option_id, data: { last: answers.include?(option) ? 'checked' : '' } } %>
- <%= check_box_tag('answer[with_other][options][]', option, answers.include?(option), checkbox_html) %> + <%= check_box_tag('answer[answer_hash][options][]', option, answers.include?(option), checkbox_html) %> <%= label_tag(option_id, option) %>
<% @@ -19,9 +19,9 @@ checkbox_html = {class: 'checkbox', id: option_id, data: { last: answers.include?('other') ? 'checked' : '' } } text_html = {class: 'input-other', id: "text_#{option_id}", data: { last: other_value }} %> - <%= check_box_tag('answer[with_other][options][]', 'other', answers.include?('other'), checkbox_html) %> + <%= check_box_tag('answer[answer_hash][options][]', 'other', answers.include?('other'), checkbox_html) %> <%= label_tag(option_id, 'Other') %> - <%= text_field_tag 'answer[with_other][other]', other_value, text_html %> + <%= text_field_tag 'answer[answer_hash][other]', other_value, text_html %> <%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %> diff --git a/app/views/quiz/_live_code.html.erb b/app/views/quiz/_live_code.html.erb index b3a92b0..69b3ccb 100644 --- a/app/views/quiz/_live_code.html.erb +++ b/app/views/quiz/_live_code.html.erb @@ -42,22 +42,22 @@ <%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %> diff --git a/app/views/quiz/_text.html.erb b/app/views/quiz/_text.html.erb index 8fe7a8c..359f3ff 100644 --- a/app/views/quiz/_text.html.erb +++ b/app/views/quiz/_text.html.erb @@ -1,9 +1,7 @@ -<% - answers = answer.respond_to?(:answer) ? answer.answer : answer -%> +<% answer = answer.respond_to?(:answer) ? answer.answer : answer %> - -<%= text_area_tag 'answer[text]', answers, {rows: 10, data: { last: answers } } %> + +<%= text_area_tag 'answer[answer]', answer, {rows: 10, data: { last: answer } } %>
Characters remaining:
diff --git a/test/controllers/quiz_controller_test.rb b/test/controllers/quiz_controller_test.rb index de17520..843e037 100644 --- a/test/controllers/quiz_controller_test.rb +++ b/test/controllers/quiz_controller_test.rb @@ -24,7 +24,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test "should redirect to saved on save" do setup_auth candidates(:dawn) qid = questions(:fed5).id - post post_answer_path, params: { save: 'Save', answer: { question_id: qid, radio: 'an option' } } + post post_answer_path, params: { save: 'Save', answer: { question_id: qid, answer: 'an option' } } assert_redirected_to saved_path assert session[:test_id].present? @@ -33,7 +33,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test "should redirect to next question on next" do setup_auth candidates(:roy) qid = questions(:fed3).id - params = { submit: 'Next', answer: { question_id: qid, live_code: { text: 'stuff' } } } + params = { submit: 'Next', answer: { question_id: qid, answer_hash: { text: 'stuff' } } } post post_answer_path, params: params assert_redirected_to question_path @@ -61,7 +61,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test "should get flash message on bad radio response" do setup_auth candidates(:dawn) qid = questions(:fed5).id - post post_answer_path, params: { answer: { question_id: qid, radio: nil } } + post post_answer_path, params: { answer: { question_id: qid, answer: nil } } assert_response :success assert session[:test_id].present? @@ -73,7 +73,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test "should get flash message on bad text response" do setup_auth candidates(:dawn) qid = questions(:fed4).id - post post_answer_path, params: { answer: { question_id: qid, text: nil } } + post post_answer_path, params: { answer: { question_id: qid, answer: nil } } assert_response :success assert session[:test_id].present? @@ -85,7 +85,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test "should process checkbox" do setup_auth candidates(:dawn) qid = questions(:fed10).id - post post_answer_path, params: { answer: { question_id: qid, checkbox: 'an-option' } } + post post_answer_path, params: { answer: { question_id: qid, answer_array: 'an-option' } } assert_response :success assert session[:test_id].present? @@ -96,7 +96,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test 'should handle XHR update and complete progress' do setup_auth candidates(:peggy) qid = questions(:fed10).id - post post_answer_path, xhr: true, params: { answer: { question_id: qid, checkbox: ['an-option'] } } + post post_answer_path, xhr: true, params: { answer: { question_id: qid, answer_array: ['an-option'] } } assert_response :success assert_match(/updated successfully/, JSON.parse(@response.body)['message']) @@ -108,7 +108,7 @@ class QuizControllerTest < ActionDispatch::IntegrationTest test 'should handle XHR fail' do setup_auth candidates(:peggy) qid = questions(:fed10).id - post post_answer_path, xhr: true, params: { answer: { question_id: qid, checkbox: nil } } + post post_answer_path, xhr: true, params: { answer: { question_id: qid, answer_array: [nil] } } assert_response 400 assert_match(/select.*answer/i, JSON.parse(@response.body).join) diff --git a/test/integration/question_live_coder_test.rb b/test/integration/question_live_coder_test.rb index 9fec713..59eed19 100644 --- a/test/integration/question_live_coder_test.rb +++ b/test/integration/question_live_coder_test.rb @@ -22,6 +22,6 @@ class QuestionLiveCoderTest < ActionDispatch::IntegrationTest get question_path(question.id) assert_response :success - assert_select '#answer_live_code_html', question.input_options['html'] + assert_select '#answer_answer_hash_html', question.input_options['html'] end end