From 09eb55b0ca382105baf421020c1f643531d4f45a Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 20 Apr 2022 18:06:04 +0100 Subject: [PATCH] Create org la relation --- app/models/organisation.rb | 1 + app/models/organisation_la.rb | 2 ++ db/migrate/20220420165451_create_organisation_la.rb | 10 ++++++++++ db/schema.rb | 10 +++++++++- spec/factories/organisation.rb | 7 +++++++ spec/models/organisation_spec.rb | 10 ++++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/models/organisation_la.rb create mode 100644 db/migrate/20220420165451_create_organisation_la.rb diff --git a/app/models/organisation.rb b/app/models/organisation.rb index e9dd7eafe..bd8b77fdc 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -3,6 +3,7 @@ class Organisation < ApplicationRecord has_many :owned_case_logs, class_name: "CaseLog", foreign_key: "owning_organisation_id" has_many :managed_case_logs, class_name: "CaseLog", foreign_key: "managing_organisation_id" has_many :data_protection_confirmations + has_many :organisation_las has_paper_trail diff --git a/app/models/organisation_la.rb b/app/models/organisation_la.rb new file mode 100644 index 000000000..18ab26d26 --- /dev/null +++ b/app/models/organisation_la.rb @@ -0,0 +1,2 @@ +class OrganisationLa < ApplicationRecord +end diff --git a/db/migrate/20220420165451_create_organisation_la.rb b/db/migrate/20220420165451_create_organisation_la.rb new file mode 100644 index 000000000..52cacdfb2 --- /dev/null +++ b/db/migrate/20220420165451_create_organisation_la.rb @@ -0,0 +1,10 @@ +class CreateOrganisationLa < ActiveRecord::Migration[7.0] + def change + create_table :organisation_las do |t| + t.belongs_to :organisation + t.column :ons_code, :string + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c06c0e7e4..451f16749 100644 --- a/db/schema.rb +++ b/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_04_11_092231) do +ActiveRecord::Schema[7.0].define(version: 202202071123100) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -256,6 +256,14 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_11_092231) do t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } end + create_table "organisation_las", force: :cascade do |t| + t.bigint "organisation_id" + t.string "ons_code" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["organisation_id"], name: "index_organisation_las_on_organisation_id" + end + create_table "organisations", force: :cascade do |t| t.string "name" t.string "phone" diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index 593bba4ae..2bb293968 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -8,4 +8,11 @@ FactoryBot.define do created_at { Time.zone.now } updated_at { Time.zone.now } end + + factory :organisation_la do + organisation + ons_code { "E07000041" } + created_at { Time.zone.now } + updated_at { Time.zone.now } + end end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 59e0f05d8..d3c09368f 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -29,6 +29,16 @@ RSpec.describe Organisation, type: :model do end end + context "when the organisation only operates in specific local authorities" do + before do + FactoryBot.create(:organisation_la, organisation_id: organisation.id) + end + + it "has local authorities associated" do + expect(organisation.organisation_las.pluck(:ons_code)).to eq(["E07000041"]) + end + end + context "with case logs" do let(:other_organisation) { FactoryBot.create(:organisation) } let!(:owned_case_log) do