From c636d47363b9e4f077e4ae6648480942d317c82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 9 Jul 2026 12:33:39 +0200 Subject: [PATCH] disable type field on pdu if there is connectoins and add validatoin --- app/models/power_distribution_unit.rb | 13 +++++++++++++ app/views/power_distribution_units/_form.html.erb | 3 ++- config/locales/activerecord.en.yml | 5 +++++ config/locales/activerecord.fr.yml | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/models/power_distribution_unit.rb b/app/models/power_distribution_unit.rb index 47d7380d9..cb2a8f234 100644 --- a/app/models/power_distribution_unit.rb +++ b/app/models/power_distribution_unit.rb @@ -25,6 +25,8 @@ class PowerDistributionUnit < ApplicationRecord validates :serial_number, presence: true, uniqueness: true, format: { without: /\s/ } validates :ipmi_url, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]), allow_blank: true + validate :type_cannot_change_if_connections_present + accepts_nested_attributes_for :circuits, allow_destroy: true delegate :to_s, to: :name @@ -43,8 +45,19 @@ def deep_dup end end + # TODO: remove me when connections are implemented + def connections + [] + end + private + def type_cannot_change_if_connections_present + return unless connections.present? && will_save_change_to_type? + + errors.add(:type, :cannot_change_if_connections_present) + end + def slug_candidates [ :name, diff --git a/app/views/power_distribution_units/_form.html.erb b/app/views/power_distribution_units/_form.html.erb index ed5487e08..23500c0c7 100644 --- a/app/views/power_distribution_units/_form.html.erb +++ b/app/views/power_distribution_units/_form.html.erb @@ -27,7 +27,8 @@ PowerDistributionUnitDecorator.power_distribution_unit_types_options_for_select, { prompt: true }, class: "form-select", - data: { controller: "select" } %> + data: { controller: "select" }, + disabled: f.object.connections.present? %> <% end %> diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 8af8254f3..db353ab12 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -303,6 +303,11 @@ en: domain_ids: blank: can't be blank, or "All domains" must be checked + power_distribution_unit: + attributes: + type: + cannot_change_if_connections_present: cannot bed changed if there is connections + # Common attributes attributes: bay_id: Bay diff --git a/config/locales/activerecord.fr.yml b/config/locales/activerecord.fr.yml index 09f438afc..3ef7d2a2f 100644 --- a/config/locales/activerecord.fr.yml +++ b/config/locales/activerecord.fr.yml @@ -540,6 +540,11 @@ fr: domain_ids: blank: doit être rempli, ou "Tous les domaines" doit être coché + power_distribution_unit: + attributes: + type: + cannot_change_if_connections_present: ne peut pas être changé s'il y a des connections + # Common attributes attributes: bay_id: Baie