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

KG - Survey Dependency Bugs / Updates #295

Merged
merged 9 commits into from
May 30, 2017
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
22 changes: 1 addition & 21 deletions app/assets/javascripts/surveyor/surveys.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,7 @@ $(document).ready ->
type: 'get'
url: "/surveyor/surveys/#{survey_id}/preview.js"

### Survey Form ###
options = {
text: 'text', email: 'text', zipcode: 'text', time: 'time', phone: 'text',
textarea: 'textarea', yes_no: 'yes_no', state: 'dropdown', country: 'dropdown',
date: 'date', number: 'number'
}

$(document).on 'change', '.select-question-type', ->
send_update_request($(this), $(this).val())

question_id = $(this).data('question-id')

if options[$(this).val()]
option_type = options[$(this).val()].replace('_', '-')
$(".question-options[data-question-id=#{question_id}]").addClass('hidden')
$(".question-options.#{option_type}-options[data-question-id=#{question_id}]").removeClass('hidden')
else
$(".question-options[data-question-id=#{question_id}]").addClass('hidden')
$(".question-options.customize-options[data-question-id=#{question_id}]").removeClass('hidden')

$(document).on 'change', '.select-depender', ->
$(document).on 'change', '.select-depender, .select-question-type', ->
send_update_request($(this), $(this).val())

$(document).on 'focusout', '#survey-modal input[type="text"], #survey-modal textarea', ->
Expand Down
8 changes: 8 additions & 0 deletions app/assets/stylesheets/surveyor/surveys.sass
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
.form-group, .section, .question, .option
display: inline-block
width: 100%

#survey-active,
.radio-example,
.checkbox-example
margin-top: 0

.radio-label
margin-top: 10px

.add-section span, .add-question span
margin-right: 5px
Expand Down
22 changes: 22 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class Question < ActiveRecord::Base

belongs_to :section
belongs_to :depender, class_name: 'Option'

Expand All @@ -37,4 +38,25 @@ class Question < ActiveRecord::Base
inclusion: { in: [true, false] }

accepts_nested_attributes_for :options, allow_destroy: true

after_update :update_options_based_on_question_type, if: :question_type_changed?

def previous_questions
self.survey.questions.where("questions.id < ?", self.id)
end

def is_dependent?
self.is_dependent && self.depender_id.present?
end

private

def update_options_based_on_question_type
self.options.destroy_all

if self.question_type == 'yes_no'
self.options.create(content: 'Yes')
self.options.create(content: 'No')
end
end
end
2 changes: 1 addition & 1 deletion app/views/surveyor/options/create.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$(".question-<%=@question.id%>-options").append("<%= j render 'surveyor/surveys/form/survey_option', question: @question, option: @option %>")
$(".question-options[data-question-id='<%=@question.id%>']").html('<%= j render "surveyor/surveys/form/form_partials/#{@question.question_type}_example", question: @question %>')
build_dependents_selectpicker("<%=@option.survey.id%>")
2 changes: 1 addition & 1 deletion app/views/surveyor/options/destroy.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$(".option-<%=@option.id%>").remove()
$(".question-options[data-question-id='<%=@option.question.id%>']").html('<%= j render "surveyor/surveys/form/form_partials/#{@option.question.question_type}_example", question: @option.question %>')
build_dependents_selectpicker("<%=@option.survey.id%>")
3 changes: 3 additions & 0 deletions app/views/surveyor/questions/destroy.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if $(".question-<%=@question.id%>").is(':first-child')
$(".question-<%=@question.id%>").next().find('#question-is_dependent').prop('disabled', 'disabled')
$(".question-<%=@question.id%>").remove()
build_dependents_selectpicker("<%=@question.survey.id%>")
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- question.options.each_with_index do |option, index|
.option.likert-group.no-padding{ class: "col-sm-#{(12 / question.options.count).ceil}"}
.option.likert-group.no-padding{ class: "col-sm-#{(12 / question.options.count).ceil}", data: { question_id: question.id, option_id: option.id } }
.col-sm-12.text-center.no-padding
= index+1
.col-sm-12.text-center.no-padding.likert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- if ['new', 'preview'].include?(action_name)
= qr.label :content, class: 'radio-inline option' do
= qr.radio_button :content, 'yes'
= t(:constants)[:yes_select]
= qr.label :content, class: 'radio-inline option' do
= qr.radio_button :content, 'no'
= t(:constants)[:no_select]
- else
%label.radio-inline.option
= radio_button_tag :content_yes_no, 'yes', qr.content == 'yes', disabled: true
= t(:constants)[:yes_select]
%label.radio-inline.option
= radio_button_tag :content_yes_no, 'no', qr.content == 'no', disabled: true
= t(:constants)[:no_select]
- question.options.each do |option|
- if ['new', 'preview'].include?(action_name)
= qr.label :content, class: 'radio-inline option', data: { question_id: question.id, option_id: option.id } do
= qr.radio_button :content, option.content.downcase
= option.content
- else
%label.radio-inline.option
= radio_button_tag :content_yes_no, option.content.downcase, qr.content == option.content.downcase, disabled: true
= option.content
9 changes: 7 additions & 2 deletions app/views/surveyor/survey_updater/update.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ if !$("#<%=@klass%>-<%=error[0]%>").parents('.form-group').hasClass('has-error')
<% else %>
$("#<%=@klass%>-<%=@field%>").parents('.form-group').removeClass('has-error')
$("#<%=@klass%>-<%=@field%>").parents('.form-group').find('.help-block').remove()
<% end %>

