From 6e1f71fd0b8b334e83158001289646111a68314e Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 27 Nov 2023 09:20:56 +0000 Subject: [PATCH] refactor --- .../{organisation.rb => owning_organisation.rb} | 12 +++++------- app/models/form/sales/subsections/setup.rb | 2 +- ...anisation_spec.rb => owning_organisation_spec.rb} | 4 ++-- spec/models/form/sales/subsections/setup_spec.rb | 2 +- spec/models/form_spec.rb | 2 +- spec/requests/form_controller_spec.rb | 2 +- 6 files changed, 11 insertions(+), 13 deletions(-) rename app/models/form/sales/pages/{organisation.rb => owning_organisation.rb} (79%) rename spec/models/form/sales/pages/{organisation_spec.rb => owning_organisation_spec.rb} (97%) diff --git a/app/models/form/sales/pages/organisation.rb b/app/models/form/sales/pages/owning_organisation.rb similarity index 79% rename from app/models/form/sales/pages/organisation.rb rename to app/models/form/sales/pages/owning_organisation.rb index ef6669391..d8787a456 100644 --- a/app/models/form/sales/pages/organisation.rb +++ b/app/models/form/sales/pages/owning_organisation.rb @@ -1,7 +1,7 @@ -class Form::Sales::Pages::Organisation < ::Form::Page +class Form::Sales::Pages::OwningOrganisation < ::Form::Page def initialize(id, hsh, subsection) super - @id = "organisation" + @id = "owning_organisation" end def questions @@ -14,9 +14,7 @@ class Form::Sales::Pages::Organisation < ::Form::Page return false unless current_user return true if current_user.support? return false unless FeatureToggle.sales_managing_organisation_enabled? - - return true if stock_owners_with_own_stock_count(current_user) > 1 - return true if current_user.organisation.holds_own_stock? && stock_owners_with_own_stock_count(current_user) >= 1 + return true if has_multiple_stock_owners_with_own_stock?(current_user) stock_owners = if FeatureToggle.merge_organisations_enabled? current_user.organisation.stock_owners.where(holds_own_stock: true) + current_user.organisation.absorbed_organisations.where(holds_own_stock: true) @@ -43,7 +41,7 @@ class Form::Sales::Pages::Organisation < ::Form::Page private - def stock_owners_with_own_stock_count(user) - user.organisation.stock_owners.where(holds_own_stock: true).count + def has_multiple_stock_owners_with_own_stock?(user) + user.organisation.stock_owners.where(holds_own_stock: true).count > 1 || user.organisation.holds_own_stock? && user.organisation.stock_owners.where(holds_own_stock: true).count >= 1 end end diff --git a/app/models/form/sales/subsections/setup.rb b/app/models/form/sales/subsections/setup.rb index 67c160e52..26a47c700 100644 --- a/app/models/form/sales/subsections/setup.rb +++ b/app/models/form/sales/subsections/setup.rb @@ -7,7 +7,7 @@ class Form::Sales::Subsections::Setup < ::Form::Subsection def pages @pages ||= [ - Form::Sales::Pages::Organisation.new(nil, nil, self), + Form::Sales::Pages::OwningOrganisation.new(nil, nil, self), Form::Sales::Pages::ManagingOrganisation.new(nil, nil, self), Form::Sales::Pages::CreatedBy.new(nil, nil, self), Form::Sales::Pages::SaleDate.new(nil, nil, self), diff --git a/spec/models/form/sales/pages/organisation_spec.rb b/spec/models/form/sales/pages/owning_organisation_spec.rb similarity index 97% rename from spec/models/form/sales/pages/organisation_spec.rb rename to spec/models/form/sales/pages/owning_organisation_spec.rb index 639c8a4ff..4a2ca0408 100644 --- a/spec/models/form/sales/pages/organisation_spec.rb +++ b/spec/models/form/sales/pages/owning_organisation_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe Form::Sales::Pages::Organisation, type: :model do +RSpec.describe Form::Sales::Pages::OwningOrganisation, type: :model do subject(:page) { described_class.new(page_id, page_definition, subsection) } let(:page_id) { nil } @@ -17,7 +17,7 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do end it "has the correct id" do - expect(page.id).to eq("organisation") + expect(page.id).to eq("owning_organisation") end it "has the correct header" do diff --git a/spec/models/form/sales/subsections/setup_spec.rb b/spec/models/form/sales/subsections/setup_spec.rb index 71bff2e32..cdc3aff8d 100644 --- a/spec/models/form/sales/subsections/setup_spec.rb +++ b/spec/models/form/sales/subsections/setup_spec.rb @@ -15,7 +15,7 @@ RSpec.describe Form::Sales::Subsections::Setup, type: :model do it "has correct pages" do expect(setup.pages.map(&:id)).to eq( %w[ - organisation + owning_organisation managing_organisation created_by completion_date diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index cf0bb9885..838b636dd 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -388,7 +388,7 @@ RSpec.describe Form, type: :model do expect(form.subsections.count).to eq(1) expect(form.subsections.first.id).to eq("setup") expect(form.pages.count).to eq(13) - expect(form.pages.first.id).to eq("organisation") + expect(form.pages.first.id).to eq("owning_organisation") expect(form.questions.count).to eq(14) expect(form.questions.first.id).to eq("owning_organisation_id") expect(form.start_date).to eq(Time.zone.parse("2022-04-01")) diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index fbe3dbb1d..10dab7fe4 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -181,7 +181,7 @@ RSpec.describe FormController, type: :request do end it "correctly sets owning organisation" do - post "/sales-logs/#{sales_log.id}/organisation", params: params + post "/sales-logs/#{sales_log.id}/owning-organisation", params: params expect(response).to redirect_to("/sales-logs/#{sales_log.id}/created-by") follow_redirect! sales_log.reload