From 010bdd25cf6b070e78d1113c7a92b606a1a17acc Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Tue, 15 Apr 2025 17:49:14 +0100 Subject: [PATCH] Refactor organisation name changes to use integer for change_type and date for change_date --- .../organisation_name_changes_controller.rb | 2 +- app/models/organisation_name_change.rb | 13 ++++++++++--- ...250409113321_create_organisation_name_changes.rb | 4 ++-- db/schema.rb | 4 ++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/organisation_name_changes_controller.rb b/app/controllers/organisation_name_changes_controller.rb index b90bc3f6e..0ff2dc11f 100644 --- a/app/controllers/organisation_name_changes_controller.rb +++ b/app/controllers/organisation_name_changes_controller.rb @@ -4,7 +4,7 @@ class OrganisationNameChangesController < ApplicationController def create @organisation_name_change = @organisation.organisation_name_changes.new(organisation_name_change_params) - @organisation_name_change.change_type = "User change" + @organisation_name_change.change_type = :user_change if @organisation_name_change.save notice_message = @organisation_name_change.immediate_change ? "Name change saved successfully." : "Name change scheduled for #{@organisation_name_change.formatted_change_date}." diff --git a/app/models/organisation_name_change.rb b/app/models/organisation_name_change.rb index 328ca2616..ba9702b76 100644 --- a/app/models/organisation_name_change.rb +++ b/app/models/organisation_name_change.rb @@ -13,6 +13,13 @@ class OrganisationNameChange < ApplicationRecord before_validation :set_change_date_if_immediate + CHANGE_TYPE = { + user_change: 1, + merge: 2, + }.freeze + + enum :change_type, CHANGE_TYPE, prefix: true + has_paper_trail def includes_date?(date) @@ -46,7 +53,7 @@ class OrganisationNameChange < ApplicationRecord private def set_change_date_if_immediate - self.change_date = Time.zone.now if immediate_change == true + self.change_date = Time.zone.now if immediate_change end def change_date_must_be_after_last_change_date @@ -66,8 +73,8 @@ private def change_date_must_be_unique_for_organisation return if change_date.blank? - if organisation.organisation_name_changes.visible.select(&:persisted?).any? { |record| record.change_date.to_date == change_date.to_date } - errors.add(:change_date, "Start date cannot be the same as a previous change date for this organisation.") + if organisation.organisation_name_changes.visible.select(&:persisted?).any? { |record| record.change_date == change_date } + errors.add(:change_date, "Start date cannot be the same as an existing name change.") end end diff --git a/db/migrate/20250409113321_create_organisation_name_changes.rb b/db/migrate/20250409113321_create_organisation_name_changes.rb index 57cc91965..33eb495ca 100644 --- a/db/migrate/20250409113321_create_organisation_name_changes.rb +++ b/db/migrate/20250409113321_create_organisation_name_changes.rb @@ -3,8 +3,8 @@ class CreateOrganisationNameChanges < ActiveRecord::Migration[7.2] create_table :organisation_name_changes do |t| t.references :organisation, null: false, foreign_key: true t.string :name, null: false - t.string :change_type - t.datetime :change_date, null: false + t.integer :change_type, null: false + t.date :change_date, null: false t.datetime :discarded_at t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 705d02bdf..eaf4d25cc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -500,8 +500,8 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_05_092900) do create_table "organisation_name_changes", force: :cascade do |t| t.bigint "organisation_id", null: false t.string "name", null: false - t.string "change_type" - t.datetime "change_date", null: false + t.integer "change_type", null: false + t.date "change_date", null: false t.datetime "discarded_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false