Browse Source

add managing relationship

CLDC-1661-new-page-for-housing-providers
Jack S 2 years ago
parent
commit
baa052f802
  1. 8
      app/models/organisation.rb
  2. 5
      spec/factories/organisation.rb
  3. 14
      spec/factories/organisation_relationship.rb
  4. 64
      spec/models/organisation_spec.rb

8
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

5
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

14
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

64
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

Loading…
Cancel
Save