diff --git a/app/models/concerns/rdv/address_concern.rb b/app/models/concerns/rdv/address_concern.rb index 2a39d9faa7..d1965e820c 100644 --- a/app/models/concerns/rdv/address_concern.rb +++ b/app/models/concerns/rdv/address_concern.rb @@ -53,4 +53,9 @@ def address_without_personal_information Motif.human_attribute_value(:location_type, :phone) end end + + def user_for_home_rdv + proches, responsables = users.partition(&:responsible_id) + (responsables + proches).select(&:address).first || users.first + end end diff --git a/app/models/rdv.rb b/app/models/rdv.rb index f22203dea5..1e52d59338 100644 --- a/app/models/rdv.rb +++ b/app/models/rdv.rb @@ -211,11 +211,6 @@ def creneaux_available(date_range) CreneauxSearch::Calculator.available_slots(motif, lieu, date_range) end - def user_for_home_rdv - responsibles = users.loaded? ? users.select(&:responsible_id) : users.where.not(responsible_id: [nil]) - [responsibles, users].flatten.select(&:address).first || users.first - end - # Ces plages d'ouvertures sont utilisé pour afficher des infos # s'il y a un chevauchement avec le RDV. # diff --git a/spec/models/rdv_spec.rb b/spec/models/rdv_spec.rb index 68839b60b2..d9b7e050c9 100644 --- a/spec/models/rdv_spec.rb +++ b/spec/models/rdv_spec.rb @@ -186,17 +186,17 @@ end context "when rdv is at home" do - let(:responsible) { create(:user) } - let(:child) { create(:user, responsible: responsible) } - let(:rdv) { create(:rdv, :at_home, users: [child]) } + let(:responsable) { create(:user, address: "1 rue du Responsable, 74000 Annecy") } + let(:proche) { create(:user, responsible: responsable, address: "33 impasse du Proche, 42300 Roanne") } + let(:rdv) { create(:rdv, :at_home, users: [responsable, proche]) } - it { is_expected.to eq responsible.address } + it { is_expected.to eq responsable.address } end context "when rdv is by phone" do - let(:responsible) { create(:user) } - let(:child) { create(:user, responsible: responsible) } - let(:rdv) { create(:rdv, :by_phone, users: [child]) } + let(:responsable) { create(:user) } + let(:proche) { create(:user, responsible: responsable) } + let(:rdv) { create(:rdv, :by_phone, users: [responsable, proche]) } it { is_expected.to be_blank } end