From d6ebaa3278a4cd2aee28487493f4432b7accf5af Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:41:54 +0000 Subject: [PATCH] Check org name uniqueness --- app/controllers/organisations_controller.rb | 1 + app/models/organisation.rb | 1 + config/locales/en.yml | 1 + db/migrate/20241125153349_add_unique_index_to_org_name.rb | 5 +++++ db/schema.rb | 1 + spec/models/organisation_spec.rb | 6 ++++++ 6 files changed, 15 insertions(+) create mode 100644 db/migrate/20241125153349_add_unique_index_to_org_name.rb diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 8ffe426d7..93b667a99 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -155,6 +155,7 @@ class OrganisationsController < ApplicationController end redirect_to details_organisation_path(@organisation) else + @used_rent_periods = @organisation.lettings_logs.pluck(:period).uniq.compact.map(&:to_s) @rent_periods = helpers.rent_periods_with_checked_attr(checked_periods: selected_rent_periods) render :edit, status: :unprocessable_entity end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 23f91f1ad..69c80d198 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -58,6 +58,7 @@ class Organisation < ApplicationRecord alias_method :la?, :LA? validates :name, presence: { message: I18n.t("validations.organisation.name_missing") } + validates :name, uniqueness: { case_sensitive: false, message: I18n.t("validations.organisation.name_not_unique") } validates :provider_type, presence: { message: I18n.t("validations.organisation.provider_type_missing") } def self.find_by_id_on_multiple_fields(id) diff --git a/config/locales/en.yml b/config/locales/en.yml index 851a9ea2c..499ffd144 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -235,6 +235,7 @@ en: organisation: data_sharing_agreement_not_signed: "Your organisation must accept the Data Sharing Agreement before you can create any logs." name_missing: "Enter the name of the organisation." + name_not_unique: "An organisation with this name already exists. Enter a name that hasn’t been used." provider_type_missing: "Select the organisation type." stock_owner: blank: "You must choose a stock owner." diff --git a/db/migrate/20241125153349_add_unique_index_to_org_name.rb b/db/migrate/20241125153349_add_unique_index_to_org_name.rb new file mode 100644 index 000000000..a7a124183 --- /dev/null +++ b/db/migrate/20241125153349_add_unique_index_to_org_name.rb @@ -0,0 +1,5 @@ +class AddUniqueIndexToOrgName < ActiveRecord::Migration[7.0] + def change + add_index :organisations, :name, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index d31a54da2..1a1c127c6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -546,6 +546,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_12_04_100518) do t.datetime "discarded_at" t.datetime "schemes_deduplicated_at" t.index ["absorbing_organisation_id"], name: "index_organisations_on_absorbing_organisation_id" + t.index ["name"], name: "index_organisations_on_name", unique: true t.index ["old_visible_id"], name: "index_organisations_on_old_visible_id", unique: true end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 9b01845ae..b117feef7 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -19,6 +19,12 @@ RSpec.describe Organisation, type: :model do .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}") end + it "validates uniqueness of name" do + org = build(:organisation, name: organisation.name.downcase) + org.valid? + expect(org.errors[:name]).to include(I18n.t("validations.organisation.name_not_unique")) + end + context "with parent/child associations", :aggregate_failures do let!(:child_organisation) { create(:organisation, name: "MHCLG Child") } let!(:grandchild_organisation) { create(:organisation, name: "MHCLG Grandchild") }