diff --git a/app/models/organisation.rb b/app/models/organisation.rb index bd57838b8..e86be1e9f 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -13,15 +13,11 @@ class Organisation < ApplicationRecord has_many :child_organisation_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship" has_many :child_organisations, through: :child_organisation_relationships - has_many :managing_agents, - -> { joins(:parent_organisation_relationships).where({ parent_organisation_relationships: { relationship_type: OrganisationRelationship::MANAGING } }) }, - class_name: "Organisation", - through: :parent_organisation_relationships, - source: :child_organisation + has_many :managing_agent_relationships, -> { where(relationship_type: OrganisationRelationship::MANAGING) }, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship" + has_many :managing_agents, through: :managing_agent_relationships, source: :parent_organisation scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by, ->(param) { search_by_name(param) } - has_paper_trail auto_strip_attributes :name diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index 8b02f030a..fa40663ac 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -17,9 +17,4 @@ FactoryBot.define do created_at { Time.zone.now } updated_at { Time.zone.now } end - - factory :organisation_relationship do - child_organisation { FactoryBot.create(:organisation) } - parent_organisation { FactoryBot.create(:organisation) } - end end diff --git a/spec/factories/organisation_relationship.rb b/spec/factories/organisation_relationship.rb new file mode 100644 index 000000000..418599902 --- /dev/null +++ b/spec/factories/organisation_relationship.rb @@ -0,0 +1,14 @@ +FactoryBot.define do + factory :organisation_relationship do + child_organisation { FactoryBot.create(:organisation) } + parent_organisation { FactoryBot.create(:organisation) } + + trait :owning do + relationship_type { OrganisationRelationship::OWNING } + end + + trait :managing do + relationship_type { OrganisationRelationship::MANAGING } + end + end +end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index e2336bd0c..1d9e557ec 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -27,39 +27,67 @@ RSpec.describe Organisation, type: :model do .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}") end - context "with managing association" do - let(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } + context "with parent/child associations", :aggregate_failures do + let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } + let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") } before do - FactoryBot.create(:organisation_relationship, child_organisation:, parent_organisation: organisation, relationship_type: 0) - end + FactoryBot.create( + :organisation_relationship, + :managing, + child_organisation:, + parent_organisation: organisation, + ) - it "has correct child" do - expect(organisation.child_organisations.first).to eq(child_organisation) + FactoryBot.create( + :organisation_relationship, + :managing, + child_organisation: grandchild_organisation, + parent_organisation: child_organisation, + ) end - it "has correct parent" do - expect(child_organisation.parent_organisations.first).to eq(organisation) + it "has correct child_organisations" do + expect(organisation.child_organisations).to eq([child_organisation]) + expect(child_organisation.child_organisations).to eq([grandchild_organisation]) end - it "has correct managing agents" do - expect(child_organisation.managing_agents).to eq([organisation]) + it "has correct parent_organisations" do + expect(child_organisation.parent_organisations).to eq([organisation]) + expect(grandchild_organisation.parent_organisations).to eq([child_organisation]) end end - context "with owning association" do - let(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } + context "with managing association", :aggregate_failures do + let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") } + let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") } before do - FactoryBot.create(:organisation_relationship, child_organisation:, parent_organisation: organisation, relationship_type: 1) - end + FactoryBot.create( + :organisation_relationship, + :managing, + child_organisation:, + parent_organisation: organisation, + ) + + FactoryBot.create( + :organisation_relationship, + :owning, + child_organisation:, + parent_organisation: organisation, + ) - it "has correct child" do - expect(organisation.child_organisations.first).to eq(child_organisation) + FactoryBot.create( + :organisation_relationship, + :managing, + child_organisation: grandchild_organisation, + parent_organisation: child_organisation, + ) end - it "has correct parent" do - expect(child_organisation.parent_organisations.first).to eq(organisation) + it "has correct managing_agents" do + expect(child_organisation.managing_agents).to eq([organisation]) + expect(grandchild_organisation.managing_agents).to eq([child_organisation]) end end