Skip to content

Commit

Permalink
Fix survey conditional display
Browse files Browse the repository at this point in the history
* fix conditionnal questions

* WIP

* add before validation
  • Loading branch information
eliegaboriau authored and alecslupu committed Feb 28, 2023
1 parent 6784352 commit 0d58599
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
17 changes: 16 additions & 1 deletion decidim-forms/app/forms/decidim/forms/answer_form.rb
Expand Up @@ -59,7 +59,22 @@ def custom_choices
end

def display_conditions_fulfilled?
question.display_conditions.all? do |condition|
return optional_conditions_fulfilled? unless question.display_conditions.where(mandatory: true).any?

mandatory_conditions_fulfilled?
end

def mandatory_conditions_fulfilled?
question.display_conditions.where(mandatory: true).all? do |condition|
answer = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id }
condition.fulfilled?(answer)
end
end

def optional_conditions_fulfilled?
return true unless question.display_conditions.where(mandatory: false).any?

question.display_conditions.where(mandatory: false).any? do |condition|
answer = context.responses&.find { |r| r.question_id&.to_i == condition.condition_question.id }
condition.fulfilled?(answer)
end
Expand Down
4 changes: 4 additions & 0 deletions decidim-forms/app/forms/decidim/forms/questionnaire_form.rb
Expand Up @@ -4,6 +4,8 @@ module Decidim
module Forms
# This class holds a Form to answer a questionnaire from Decidim's public page.
class QuestionnaireForm < Decidim::Form
include ActiveModel::Validations::Callbacks

# as questionnaire uses "answers" for the database relationships is
# important not to use the same word here to avoid querying all the entries, resulting in a high performance penalty
attribute :responses, Array[AnswerForm]
Expand All @@ -12,6 +14,8 @@ class QuestionnaireForm < Decidim::Form

attribute :tos_agreement, Boolean

before_validation :before_validation

validates :tos_agreement, allow_nil: false, acceptance: true
validate :session_token_in_context

Expand Down
Expand Up @@ -193,6 +193,60 @@ def tokenize(id)
end
end
end

context "when display_conditions are not mandatory on the same question but are fulfilled" do
let(:questionnaire_conditionned) { create(:questionnaire, questionnaire_for: participatory_process) }
let!(:option1) { create :answer_option, question: condition_question }
let!(:option2) { create :answer_option, question: condition_question }
let!(:option3) { create :answer_option, question: condition_question }
let!(:condition_question) do
create(
:questionnaire_question,
questionnaire: questionnaire_conditionned,
mandatory: false,
question_type: "single_option"
)
end
let!(:question) { create(:questionnaire_question, questionnaire: questionnaire_conditionned, question_type: "short_answer") }
let!(:display_condition) { create(:display_condition, question: question, condition_question: condition_question, condition_type: :equal, answer_option: option1, mandatory: false) }
let!(:display_condition2) { create(:display_condition, question: question, condition_question: condition_question, condition_type: :equal, answer_option: option3, mandatory: false) }
let(:form_params) do
{
"responses" => [
{
"choices" => [
{ "body" => option1.body, "answer_option_id" => option1.id }
],
"question_id" => condition_question.id
},
{
"body" => "answer_test",
"question_id" => question.id
}
],
"tos_agreement" => "1"
}
end
let(:command) { described_class.new(form, current_user, questionnaire_conditionned) }

it "broadcasts ok" do
expect { command.call }.to broadcast(:ok)
end

it "creates a questionnaire answer for each question answered" do
expect do
command.call
end.to change(Answer, :count).by(2)
expect(Answer.all.map(&:questionnaire)).to eq([questionnaire_conditionned, questionnaire_conditionned])
end

it "creates answers with the correct information" do
command.call

expect(Answer.first.choices.first.answer_option).to eq(option1)
expect(Answer.second.body).to eq("answer_test")
end
end
end

describe "when the user is unregistered" do
Expand Down

0 comments on commit 0d58599

Please sign in to comment.