From 5b33b51e5a977565a2d37486ac113b034abfeafe Mon Sep 17 00:00:00 2001 From: Jack S Date: Fri, 18 Nov 2022 15:28:26 +0000 Subject: [PATCH] Show also users from owning_organisation --- .../form/lettings/questions/created_by_id.rb | 7 +++++-- .../lettings/questions/created_by_id_spec.rb | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/models/form/lettings/questions/created_by_id.rb b/app/models/form/lettings/questions/created_by_id.rb index cf7323940..a120e72a8 100644 --- a/app/models/form/lettings/questions/created_by_id.rb +++ b/app/models/form/lettings/questions/created_by_id.rb @@ -20,9 +20,12 @@ class Form::Lettings::Questions::CreatedById < ::Form::Question end def displayed_answer_options(log, _user = nil) - return answer_options unless log.owning_organisation + return answer_options unless log.owning_organisation && log.managing_organisation + + user_ids = [""] + user_ids += log.owning_organisation.users.pluck(:id) + user_ids += log.managing_organisation.users.pluck(:id) - user_ids = log.owning_organisation.users.pluck(:id) + [""] answer_options.select { |k, _v| user_ids.include?(k) } end diff --git a/spec/models/form/lettings/questions/created_by_id_spec.rb b/spec/models/form/lettings/questions/created_by_id_spec.rb index 152a24265..04fac20fb 100644 --- a/spec/models/form/lettings/questions/created_by_id_spec.rb +++ b/spec/models/form/lettings/questions/created_by_id_spec.rb @@ -8,8 +8,9 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do let(:page) { instance_double(Form::Page) } let(:subsection) { instance_double(Form::Subsection) } let(:form) { instance_double(Form) } - let(:user_1) { FactoryBot.create(:user, name: "first user") } - let(:user_2) { FactoryBot.create(:user, name: "second user") } + let(:user_1) { create(:user, name: "first user") } + let(:user_2) { create(:user, name: "second user") } + let(:user_3) { create(:user, name: "third user") } let!(:expected_answer_options) do { "" => "Select an option", @@ -51,7 +52,7 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do end context "when the current user is support" do - let(:support_user) { FactoryBot.build(:user, :support) } + let(:support_user) { build(:user, :support) } it "is shown in check answers" do expect(question.hidden_in_check_answers?(nil, support_user)).to be false @@ -59,7 +60,7 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do end context "when the current user is not support" do - let(:user) { FactoryBot.build(:user) } + let(:user) { build(:user) } it "is not shown in check answers" do expect(question.hidden_in_check_answers?(nil, user)).to be true @@ -67,11 +68,18 @@ RSpec.describe Form::Lettings::Questions::CreatedById, type: :model do end context "when the owning organisation is already set" do - let(:lettings_log) { FactoryBot.create(:lettings_log, owning_organisation: user_2.organisation) } + let(:lettings_log) do + create( + :lettings_log, + owning_organisation: user_2.organisation, + managing_organisation: user_3.organisation, + ) + end let(:expected_answer_options) do { "" => "Select an option", user_2.id => "#{user_2.name} (#{user_2.email})", + user_3.id => "#{user_3.name} (#{user_3.email})", } end