From 5f395dcf4124c7b8615a3beb20e0384797a38bfc Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Mon, 8 Aug 2016 17:08:20 -0500 Subject: [PATCH] live coder refactor fixes #36 and #50 --- app/assets/javascripts/application.js | 2 +- app/assets/javascripts/live-coder.js.erb | 28 ++------- app/assets/javascripts/summary-edit.js | 10 --- app/assets/javascripts/test-builder.js | 66 -------------------- app/controllers/quiz_controller.rb | 8 +-- app/views/quiz/_checkbox.html.erb | 3 +- app/views/quiz/_live_code.html.erb | 37 ++++++++--- app/views/quiz/_live_code_text.html.erb | 58 ++++++++++++++++- app/views/quiz/_radio.html.erb | 3 +- app/views/quiz/_text.html.erb | 9 ++- app/views/quiz/live_code.html.erb | 18 ------ app/views/quiz/live_code_text.html.erb | 21 ------- app/views/quiz/question.html.erb | 2 +- app/views/quiz/summary.html.erb | 3 +- app/views/review/view.html.erb | 2 +- config/routes.rb | 1 - test/integration/question_live_coder_test.rb | 11 ++++ 17 files changed, 117 insertions(+), 165 deletions(-) delete mode 100644 app/assets/javascripts/test-builder.js delete mode 100644 app/views/quiz/live_code.html.erb delete mode 100644 app/views/quiz/live_code_text.html.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 71d4d97..e716f6f 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -15,9 +15,9 @@ //= require turbolinks //= require modernizr-lite/modernizr //= require jquery-linedtextarea-moser + //= require button-group //= require form-animation //= require summary-edit -//= require test-builder //= require textarea-limit //= require live-coder diff --git a/app/assets/javascripts/live-coder.js.erb b/app/assets/javascripts/live-coder.js.erb index 0847e6e..ffc2a64 100644 --- a/app/assets/javascripts/live-coder.js.erb +++ b/app/assets/javascripts/live-coder.js.erb @@ -1,5 +1,4 @@ -function updateResults(elem) { - var resultsContainer = $(elem).find('[data-id="results"]')[0]; +function updateResults(elem) { var resultsContainer = $(elem).find('[data-id="results"]')[0]; var codeHtml = $(elem).find('.code-html')[0].value.trim(); var codeCss = $(elem).find('.code-css')[0].value.trim(); var codeJs = $(elem).find('.code-js')[0].value.trim(); @@ -92,22 +91,8 @@ function indentSelection(e){ } } -function loadLiveCoders(){ - $.each($('.answer-sec.live_code-type, .answer-sec.live_code_text-type'), function(index, elem){ - var qid = $(elem).data('qid'); - $(elem).find("[data-id='live-coder-answer']").load("/live-coder-entry/" + qid, function(){ - $(elem).find('.js-error').addClass('hidden'); - $(elem).find(".code-input textarea").linedtextarea(); - if(window.location.href.indexOf("summary") > -1) { - $(elem).find(".code-input textarea").attr('disabled', true); - } - updateResults(this); - }); - }); -} timer = 0; - $(function(){ // wait a half second before updating results // restart the timer if they resume typing @@ -117,14 +102,13 @@ $(function(){ timer = setTimeout(updateResults(elem), 500); }); + $("[data-id=live-coder-answer]").each(function(){ + updateResults(this); + }); + $("html").on('keydown', "textarea[data-id^=code-]", function(e){ indentSelection(e); }); - $.when(loadLiveCoders()).done(function(){ - //simple live coder for summary page - $("[data-id=live-coder-no-js], [data-id=live-coder-finish-later]").addClass('hidden'); - $("[data-id=live-coder]").removeClass('hidden'); - }); - + $(".code-input textarea").linedtextarea(); }); diff --git a/app/assets/javascripts/summary-edit.js b/app/assets/javascripts/summary-edit.js index 4f94d31..2611acc 100644 --- a/app/assets/javascripts/summary-edit.js +++ b/app/assets/javascripts/summary-edit.js @@ -177,16 +177,6 @@ $('.answer-block').prop('disabled', true); // Question events $('.answer-sec') .find('.button-cancel, .button-save').hide().end() - // delegating events .on('click', '.button-edit', editClickHandler) .on('click', '.button-cancel', cancelClickHandler) .on('click', '.button-save', saveClickHandler); - - // Dynamically load in coders -$.each($('.answer-sec.live_code-type, .answer-sec.live_code_text-type'), function(index, elem){ - var qid = $(elem).data('qid'); - $(elem).find("[data-id='live-coder-answer']").load("/live-coder-entry/" + qid, function(){ - $(elem).find('.js-error').addClass('hidden'); - $(elem).find(".code-input textarea").linedtextarea(); - }); -}); diff --git a/app/assets/javascripts/test-builder.js b/app/assets/javascripts/test-builder.js deleted file mode 100644 index 25fca79..0000000 --- a/app/assets/javascripts/test-builder.js +++ /dev/null @@ -1,66 +0,0 @@ -$(document).ready(function() { - //$(".answer_container").hide(); - //$(".test-builder-success").hide(); - //$(".question-edit-container").hide(); - - $(".skills-app-form").each(function () { - var thisQu = $(this), - questionOf = thisQu.find(".question-block").text(); - - - thisQu.find(".testbuilderquestion").val(questionOf); - - thisQu.find(".answer_type").change(function(){ - if ( $(this).val() == "1" ) { - thisQu.find(".answer_container").hide(); - thisQu.find(".answer_container.answer_text").show(); - } - if ( $(this).val() == "2" ) { - thisQu.find(".answer_container").hide(); - thisQu.find(".answer_container.answer_multiple").show(); - } - if ( $(this).val() == "3" ) { - thisQu.find(".answer_container").hide(); - thisQu.find(".answer_container.answer_multiple").show(); - } - }); - thisQu.find(".test-builder-edit").click(function(){ - thisQu.find(".question-display").slideUp().fadeOut(); - thisQu.find(".question-edit-container").slideDown().fadeIn(); - }); - thisQu.find(".btn-saveedit").click(function(){ - if(thisQu.valid()) { - thisQu.find(".question-display").slideDown().fadeIn(); - thisQu.find(".question-edit-container").slideUp().fadeOut(); - thisQu.find(".test-builder-success").slideDown().fadeIn(); - thisQu.find(".test-builder-success").delay(3000).slideUp().fadeOut(); - } - }); - thisQu.find(".btn-cancel-edit").click(function(){ - thisQu.find(".question-display").slideDown().fadeIn(); - thisQu.find(".question-edit-container").slideUp().fadeOut(); - }); - thisQu.find(".btn-cancel").click(function(){ - thisQu.find(".answer_container").hide(); - }); - thisQu.find(".skills-app-form").submit(function() { - return false; - }); - thisQu.find(".test-builder-save").click(function(){ - if(thisQu.valid()) { - thisQu.find(".answer_container").hide(); - thisQu.find(".test-builder-success").slideDown(); - thisQu.find(".test-builder-success").delay(3000).slideUp(); - } - else { - thisQu.find(".form-validation-script:invalid").css("border-color", "red"); - } - }); - thisQu.find( ".btn-save" ).click(function() { - if(thisQu.valid()) { - window.location.href = "test-builder-success.html"; - } - }); - }); - -}); \ No newline at end of file diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb index 078b029..e87593d 100644 --- a/app/controllers/quiz_controller.rb +++ b/app/controllers/quiz_controller.rb @@ -5,6 +5,7 @@ class QuizController < ApplicationController qid = prep_status.current_question_id || params[:question_id] redirect_to :summary and return if qid.nil? prep_question qid + @answer = prep_answer qid prep_instance_answer @question end @@ -14,13 +15,6 @@ class QuizController < ApplicationController 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? diff --git a/app/views/quiz/_checkbox.html.erb b/app/views/quiz/_checkbox.html.erb index e63e23c..4e8a2b5 100644 --- a/app/views/quiz/_checkbox.html.erb +++ b/app/views/quiz/_checkbox.html.erb @@ -7,6 +7,7 @@ name: "answer[checkbox][]", checked: Array(question.answer).include?(option) } + answers = answer.try(:answer) || answer %>
<%= form.check_box(:answer, checkbox_html, option, '') %> @@ -14,4 +15,4 @@
<% end %> -<%= render partial: "quiz/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.html.erb b/app/views/quiz/_live_code.html.erb index a1df21f..20de13f 100644 --- a/app/views/quiz/_live_code.html.erb +++ b/app/views/quiz/_live_code.html.erb @@ -1,19 +1,17 @@ <% option_id = "#{question.question_id}_finish-later" - checkbox_html = {class: 'checkbox', id: "answer_#{option_id}", name: "answer[#{question.input_type}][later]", checked: Array(question.answer).include?('finish-later') } + disabled = local_assigns.fetch :disable_input, false + answers = answer.try(:answer) || answer %> - -<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %> +<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %> -
+

