Browse Source

Rename change_date to startdate in organisation name changes and update related logic

pull/3053/head
Manny Dinssa 3 weeks ago
parent
commit
d3e9c1a7f0
  1. 5
      app/controllers/organisation_name_changes_controller.rb
  2. 8
      app/models/organisation.rb
  3. 54
      app/models/organisation_name_change.rb
  4. 2
      app/views/organisation_name_changes/new.html.erb
  5. 4
      db/migrate/20250409113321_create_organisation_name_changes.rb
  6. 4
      db/schema.rb

5
app/controllers/organisation_name_changes_controller.rb

@ -6,8 +6,9 @@ class OrganisationNameChangesController < ApplicationController
@organisation_name_change = @organisation.organisation_name_changes.new(organisation_name_change_params)
@organisation_name_change.change_type = :user_change
Rails.logger.debug "Params: #{params[:organisation_name_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}."
notice_message = @organisation_name_change.immediate_change ? "Name change saved successfully." : "Name change scheduled for #{@organisation_name_change.formatted_startdate}."
redirect_to organisation_path(@organisation), notice: notice_message
else
render :new, status: :unprocessable_entity
@ -30,6 +31,6 @@ private
end
def organisation_name_change_params
params.require(:organisation_name_change).permit(:name, :change_date, :immediate_change)
params.require(:organisation_name_change).permit(:name, :startdate, :immediate_change)
end
end

8
app/models/organisation.rb

@ -105,12 +105,12 @@ class Organisation < ApplicationRecord
end
def fetch_name_changes_with_dates
organisation_name_changes.visible.order(:change_date).map.with_index do |change, index|
next_change = organisation_name_changes.visible.order(:change_date)[index + 1]
organisation_name_changes.visible.order(:startdate).map.with_index do |change, index|
next_change = organisation_name_changes.visible.order(:startdate)[index + 1]
{
name: change.name,
start_date: change.change_date,
end_date: next_change&.change_date&.yesterday,
start_date: change.startdate,
end_date: next_change&.startdate&.yesterday,
}
end
end

54
app/models/organisation_name_change.rb

@ -4,14 +4,14 @@ class OrganisationNameChange < ApplicationRecord
scope :visible, -> { where(discarded_at: nil) }
validates :name, presence: true
validates :change_date, presence: true, unless: -> { immediate_change }
validate :change_date_must_be_after_last_change_date
validates :startdate, presence: true, unless: -> { immediate_change }
validate :startdate_must_be_after_last_change
validate :name_must_be_different_from_current
validate :change_date_must_be_unique_for_organisation
validate :startdate_must_be_unique_for_organisation
attr_accessor :immediate_change
before_validation :set_change_date_if_immediate
before_validation :set_startdate_if_immediate
CHANGE_TYPE = {
user_change: 1,
@ -23,66 +23,66 @@ class OrganisationNameChange < ApplicationRecord
has_paper_trail
def includes_date?(date)
change_date <= date && (next_change&.change_date.nil? || next_change&.change_date > date)
startdate <= date && (next_change&.startdate.nil? || next_change&.startdate > date)
end
def before_date?(date)
change_date < date
startdate < date
end
def after_date?(date)
change_date > date
startdate > date
end
def next_change
organisation.organisation_name_changes.where("change_date > ?", change_date).order(change_date: :asc).first
organisation.organisation_name_changes.where("startdate > ?", startdate).order(startdate: :asc).first
end
def previous_change
organisation.organisation_name_changes.where("change_date < ?", change_date).order(change_date: :desc).first
organisation.organisation_name_changes.where("startdate < ?", startdate).order(startdate: :desc).first
end
def active?(date = Time.zone.now)
includes_date?(date)
end
def formatted_change_date(format = :govuk_date)
change_date.to_formatted_s(format)
def formatted_startdate(format = :govuk_date)
startdate.to_formatted_s(format)
end
private
def set_change_date_if_immediate
self.change_date = Time.zone.now if immediate_change
def set_startdate_if_immediate
self.startdate = Time.zone.now if immediate_change
end
def change_date_must_be_after_last_change_date
return if change_date.blank?
def startdate_must_be_after_last_change
return if startdate.blank?
last_change_date = organisation.organisation_name_changes
last_startdate = organisation.organisation_name_changes
.visible
.where("change_date < ?", change_date)
.order(change_date: :desc)
.first&.change_date
.where("startdate < ?", startdate)
.order(startdate: :desc)
.first&.startdate
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}).")
if last_startdate && startdate <= last_startdate
errors.add(:startdate, "Start date must be after the last change date (#{last_startdate}).")
end
end
def change_date_must_be_unique_for_organisation
return if change_date.blank?
def startdate_must_be_unique_for_organisation
return if startdate.blank?
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 another name change.") unless immediate_change
if organisation.organisation_name_changes.visible.select(&:persisted?).any? { |record| record.startdate == startdate }
errors.add(:startdate, "Start date cannot be the same as another name change.") unless immediate_change
errors.add(:immediate_change, "Start date cannot be the same as another name change.") if immediate_change
end
end
def name_must_be_different_from_current
return if name.blank? || change_date.blank?
return if name.blank? || startdate.blank?
if name == organisation.name(date: change_date)
if name == organisation.name(date: startdate)
errors.add(:name, "New name must be different from the current name on the change date.")
end
end

2
app/views/organisation_name_changes/new.html.erb

@ -48,7 +48,7 @@
"data-info": { conditional_questions: { scheduled_date: [false] } }.to_json do %>
<%= render partial: "components/date_picker", locals: {
resource: @organisation_name_change,
question_id: :change_date,
question_id: :startdate,
legend: { text: "Set start date", size: "m" },
resource_type: "organisation_name_change",
hint: "For example, 13/9/2025",

4
db/migrate/20250409113321_create_organisation_name_changes.rb

@ -4,12 +4,12 @@ class CreateOrganisationNameChanges < ActiveRecord::Migration[7.2]
t.references :organisation, null: false, foreign_key: true
t.string :name, null: false
t.integer :change_type, null: false
t.date :change_date, null: false
t.date :startdate, null: false
t.datetime :discarded_at
t.timestamps
end
add_index :organisation_name_changes, %i[organisation_id change_date], unique: true, name: "index_org_name_changes_on_org_id_and_change_date"
add_index :organisation_name_changes, %i[organisation_id startdate], unique: true, name: "index_org_name_changes_on_org_id_and_startdate"
end
end

4
db/schema.rb

@ -501,11 +501,11 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_05_092900) do
t.bigint "organisation_id", null: false
t.string "name", null: false
t.integer "change_type", null: false
t.date "change_date", null: false
t.date "startdate", null: false
t.datetime "discarded_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["organisation_id", "change_date"], name: "index_org_name_changes_on_org_id_and_change_date", unique: true
t.index ["organisation_id", "startdate"], name: "index_org_name_changes_on_org_id_and_startdate", unique: true
t.index ["organisation_id"], name: "index_organisation_name_changes_on_organisation_id"
end

Loading…
Cancel
Save