<% if @klass == 'survey' %>
$('.survey-table').bootstrapTable('refresh')
<% end %>
<% end %>

<% if @klass == 'question' %>
$(".question-options[data-question-id='<%=@object.id%>']").html('<%= j render "surveyor/surveys/form/form_partials/#{@object.question_type}_example", question: @object %>')
$('.selectpicker').selectpicker()
<% end %>
<% end %>
10 changes: 5 additions & 5 deletions app/views/surveyor/surveys/form/_dependent_dropdown.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%select.selectpicker.select-depender#question-depender_id{ selected: question.depender_id }
%select.selectpicker.select-depender#question-depender_id
%option{ value: "" }
= t(:constants)[:prompts][:none]
- survey.questions.where.not(id: question.id).each do |question|
%optgroup{ label: question.content }
- question.options.each do |option|
%option{ value: option.id }
- question.previous_questions.each do |q|
%optgroup{ label: q.content }
- q.options.each do |option|
%option{ value: option.id, selected: question.depender_id == option.id }
= option.content
23 changes: 4 additions & 19 deletions app/views/surveyor/surveys/form/_survey_question.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

.question{ class: "question-#{question.id}", data: { question_id: question.id } }
%br
%hr
Expand All @@ -36,7 +37,7 @@
= check_box_tag "question-required", question.required, question.required, class: 'form-control'
= label_tag "question-is_dependent", t(:surveyor)[:surveys][:form][:content][:question][:dependent][:label], class: 'col-sm-2 control-label no-padding-x text-right'
.col-sm-1.no-padding
= check_box_tag "question-is_dependent", question.is_dependent, question.is_dependent, class: 'form-control is-dependent'
= check_box_tag "question-is_dependent", question.is_dependent, question.is_dependent, class: 'form-control is-dependent', disabled: question.previous_questions.empty?
.dependent-dropdown-container{ class: question.is_dependent ? '' : 'hidden' }
= label_tag "question-depender_id", t(:surveyor)[:surveys][:form][:content][:question][:dependent][:dropdown][:label], class: 'col-sm-2 control-label no-padding-x'
.col-sm-4.no-padding
Expand All @@ -46,21 +47,5 @@
= link_to surveyor_question_path(question), method: :delete, remote: true, class: 'btn btn-danger pull-right delete-question', title: t(:surveyor)[:surveys][:form][:content][:question][:delete], data: { toggle: 'tooltip', animation: 'false' } do
%span.glyphicon.glyphicon-trash

-# Render disabled example content so the user can see what the question might look like
= render 'surveyor/surveys/form/form_partials/text_example', question: question
= render 'surveyor/surveys/form/form_partials/textarea_example', question: question
= render 'surveyor/surveys/form/form_partials/yes_no_example', question: question
= render 'surveyor/surveys/form/form_partials/date_example', question: question
= render 'surveyor/surveys/form/form_partials/time_example', question: question
= render 'surveyor/surveys/form/form_partials/number_example', question: question
= render 'surveyor/surveys/form/form_partials/dropdown_example', question: question

.question-options.customize-options{ class: display_question_example(question, 'custom'), data: { question_id: question.id } }
%hr
.col-sm-12
%div{ class: "question-#{question.id}-options" }
- question.options.each do |option|
= render 'surveyor/surveys/form/survey_option', survey: survey, section: section, question: question, option: option