This is a question where you will be asked to write code, and it utilizes a JavaScript-enabled environment. It looks like JavaScript is not loaded. @@ -30,8 +28,27 @@ <% end %>

-
- <% # answers inserted dynamically via jQuery .load() - # to check for JS capability/enabled - %> + + + diff --git a/app/views/quiz/_live_code_text.html.erb b/app/views/quiz/_live_code_text.html.erb index 1349b48..636234a 100644 --- a/app/views/quiz/_live_code_text.html.erb +++ b/app/views/quiz/_live_code_text.html.erb @@ -1 +1,57 @@ -<%= render partial: "quiz/live_code", locals: {question: question, form: form} %> +<% + option_id = "#{question.question_id}_finish-later" + checkbox_html = {class: 'checkbox', + id: "answer_#{option_id}", + name: "answer[#{question.input_type}][later]", + checked: Array(question.answer).include?('finish-later') + } + disabled = local_assigns.fetch :disable_input, false + answers = answer.try(:answer) || answer +%> + +<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %> + +
+

+ This is a question where you will be asked to write code, and it utilizes a JavaScript-enabled + environment. It looks like JavaScript is not loaded. + <% unless params[:action] == 'summary' %> + Please check the box below to acknowledge that you agree to come back at a later time to finish + answering this question before you can submit the test. + <% else %> + You will need to enable JavaScript and answer this question before you can submit the test. + <% end %> +

