From 7bd1fb0eff117c62e44ce68ce918c972999958f5 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 10 Aug 2023 08:47:54 +0100 Subject: [PATCH] Update routed to method --- app/models/form/sales/pages/organisation.rb | 17 +++------- .../form/sales/pages/organisation_spec.rb | 32 +++++++++---------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/app/models/form/sales/pages/organisation.rb b/app/models/form/sales/pages/organisation.rb index 8f6728821..405cf51c1 100644 --- a/app/models/form/sales/pages/organisation.rb +++ b/app/models/form/sales/pages/organisation.rb @@ -10,24 +10,17 @@ class Form::Sales::Pages::Organisation < ::Form::Page ] end - def routed_to?(log, current_user) + def routed_to?(_log, current_user) return false unless current_user return true if current_user.support? - stock_owners = current_user.organisation.stock_owners + current_user.organisation.absorbed_organisations.where(holds_own_stock: true) + absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true) if current_user.organisation.holds_own_stock? - if current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?) - return true - end - return true if stock_owners.count >= 1 - - log.update!(owning_organisation: current_user.organisation) + return true if absorbed_stock_owners.count >= 1 else - return false if stock_owners.count.zero? - return true if stock_owners.count > 1 - - log.update!(owning_organisation: stock_owners.first) + return false if absorbed_stock_owners.count.zero? + return true if absorbed_stock_owners.count > 1 end false diff --git a/spec/models/form/sales/pages/organisation_spec.rb b/spec/models/form/sales/pages/organisation_spec.rb index 710edbecc..8f160ca53 100644 --- a/spec/models/form/sales/pages/organisation_spec.rb +++ b/spec/models/form/sales/pages/organisation_spec.rb @@ -88,8 +88,8 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do expect(page.routed_to?(log, user)).to eq(false) end - it "updates owning_organisation_id" do - expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(stock_owner) + it "does not update owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation) end end @@ -111,11 +111,7 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do end it "is not shown" do - expect(page.routed_to?(log, user)).to eq(true) - end - - it "updates owning_organisation_id" do - expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation) + expect(page.routed_to?(log, user)).to eq(false) end end end @@ -129,12 +125,6 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do it "is not shown" do expect(page.routed_to?(log, user)).to eq(false) end - - it "updates owning_organisation_id to user organisation" do - expect { - page.routed_to?(log, user) - }.to change(log.reload, :owning_organisation).from(nil).to(user.organisation) - end end context "with >0 stock_owners" do @@ -143,12 +133,20 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do create(:organisation_relationship, child_organisation: user.organisation) end - it "is shown" do - expect(page.routed_to?(log, user)).to eq(true) + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) end + end - it "does not update owning_organisation_id" do - expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil) + context "with merged organisations" do + let(:merged_org) { create(:organisation) } + + before do + merged_org.update!(absorbing_organisation: user.organisation, merge_date: Time.zone.now) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) end end end