.form-group
= link_to t(:surveyor)[:surveys][:form][:content][:option][:add], surveyor_options_path(question_id: question.id), method: :post, remote: true, class: 'btn btn-sm btn-primary add-option'
.question-options{ data: { question_id: question.id } }
= render "surveyor/surveys/form/form_partials/#{question.question_type}_example", question: question
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,5 @@
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-# Render disabled example content so the user can see what the question might look like
.option{ class: "option-#{option.id}", data: { option_id: option.id } }
.form-group
.col-lg-11.no-padding
= text_field_tag "option-content", option.content, class: 'form-control option-content', placeholder: t(:surveyor)[:surveys][:form][:content][:option][:content][:placeholder], maxlength: 255
.col-lg-1.no-padding
= link_to surveyor_option_path(option), method: :delete, remote: true, class: 'btn btn-danger pull-right delete-option', title: t(:surveyor)[:surveys][:form][:content][:option][:delete], data: { toggle: 'tooltip', animation: 'false' } do
%span.glyphicon.glyphicon-trash
.form-group
= link_to t(:surveyor)[:surveys][:form][:content][:option][:add], surveyor_options_path(question_id: question.id), method: :post, remote: true, class: 'btn btn-sm btn-primary add-option'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-# Copyright © 2011-2016 MUSC Foundation for Research Development
-# All rights reserved.

-# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

-# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

-# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
-# disclaimer in the documentation and/or other materials provided with the distribution.

-# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
-# derived from this software without specific prior written permission.

-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
-# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
-# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

%hr
.col-sm-12
- question.options.each do |option|
.form-group.option{ data: { option_id: option.id } }
.col-lg-1.text-center
= check_box_tag :response_example, '', false, class: 'checkbox-example form-control', disabled: true
.col-lg-10.no-padding
= text_field_tag "option-content", option.content, class: 'form-control option-content', placeholder: t(:surveyor)[:surveys][:form][:content][:option][:content][:placeholder], maxlength: 255
.col-lg-1.no-padding
= link_to surveyor_option_path(option), method: :delete, remote: true, class: 'btn btn-danger pull-right delete-option', title: t(:surveyor)[:surveys][:form][:content][:option][:delete], data: { toggle: 'tooltip', animation: 'false' } do
%span.glyphicon.glyphicon-trash
= render 'surveyor/surveys/form/form_partials/add_option_button', question: question
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-# Copyright © 2011-2016 MUSC Foundation for Research Development
-# All rights reserved.

-# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

-# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

-# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
-# disclaimer in the documentation and/or other materials provided with the distribution.

-# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
-# derived from this software without specific prior written permission.

-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
-# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
-# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.form-group
= country_select :response_example, '', { priority_countries: ['US'], include_blank: true }, { class: 'form-control selectpicker', disabled: true }
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.question-options.date-options{ class: display_question_example(question, 'date'), data: { question_id: question.id } }
.form-group
.input-group#datetimepicker
= text_field_tag :response_example, '', class: 'form-control', disabled: "disabled"
%span.input-group-addon
%span.glyphicon.glyphicon-calendar
.form-group
.input-group#datetimepicker
= text_field_tag :response_example, '', class: 'form-control', disabled: "disabled"
%span.input-group-addon
%span.glyphicon.glyphicon-calendar
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.question-options.dropdown-options{ class: display_question_example(question, 'dropdown'), data: { question_id: question.id } }
.form-group
= select_tag :response_example, [], class: 'form-control selectpicker', disabled: "disabled"
%hr
.col-sm-12
- question.options.each do |option|
.form-group.option{ data: {option_id: option.id } }
.col-lg-11.no-padding
= text_field_tag "option-content", option.content, class: 'form-control option-content', placeholder: t(:surveyor)[:surveys][:form][:content][:option][:content][:placeholder], maxlength: 255
.col-lg-1.no-padding
= link_to surveyor_option_path(option), method: :delete, remote: true, class: 'btn btn-danger pull-right delete-option', title: t(:surveyor)[:surveys][:form][:content][:option][:delete], data: { toggle: 'tooltip', animation: 'false' } do
%span.glyphicon.glyphicon-trash
= render 'surveyor/surveys/form/form_partials/add_option_button', question: question