Browse Source

CLDC-1433 Add parent child relationship model (#828)

* Update remote schema.rb with migrations, main features for this ticket still incoming

* Add join table for organisation self-referential parent/child relationships

* Add join table for organisation self-referential parent/child relationships

* Remove organisation_la factory and add test for organisation_relationship child/parent association

* Update spec/models/organisation_spec.rb

Co-authored-by: James Rose <james@jbpr.net>

Co-authored-by: James Rose <james@jbpr.net>
pull/834/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
4ae1bfb817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/organisation.rb
  2. 4
      app/models/organisation_relationship.rb
  3. 9
      db/migrate/20220810152340_add_parent_child_relationships.rb
  4. 9
      db/schema.rb
  5. 12
      spec/factories/organisation.rb
  6. 16
      spec/models/organisation_spec.rb

4
app/models/organisation.rb

@ -6,6 +6,10 @@ class Organisation < ApplicationRecord
has_many :organisation_rent_periods
has_many :owned_schemes, class_name: "Scheme", foreign_key: "owning_organisation_id", dependent: :delete_all
has_many :managed_schemes, class_name: "Scheme", foreign_key: "managing_organisation_id"
has_many :parent_organisation_relationships, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :parent_organisations, through: :parent_organisation_relationships
has_many :child_organisation_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :child_organisations, through: :child_organisation_relationships
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }

4
app/models/organisation_relationship.rb

@ -0,0 +1,4 @@
class OrganisationRelationship < ApplicationRecord
belongs_to :child_organisation, class_name: "Organisation"
belongs_to :parent_organisation, class_name: "Organisation"
end

9
db/migrate/20220810152340_add_parent_child_relationships.rb

@ -0,0 +1,9 @@
class AddParentChildRelationships < ActiveRecord::Migration[7.0]
def change
create_table :organisation_relationships do |t|
t.integer :child_organisation_id, foreign_key: true
t.integer :parent_organisation_id, foreign_key: true
t.timestamps
end
end
end

9
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_08_02_125711) do
ActiveRecord::Schema[7.0].define(version: 2022_08_10_152340) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -263,6 +263,13 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_02_125711) do
t.boolean "empty_export", default: false, null: false
end
create_table "organisation_relationships", force: :cascade do |t|
t.integer "child_organisation_id"
t.integer "parent_organisation_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "organisation_rent_periods", force: :cascade do |t|
t.bigint "organisation_id"
t.integer "rent_period"

12
spec/factories/organisation.rb

@ -11,17 +11,15 @@ FactoryBot.define do
holds_own_stock { true }
end
factory :organisation_la do
organisation
ons_code { "E07000041" }
created_at { Time.zone.now }
updated_at { Time.zone.now }
end
factory :organisation_rent_period do
organisation
rent_period { 2 }
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

16
spec/models/organisation_spec.rb

@ -27,6 +27,22 @@ 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 parent/child association" do
let(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") }
before do
FactoryBot.create(:organisation_relationship, child_organisation:, parent_organisation: organisation)
end
it "has correct child" do
expect(organisation.child_organisations.first).to eq(child_organisation)
end
it "has correct parent" do
expect(child_organisation.parent_organisations.first).to eq(organisation)
end
end
context "with data protection confirmations" do
before do
FactoryBot.create(:data_protection_confirmation, organisation:, confirmed: false, created_at: Time.utc(2018, 0o6, 0o5, 10, 36, 49))

Loading…
Cancel
Save