refactor quiz processing

completes #60
This commit is contained in:
Mark Moser
2016-09-02 17:51:35 -05:00
parent 3f8d089701
commit a977c0ceb3
10 changed files with 62 additions and 87 deletions

View File

@ -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
%>
<div class="form-group-multiples">
<%= 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) %>
</div>
<% end %>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>

View File

@ -7,7 +7,7 @@
checkbox_html = {class: 'checkbox', id: option_id, data: { last: answers.include?(option) ? 'checked' : '' } }
%>
<div class="form-group-multiples">
<%= 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) %>
</div>
<%
@ -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 %>
</div>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>

View File

@ -42,22 +42,22 @@
</div>
<div id="answer<%= question.question_id %>" data-id="live-coder-answer" style="display: none;">
<label for="answer_live_code_text">Enter answer here</label>
<%= text_area_tag 'answer[live_code][text]', value_text, { disabled: true, data: {last: answers['text']}} %>
<label for="answer_answer_hash_text">Enter answer here</label>
<%= text_area_tag 'answer[answer_hash][text]', value_text, { disabled: true, data: {last: answers['text']}} %>
<div class="code-input">
<label for="answer_live_code_html">HTML</label>
<%= text_area_tag 'answer[live_code][html]', value_html, { disabled: true, data: {id: 'code-html', last: answers['html']}, class: 'code-answer code-html' } %>
<label for="answer_answer_hash_html">HTML</label>
<%= text_area_tag 'answer[answer_hash][html]', value_html, { disabled: true, 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][css]', value_css, { disabled: true, data: {id: 'code-css', last: answers['css']}, class: 'code-answer code-css' } %>
<label for="answer_answer_hash_css">CSS</label>
<%= text_area_tag 'answer[answer_hash][css]', value_css, { disabled: true, 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][js]', value_js, { disabled: true, data: {id: 'code-js', last: answers['js']}, class: 'code-answer code-js' } %>
<label for="answer_answer_hash_js">JS</label>
<%= text_area_tag 'answer[answer_hash][js]', value_js, { disabled: true, data: {id: 'code-js', last: answers['js']}, class: 'code-answer code-js' } %>
</div>
<div class="results" data-id="results"></div>

View File

@ -1,12 +1,15 @@
<% question.input_options.each do | option |
option_id = "#{option.parameterize}_#{question.to_i}"
radio_html = {class: 'radio', id: option_id, data: {last: (question.answer == option) ? 'checked' : '' }}
answers = answer.try(:answer) || answer
%>
<% answer = answer.try(:answer) || answer %>
<%= form.collection_radio_buttons(:answer, question.input_options, :to_s, :to_s, {}, {class: 'radio'}) do | option | %>
<%
option_id = "#{question.question_id}#{sanitize_to_id(option.value)}"
checked = answer == option.value ? 'checked' : ''
%>
<div class="form-group-multiples">
<%= radio_button_tag('answer[radio]', option, (question.answer == option), radio_html) %>
<%= label_tag(option_id, option) %>
<%= option.radio_button( id: option_id, checked: checked, data: { last: checked } ) %>
<%= option.label(for: option_id) %>
</div>
<% end %>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>

View File

@ -7,7 +7,7 @@
radio_html = {class: 'radio', id: option_id, data: {last: (answer == option) ? 'checked' : '' }}
%>
<div class="form-group-multiples">
<%= radio_button_tag('answer[with_other][options][]', option, (answer == option), radio_html) %>
<%= radio_button_tag('answer[answer_hash][options][]', option, (answer == option), radio_html) %>
<%= label_tag(option_id, option) %>
</div>
<%
@ -19,9 +19,9 @@
radio_html = {class: 'radio', id: option_id, data: { last: answer }}
text_html = {class: 'input-other', id: "text_#{option_id}", data: { last: other_value }}
%>
<%= radio_button_tag('answer[with_other][options][]', 'other', (answer == 'other'), radio_html) %>
<%= radio_button_tag('answer[answer_hash][options][]', 'other', (answer == 'other'), radio_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 %>
</div>
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>

View File

@ -1,9 +1,7 @@
<%
answers = answer.respond_to?(:answer) ? answer.answer : answer
%>
<% answer = answer.respond_to?(:answer) ? answer.answer : answer %>
<label for="answer_text">Enter answer here</label>
<%= text_area_tag 'answer[text]', answers, {rows: 10, data: { last: answers } } %>
<label for="answer_answer">Enter answer here</label>
<%= text_area_tag 'answer[answer]', answer, {rows: 10, data: { last: answer } } %>
<div class="chars <%= params[:action] == 'summary' ? 'hidden' : '' %>">Characters remaining: <span></span></div>