+ <% unless params[:action] == 'summary' %> + <%= form.check_box(:answer, checkbox_html, true, '') %> + <%= form.label(option_id, 'I will come back later to finish this code question') %> + <% end %> +
+ + + + diff --git a/app/views/quiz/_radio.html.erb b/app/views/quiz/_radio.html.erb index 89a2cf6..daa7bdf 100644 --- a/app/views/quiz/_radio.html.erb +++ b/app/views/quiz/_radio.html.erb @@ -2,6 +2,7 @@ question.input_options.each do | option | option_id = "#{option.parameterize}_#{question.to_i}" radio_html = {class: 'radio', id: option_id} + answers = answer.try(:answer) || answer %>
<%= radio_button_tag('answer[radio]', option, (question.answer == option), radio_html) %> @@ -9,4 +10,4 @@
<% end %> -<%= render partial: "quiz/answer_errors", locals: {question: question, answer: @answer} %> +<%= 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 ba6b64d..8c4b484 100644 --- a/app/views/quiz/_text.html.erb +++ b/app/views/quiz/_text.html.erb @@ -1,8 +1,11 @@ -<% hidden = params[:action] == 'summary' ? 'hidden' : '' %> +<% + hidden = params[:action] == 'summary' ? 'hidden' : '' + answers = answer.try(:answer) || answer +%> - + -<%= render partial: "quiz/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.html.erb b/app/views/quiz/live_code.html.erb deleted file mode 100644 index 5145907..0000000 --- a/app/views/quiz/live_code.html.erb +++ /dev/null @@ -1,18 +0,0 @@ -<% answer = @answer.answer || {} %> - -
- - <%= text_area_tag 'answer[live_code][html]', (answer['html']), { 'data-id' => 'code-html', class: 'code-answer code-html' } %> -
- -
- - <%= text_area_tag 'answer[live_code][css]', (answer['css']), { 'data-id' => 'code-css', class: 'code-answer code-css' } %> -
- -
- - <%= text_area_tag 'answer[live_code][js]', (answer['js']), { 'data-id' => 'code-js', class: 'code-answer code-js' } %> -
- -
diff --git a/app/views/quiz/live_code_text.html.erb b/app/views/quiz/live_code_text.html.erb deleted file mode 100644 index 6041199..0000000 --- a/app/views/quiz/live_code_text.html.erb +++ /dev/null @@ -1,21 +0,0 @@ -<% answer = @answer.answer || {} %> - - -<%= text_area_tag 'answer[live_code_text][text]', (answer['text']) %> - -
- - <%= text_area_tag 'answer[live_code_text][html]', (answer['html']), { 'data-id' => 'code-html', class: 'code-answer code-html' } %> -
- -
- - <%= text_area_tag 'answer[live_code_text][css]', (answer['css']), { 'data-id' => 'code-css', class: 'code-answer code-css' } %> -
- -
- - <%= text_area_tag 'answer[live_code_text][js]', (answer['js']), { 'data-id' => 'code-js', class: 'code-answer code-js' } %> -
- -
diff --git a/app/views/quiz/question.html.erb b/app/views/quiz/question.html.erb index 4b175d3..21cd044 100644 --- a/app/views/quiz/question.html.erb +++ b/app/views/quiz/question.html.erb @@ -16,7 +16,7 @@
<%= hidden_field_tag 'answer[question_id]', @question.question_id %> <%= hidden_field_tag 'answer[answer_id]', @question.answer_id %> - <%= render partial: @question.input_type, locals: {question: @question, form: form} %> + <%= render partial: @question.input_type, locals: {question: @question, form: form, answer: @answer } %>
<% if @status.on_summary %> diff --git a/app/views/quiz/summary.html.erb b/app/views/quiz/summary.html.erb index 8e44fbc..b843f87 100644 --- a/app/views/quiz/summary.html.erb +++ b/app/views/quiz/summary.html.erb @@ -34,7 +34,8 @@
<%= hidden_field_tag 'answer[question_id]', question.question_id %> - <%= render partial: question.input_type, locals: {question: question, form: form} %> + <%= hidden_field_tag 'answer[answer_id]', question.answer_id %> + <%= render partial: question.input_type, locals: {question: question, form: form, answer: question.answer, disable_input: true} %>
diff --git a/app/views/review/view.html.erb b/app/views/review/view.html.erb index 10f43aa..6030fa6 100644 --- a/app/views/review/view.html.erb +++ b/app/views/review/view.html.erb @@ -21,7 +21,7 @@ <% end %>
<%= hidden_field_tag 'answer[question_id]', question.question_id %> - <%= render partial: "quiz/#{question.input_type}", locals: {question: question, form: form} %> + <%= render partial: "quiz/#{question.input_type}", locals: {question: question, answer: question.answer, form: form} %>
diff --git a/config/routes.rb b/config/routes.rb index 2e7b359..847837f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,6 @@ Rails.application.routes.draw do 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 diff --git a/test/integration/question_live_coder_test.rb b/test/integration/question_live_coder_test.rb index b61b19f..f0b343a 100644 --- a/test/integration/question_live_coder_test.rb +++ b/test/integration/question_live_coder_test.rb @@ -15,4 +15,15 @@ class QuestionLiveCoderTest < ActionDispatch::IntegrationTest # TODO: add in capybara and test form post # assert_redirected summary_path end + + test "can load a live coder TEXT question" do + setup_auth candidates(:dawn) + question = questions(:fed3) + + 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