completes #62 - merges live_coder_text into live_coder

This commit is contained in:
Mark Moser 2016-08-11 17:44:30 -05:00
parent 33992d73c6
commit ccd287491d
6 changed files with 13 additions and 135 deletions

View File

@ -51,8 +51,7 @@ class QuizController < ApplicationController
:radio,
:text,
checkbox: [],
live_code: [:later, :html, :css, :js],
live_code_text: [:later, :html, :css, :js, :text]
live_code: [:later, :html, :css, :js, :text]
)
end

View File

@ -36,37 +36,12 @@ class AnswerFormatValidator < ActiveModel::EachValidator
record.errors[attribute] << (options[:message] || live_code_error_message(value))
end
def live_code_text record, attribute, value
return if value.present? && live_code_text_value_check(value)
record.errors[attribute] << (options[:message] || live_code_text_error_message(value))
end
#################################
def live_code_text_error_message value
return 'You must write code in one of the above textareas to progress.' if value.nil?
code_present = value.values_at(:html, :css, :js).join.present?
reason_present = value[:text].present?
later_present = value.keys.count == 1
return 'Please check that you will come back to complete the code example.' if later_present
return 'You must write code in one of the above textareas to progress.' unless code_present
return 'You must provide an answer in the reason field.' unless reason_present
end
def live_code_error_message value
if value.present? && value.keys.count == 1
return "Please check that you will come back to complete the code example."
end
"You must write code in one of the above textareas to progress."
end
def live_code_text_value_check value
later_present = value[:later].present? && value.keys.count == 1
code_present = value.values_at(:html, :css, :js).join.present?
reason_present = value[:text].present?
later_present || (code_present && reason_present)
"You must write comments or code in one of the textareas to progress."
end
end

View File

@ -30,9 +30,12 @@
</div>
<div id="answer<%= question.question_id %>" data-id="live-coder-answer" style="display: none;">
<label for="answer_live_code">Enter answer here</label>
<%= text_area_tag 'answer[live_code][text]', (answers['text']), {data: {last: answers['text']}} %>
<div class="code-input">
<label for="answer_live_code_html">HTML</label>
<%= text_area_tag 'answer[live_code][html]', (answers['html']), { data: {id: 'code-html', last: answers['html'] }, class: 'code-answer code-html' } %>
<%= text_area_tag 'answer[live_code][html]', (answers['html']), { data: {id: 'code-html', last: answers['html']}, class: 'code-answer code-html' } %>
</div>
<div class="code-input">

View File

@ -1,58 +0,0 @@
<%
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')
}
answers = answer.try(:answer) || answer
%>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>
<div id="nojs<%= question.question_id %>" data-id="live-coder-finish-later">
<noscript>
<p class="warning">
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 %>
</p>
<% 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 %>
</noscript>
</div>
<div id="answer<%= question.question_id %>" data-id="live-coder-answer" style="display: none;">
<label for="answer_live_code_text">Reasoning</label>
<%= text_area_tag 'answer[live_code_text][text]', (answers['text']), {data: {last: answers['text']}} %>
<div class="code-input">
<label for="answer_live_code_html">HTML</label>
<%= text_area_tag 'answer[live_code_text][html]', (answers['html']), { data: {id: 'code-html', last: answers['html']}, class: 'code-answer code-html' } %>
</div>
<div class="code-input">
<label for="answer_live_code_css">CSS</label>
<%= text_area_tag 'answer[live_code_text][css]', (answers['css']), { data: {id: 'code-css', last: answers['css']}, class: 'code-answer code-css' } %>
</div>
<div class="code-input">
<label for="answer_live_code_js">JS</label>
<%= text_area_tag 'answer[live_code_text][js]', (answers['js']), { data: {id: 'code-js', last: answers['js']}, class: 'code-answer code-js' } %>
</div>
<div class="results" data-id="results"></div>
</div>
<script>
<% # removes the no-js message SUPER FAST %>
document.getElementById("nojs<%= question.question_id %>").style.display = "none";
document.getElementById("answer<%= question.question_id %>").style.display = "";
</script>

View File

@ -22,7 +22,7 @@ fed3:
quiz: fed
question: How would you create a widget that would fit in a 250px wide area as well as a 400px wide area?
category: CSS
input_type: live_code_text
input_type: live_code
input_options:
sort: 2
active: true

View File

@ -117,7 +117,7 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase
# input_type LIVE CODER
test "live_code should PASS with populated hash" do
obj = AnswerValidatable.new('live_code')
obj.answer = { html: 'this is html', css: '', js: '' }
obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }
assert obj.valid?
assert obj.errors.messages.empty?
@ -149,58 +149,17 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase
test "live_code should FAIL without values" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "", html: "", css: "", js: "" }
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
# input_type LIVE CODER TEXT
test "live_code_text should PASS with populated hash" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code_text should PASS with finish later" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "true" }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code_text should FAIL with nil" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = nil
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code_text should FAIL without checking finish later" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "" }
refute obj.valid?
assert_match(/come back/, obj.errors.messages[:answer][0])
end
test "live_code_text should FAIL without values" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "", html: "", css: "", js: "", text: "" }
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code_text should FAIL without text" do
obj = AnswerValidatable.new('live_code_text')
obj.answer = { later: "", html: "some html", css: "", js: "", text: "" }
test "live_code should PASS with text only" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "", html: "", css: "", js: "", text: "reasons" }
refute obj.valid?
assert_match(/provide.*reason/, obj.errors.messages[:answer][0])
assert obj.valid?
assert obj.errors.messages.empty?
end
end