From f38d2b84b898f929b5f74925708bca78398733df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Mon, 31 Aug 2026 17:56:51 +0200 Subject: [PATCH 1/4] add validation and tests --- app/models/card.rb | 10 ++++++++++ config/locales/activerecord.en.yml | 5 +++++ config/locales/activerecord.fr.yml | 5 +++++ spec/models/card_spec.rb | 23 +++++++++++++++++++++++ test/fixtures/card_types.yml | 2 +- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/models/card.rb b/app/models/card.rb index 2bad5a143..954c7493f 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -18,6 +18,7 @@ class Card < ApplicationRecord has_many :cables, through: :ports validates :first_position, numericality: { only_integer: true, in: 0..100 }, allow_nil: true + validate :ensure_card_type_have_enough_ports after_commit :set_twin_card @@ -69,4 +70,13 @@ def set_twin_card .update_all({ twin_card_id: nil }) # rubocop:disable Rails/SkipsModelValidations end end + + private + + def ensure_card_type_have_enough_ports + connection_count = ports.joins(:connections).count + port_quantity = card_type&.port_quantity || 0 + + errors.add(:card_type_id, :not_enough_ports) if connection_count > port_quantity + end end diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 52bc1e5cb..6ed703625 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -315,6 +315,11 @@ en: domain_ids: blank: can't be blank, or "All domains" must be checked + card: + attributes: + card_type_id: + not_enough_ports: doesn't contain enough ports + messages: contains_unpermitted_values: "includes unauthorized values: %{wrong_values}" diff --git a/config/locales/activerecord.fr.yml b/config/locales/activerecord.fr.yml index a9bd6f70d..87d52d202 100644 --- a/config/locales/activerecord.fr.yml +++ b/config/locales/activerecord.fr.yml @@ -548,6 +548,11 @@ fr: domain_ids: blank: doit être rempli, ou "Tous les domaines" doit être coché + card: + attributes: + card_type_id: + not_enough_ports: ne contient pas assez de ports + messages: contains_unpermitted_values: "contient des valeurs non autorisées : %{wrong_values}" diff --git a/spec/models/card_spec.rb b/spec/models/card_spec.rb index e4b3bd3d2..dddeae901 100644 --- a/spec/models/card_spec.rb +++ b/spec/models/card_spec.rb @@ -20,6 +20,29 @@ it { is_expected.to validate_numericality_of(:first_position).only_integer.is_in(0..100).allow_nil } end + describe "#ensure_card_type_have_enough_ports" do + context "when enough ports" do + before do + card.card_type = card_types(:four) + card.validate + end + + it { expect(card).to be_valid } + end + + context "when not enough ports" do + let(:card) { cards(:one) } + + before do + card.card_type = card_types(:two) + card.validate + end + + it { expect(card).not_to be_valid } + it { expect(card.errors.where(:card_type_id, :not_enough_ports).count).to eq(1) } + end + end + describe "#to_s" do it { expect(card.to_s).to eq("Carte ServerName1 / Card1 / compo1") } end diff --git a/test/fixtures/card_types.yml b/test/fixtures/card_types.yml index a87ea631a..9980801b7 100644 --- a/test/fixtures/card_types.yml +++ b/test/fixtures/card_types.yml @@ -2,7 +2,7 @@ one: id: 1 name: Card1 port_type_id: 3 - port_quantity: 2 + port_quantity: 3 two: id: 2 From 16abc074088ab0f00584d107fa0ed286c7c0e237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 3 Sep 2026 10:23:06 +0200 Subject: [PATCH 2/4] apply review changes --- app/models/card.rb | 4 ++-- spec/models/card_spec.rb | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/models/card.rb b/app/models/card.rb index 954c7493f..3a8f751a7 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -16,6 +16,7 @@ class Card < ApplicationRecord has_many :ports, as: :attachable, dependent: :destroy has_many :cables, through: :ports + has_many :connections, through: :ports, source: "connections" validates :first_position, numericality: { only_integer: true, in: 0..100 }, allow_nil: true validate :ensure_card_type_have_enough_ports @@ -74,9 +75,8 @@ def set_twin_card private def ensure_card_type_have_enough_ports - connection_count = ports.joins(:connections).count port_quantity = card_type&.port_quantity || 0 - errors.add(:card_type_id, :not_enough_ports) if connection_count > port_quantity + errors.add(:card_type_id, :not_enough_ports) if connections.count > port_quantity end end diff --git a/spec/models/card_spec.rb b/spec/models/card_spec.rb index dddeae901..4401373ca 100644 --- a/spec/models/card_spec.rb +++ b/spec/models/card_spec.rb @@ -21,22 +21,20 @@ end describe "#ensure_card_type_have_enough_ports" do + before do + card.card_type = card_type + card.validate + end + context "when enough ports" do - before do - card.card_type = card_types(:four) - card.validate - end + let(:card_type) { card_types(:four) } it { expect(card).to be_valid } end context "when not enough ports" do let(:card) { cards(:one) } - - before do - card.card_type = card_types(:two) - card.validate - end + let(:card_type) { card_types(:two) } it { expect(card).not_to be_valid } it { expect(card.errors.where(:card_type_id, :not_enough_ports).count).to eq(1) } From 80641da322a4a421d951250819c0911899561130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 3 Sep 2026 16:43:29 +0200 Subject: [PATCH 3/4] fix error message --- config/locales/activerecord.fr.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/locales/activerecord.fr.yml b/config/locales/activerecord.fr.yml index 87d52d202..45c339411 100644 --- a/config/locales/activerecord.fr.yml +++ b/config/locales/activerecord.fr.yml @@ -516,6 +516,8 @@ fr: port_type_id: Type de port port_type: Type de port number: Numéro + cards: + card_type_id: "Type de carte" errors: models: From cf12aaa04d169cb86f4f9f537194d89651bcd693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Filmont?= Date: Thu, 3 Sep 2026 17:41:09 +0200 Subject: [PATCH 4/4] add missing trad --- config/locales/activerecord.en.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 6ed703625..8711fc255 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -283,6 +283,8 @@ en: port_type: Port type port_type_id: Port type number: Number + cards: + card_type_id: Card type errors: models: