introduce check_other and radio_other question types
completes issue #48
This commit is contained in:
@ -51,6 +51,7 @@ class QuizController < ApplicationController
|
||||
:radio,
|
||||
:text,
|
||||
checkbox: [],
|
||||
with_other: [:other, options: []],
|
||||
live_code: [:later, :html, :css, :js, :text]
|
||||
)
|
||||
end
|
||||
@ -110,4 +111,11 @@ class QuizController < ApplicationController
|
||||
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
|
||||
|
@ -22,7 +22,9 @@ module ApplicationHelper
|
||||
options_for_select([
|
||||
%w(Text text),
|
||||
%w(Radio radio),
|
||||
['Radio with other', 'radio_other'],
|
||||
%w(Checkbox checkbox),
|
||||
['Checkbox with other', 'checkbox_other'],
|
||||
%w(Coder live_code)
|
||||
], selected: (val.blank? ? '' : val))
|
||||
end
|
||||
|
@ -38,8 +38,23 @@ class AnswerFormatValidator < ActiveModel::EachValidator
|
||||
record.errors[attribute] << (options[:message] || live_code_error_message(value))
|
||||
end
|
||||
|
||||
def with_other record, attribute, value
|
||||
return if value.present? && with_other_check(value)
|
||||
|
||||
record.errors[attribute] << (options[:message] || "Please select or provide an answer.")
|
||||
end
|
||||
alias radio_other with_other
|
||||
alias checkbox_other with_other
|
||||
|
||||
#################################
|
||||
|
||||
def with_other_check value
|
||||
return false unless value.respond_to? :keys
|
||||
return false if Array(value[:options]).join.blank?
|
||||
return false if value[:options].include?('other') && value[:other].to_s.blank?
|
||||
true
|
||||
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."
|
||||
|
21
app/views/admin/question/_checkbox_other.html.erb
Normal file
21
app/views/admin/question/_checkbox_other.html.erb
Normal file
@ -0,0 +1,21 @@
|
||||
<strong>Checkbox Options</strong>
|
||||
|
||||
<ul data-id="input_option_list">
|
||||
<% question.input_options.each do | option | %>
|
||||
<li>
|
||||
<%= text_field_tag 'question[multi_choice][]', option, { disabled: (disable ||= false), data: { last: option } } %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Other: <input type="text" disabled="disabled" /></li>
|
||||
</ul>
|
||||
|
||||
<% unless (disable ||= false) %>
|
||||
<div class="form-group">
|
||||
<div class="btn tertiary-btn" data-id="input_option_adder"> Add option </div>
|
||||
<li style="display: none;">
|
||||
<%= text_field_tag 'question[multi_choice][]', nil, { disabled: (disable ||= false), data: { last: nil } } %>
|
||||
</li>
|
||||
</div>
|
||||
<% end %>
|
21
app/views/admin/question/_radio_other.html.erb
Normal file
21
app/views/admin/question/_radio_other.html.erb
Normal file
@ -0,0 +1,21 @@
|
||||
<strong>Radio Options</strong>
|
||||
|
||||
<ul data-id="input_option_list">
|
||||
<% question.input_options.each do | option | %>
|
||||
<li>
|
||||
<%= text_field_tag 'question[multi_choice][]', option, { disabled: (disable ||= false), data: { last: option } } %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Other: <input type="text" disabled="disabled" /></li>
|
||||
</ul>
|
||||
|
||||
<% unless (disable ||= false) %>
|
||||
<div class="form-group">
|
||||
<div class="btn tertiary-btn" data-id="input_option_adder"> Add option </div>
|
||||
<li style="display: none;">
|
||||
<%= text_field_tag 'question[multi_choice][]', nil, { disabled: (disable ||= false), data: { last: nil } } %>
|
||||
</li>
|
||||
</div>
|
||||
<% end %>
|
27
app/views/quiz/_checkbox_other.html.erb
Normal file
27
app/views/quiz/_checkbox_other.html.erb
Normal file
@ -0,0 +1,27 @@
|
||||
<%
|
||||
answers = question.answer.nil? ? [] : Array(question.answer['options'])
|
||||
other_value = question.answer.nil? ? '' : question.answer['other']
|
||||
|
||||
question.input_options.each do | option |
|
||||
option_id = "#{option.parameterize}_#{question.to_i}"
|
||||
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) %>
|
||||
<%= label_tag(option_id, option) %>
|
||||
</div>
|
||||
<%
|
||||
end %>
|
||||
|
||||
<div class="form-group-multiples">
|
||||
<%
|
||||
option_id = "other_#{question.to_i}"
|
||||
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) %>
|
||||
<%= label_tag(option_id, 'Other') %>
|
||||
<%= text_field_tag 'answer[with_other][other]', other_value, text_html %>
|
||||
</div>
|
||||
|
||||
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>
|
27
app/views/quiz/_radio_other.html.erb
Normal file
27
app/views/quiz/_radio_other.html.erb
Normal file
@ -0,0 +1,27 @@
|
||||
<%
|
||||
answer = question.answer.nil? ? '' : Array(question.answer['options']).first
|
||||
other_value = question.answer.nil? ? '' : question.answer['other']
|
||||
|
||||
question.input_options.each do | option |
|
||||
option_id = "#{option.parameterize}_#{question.to_i}"
|
||||
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) %>
|
||||
<%= label_tag(option_id, option) %>
|
||||
</div>
|
||||
<%
|
||||
end %>
|
||||
|
||||
<div class="form-group-multiples">
|
||||
<%
|
||||
option_id = "other_#{question.to_i}"
|
||||
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) %>
|
||||
<%= label_tag option_id, 'Other' %>
|
||||
<%= text_field_tag 'answer[with_other][other]', other_value, text_html %>
|
||||
</div>
|
||||
|
||||
<%= render partial: "quiz/answer_errors", locals: {question: question, answer: answer} %>
|
Reference in New Issue
Block a user