Browse Source

refactor

pull/2077/head
Kat 3 years ago
parent
commit
cde399a7c2
  1. 12
      app/models/form/sales/pages/owning_organisation.rb
  2. 2
      app/models/form/sales/subsections/setup.rb
  3. 4
      spec/models/form/sales/pages/owning_organisation_spec.rb
  4. 2
      spec/models/form/sales/subsections/setup_spec.rb
  5. 2
      spec/models/form_spec.rb
  6. 2
      spec/requests/form_controller_spec.rb

12
app/models/form/sales/pages/organisation.rb → 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) def initialize(id, hsh, subsection)
super super
@id = "organisation" @id = "owning_organisation"
end end
def questions def questions
@ -14,9 +14,7 @@ class Form::Sales::Pages::Organisation < ::Form::Page
return false unless current_user return false unless current_user
return true if current_user.support? return true if current_user.support?
return false unless FeatureToggle.sales_managing_organisation_enabled? return false unless FeatureToggle.sales_managing_organisation_enabled?
return true if has_multiple_stock_owners_with_own_stock?(current_user)
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
stock_owners = if FeatureToggle.merge_organisations_enabled? 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) 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 private
def stock_owners_with_own_stock_count(user) def has_multiple_stock_owners_with_own_stock?(user)
user.organisation.stock_owners.where(holds_own_stock: true).count 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
end end

2
app/models/form/sales/subsections/setup.rb

@ -7,7 +7,7 @@ class Form::Sales::Subsections::Setup < ::Form::Subsection
def pages def pages
@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::ManagingOrganisation.new(nil, nil, self),
Form::Sales::Pages::CreatedBy.new(nil, nil, self), Form::Sales::Pages::CreatedBy.new(nil, nil, self),
Form::Sales::Pages::SaleDate.new(nil, nil, self), Form::Sales::Pages::SaleDate.new(nil, nil, self),

4
spec/models/form/sales/pages/organisation_spec.rb → spec/models/form/sales/pages/owning_organisation_spec.rb

@ -1,6 +1,6 @@
require "rails_helper" 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) } subject(:page) { described_class.new(page_id, page_definition, subsection) }
let(:page_id) { nil } let(:page_id) { nil }
@ -17,7 +17,7 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do
end end
it "has the correct id" do it "has the correct id" do
expect(page.id).to eq("organisation") expect(page.id).to eq("owning_organisation")
end end
it "has the correct header" do it "has the correct header" do

2
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 it "has correct pages" do
expect(setup.pages.map(&:id)).to eq( expect(setup.pages.map(&:id)).to eq(
%w[ %w[
organisation owning_organisation
managing_organisation managing_organisation
created_by created_by
completion_date completion_date

2
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.count).to eq(1)
expect(form.subsections.first.id).to eq("setup") expect(form.subsections.first.id).to eq("setup")
expect(form.pages.count).to eq(13) 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.count).to eq(14)
expect(form.questions.first.id).to eq("owning_organisation_id") expect(form.questions.first.id).to eq("owning_organisation_id")
expect(form.start_date).to eq(Time.zone.parse("2022-04-01")) expect(form.start_date).to eq(Time.zone.parse("2022-04-01"))

2
spec/requests/form_controller_spec.rb

@ -181,7 +181,7 @@ RSpec.describe FormController, type: :request do
end end
it "correctly sets owning organisation" do 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") expect(response).to redirect_to("/sales-logs/#{sales_log.id}/created-by")
follow_redirect! follow_redirect!
sales_log.reload sales_log.reload

Loading…
Cancel
Save