Browse Source

Refactor organisation name change logic to ensure visibility and uniqueness of change dates

pull/3053/head
Manny Dinssa 3 weeks ago
parent
commit
e67c2dfaf2
  1. 4
      app/controllers/organisation_name_changes_controller.rb
  2. 6
      app/models/organisation.rb
  3. 18
      app/models/organisation_name_change.rb

4
app/controllers/organisation_name_changes_controller.rb

@ -10,13 +10,13 @@ class OrganisationNameChangesController < ApplicationController
notice_message = @organisation_name_change.immediate_change ? "Name change saved successfully." : "Name change scheduled for #{@organisation_name_change.formatted_change_date}."
redirect_to organisation_path(@organisation), notice: notice_message
else
render template: "organisations/change_name", status: :unprocessable_entity
render :new, status: :unprocessable_entity
end
end
def change_name
@organisation_name_change = OrganisationNameChange.new
render "organisations/change_name", layout: "application"
render :new, layout: "application"
end
private

6
app/models/organisation.rb

@ -73,7 +73,7 @@ class Organisation < ApplicationRecord
end
def name(date: Time.zone.now)
name_change = organisation_name_changes.find { |change| change.includes_date?(date) }
name_change = organisation_name_changes.visible.find { |change| change.includes_date?(date) }
name_change&.name || read_attribute(:name)
end
@ -105,8 +105,8 @@ class Organisation < ApplicationRecord
end
def fetch_name_changes_with_dates
organisation_name_changes.order(:change_date).map.with_index do |change, index|
next_change = organisation_name_changes.order(:change_date)[index + 1]
organisation_name_changes.visible.order(:change_date).map.with_index do |change, index|
next_change = organisation_name_changes.visible.order(:change_date)[index + 1]
{
name: change.name,
start_date: change.change_date,

18
app/models/organisation_name_change.rb

@ -7,6 +7,7 @@ class OrganisationNameChange < ApplicationRecord
validates :change_date, presence: true, unless: -> { immediate_change }
validate :change_date_must_be_after_last_change_date
validate :name_must_be_different_from_current
validate :change_date_must_be_unique_for_organisation
attr_accessor :immediate_change
@ -45,18 +46,31 @@ class OrganisationNameChange < ApplicationRecord
private
def set_change_date_if_immediate
self.change_date = Time.zone.now if immediate_change
self.change_date = Time.zone.now if immediate_change == true
end
def change_date_must_be_after_last_change_date
return if change_date.blank?
last_change_date = organisation.organisation_name_changes.where("change_date < ?", change_date).order(change_date: :desc).first&.change_date
last_change_date = organisation.organisation_name_changes
.visible
.where("change_date < ?", change_date)
.order(change_date: :desc)
.first&.change_date
if last_change_date && change_date <= last_change_date
errors.add(:change_date, "Start date must be after the last change date (#{last_change_date}).")
end
end
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.")
end
end
def name_must_be_different_from_current
return if name.blank? || change_date.blank?

Loading…
Cancel
Save