Browse Source

Restrict shown answer options based on already answered questions

pull/674/head
baarkerlounger 3 years ago
parent
commit
c33eed77c0
  1. 7
      app/models/form/setup/questions/created_by_id.rb
  2. 7
      app/models/form/setup/questions/owning_organisation_id.rb
  3. 14
      spec/models/form/setup/questions/created_by_id_spec.rb
  4. 15
      spec/models/form/setup/questions/owning_organisation_id_spec.rb

7
app/models/form/setup/questions/created_by_id.rb

@ -18,6 +18,13 @@ class Form::Setup::Questions::CreatedById < ::Form::Question
end
end
def displayed_answer_options(case_log)
return answer_options unless case_log.owning_organisation
user_ids = case_log.owning_organisation.users.pluck(:id) + [""]
answer_options.select { |k, _v| user_ids.include?(k) }
end
def label_from_value(value)
return unless value

7
app/models/form/setup/questions/owning_organisation_id.rb

@ -18,6 +18,13 @@ class Form::Setup::Questions::OwningOrganisationId < ::Form::Question
end
end
def displayed_answer_options(case_log)
return answer_options unless case_log.created_by
ids = ["", case_log.created_by.organisation.id]
answer_options.select { |k, _v| ids.include?(k) }
end
def label_from_value(value)
return unless value

14
spec/models/form/setup/questions/created_by_id_spec.rb

@ -73,4 +73,18 @@ RSpec.describe Form::Setup::Questions::CreatedById, type: :model do
expect(question.hidden_in_check_answers).to be true
end
end
context "when the owning organisation is already set" do
let(:case_log) { FactoryBot.create(:case_log, owning_organisation: user_2.organisation) }
let(:expected_answer_options) do
{
"" => "Select an option",
user_2.id => user_2.name,
}
end
it "only displays users that belong to that organisation" do
expect(question.displayed_answer_options(case_log)).to eq(expected_answer_options)
end
end
end

15
spec/models/form/setup/questions/owning_organisation_id_spec.rb

@ -73,4 +73,19 @@ RSpec.describe Form::Setup::Questions::OwningOrganisationId, type: :model do
expect(question.hidden_in_check_answers).to be true
end
end
context "when the user is already set" do
let(:user) { FactoryBot.create(:user, organisation: organisation_2) }
let(:case_log) { FactoryBot.create(:case_log, created_by: user) }
let(:expected_answer_options) do
{
"" => "Select an option",
organisation_2.id => organisation_2.name,
}
end
it "only displays users that belong to that organisation" do
expect(question.displayed_answer_options(case_log)).to eq(expected_answer_options)
end
end
end

Loading…
Cancel
Save