Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAW - Dependent Questions Bug Fixes #2053

Merged
merged 1 commit into from Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/assets/javascripts/surveyor/responses.js.coffee
Expand Up @@ -31,11 +31,12 @@ $(document).ready ->
$(this).find('input').prop('checked', true)

$(document).on 'change', '.option input', ->
if ($(this).prop('type') == 'checkbox' && $(this).prop('id').endsWith('for_dependent')) || $(this).prop('type') == 'radio'
if $(this).prop('type') == 'checkbox' || $(this).prop('type') == 'radio'
question_id = $(this).parents('.option').data('question-id')
option_id = $(this).parents('.option').data('option-id')

$(".dependent-for-question-#{question_id}").addClass('d-none')
if $(this).prop('type') == 'radio'
$(".dependent-for-question-#{question_id}").addClass('d-none')

if $(this).is(":checked")
$(".dependent-for-option-#{option_id}").removeClass('d-none')
Expand Down
6 changes: 5 additions & 1 deletion app/models/question_response.rb
Expand Up @@ -86,11 +86,15 @@ def must_be_answered?
end

def depender_selected?
self.depender && self.response.question_responses.detect{ |qr| qr.question_id == self.depender.question_id }.try(:content).try(:downcase) == self.depender.content.downcase
self.depender && split_options(self.response.question_responses.detect{ |qr| qr.question_id == self.depender.question_id }.try(:content).try(:downcase)).include?(self.depender.content.downcase)
end

private

def split_options(content)
!content.nil? && content.start_with?("[\"") && content.end_with?("\"]") ? content.tr("[]\"", "").split(',').map(&:strip) : content
end

def remove_unanswered
return false if self.required? && self.content.blank?
end
Expand Down
Expand Up @@ -21,7 +21,7 @@
- question.options.each do |option|
.form-check.option.mb-1{ data: { question_id: question.id, option_id: option.id } }
- if ['new', 'edit', 'preview'].include?(action_name)
= qr.check_box :content, { multiple: true, class: 'form-check-input mt-1' }, option.content, nil
= qr.check_box :content, { multiple: true, checked: question_response.content.nil? ? false : multiple_select_formatter(question_response.content.try(:downcase)).include?(option.content.downcase), class: 'form-check-input mt-1' }, option.content, nil
.form-check-label
= option.content
- else
Expand Down