Browse Source

Refactor organisation name changes to use integer for change_type and date for change_date

pull/3053/head
Manny Dinssa 3 weeks ago
parent
commit
010bdd25cf
  1. 2
      app/controllers/organisation_name_changes_controller.rb
  2. 13
      app/models/organisation_name_change.rb
  3. 4
      db/migrate/20250409113321_create_organisation_name_changes.rb
  4. 4
      db/schema.rb

2
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}."

13
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

4
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

4
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

Loading…
Cancel
Save