diff --git a/app/models/card.rb b/app/models/card.rb index 2bad5a143..3a8f751a7 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -16,8 +16,10 @@ 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 after_commit :set_twin_card @@ -69,4 +71,12 @@ def set_twin_card .update_all({ twin_card_id: nil }) # rubocop:disable Rails/SkipsModelValidations end end + + private + + def ensure_card_type_have_enough_ports + port_quantity = card_type&.port_quantity || 0 + + errors.add(:card_type_id, :not_enough_ports) if connections.count > port_quantity + end end diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 52bc1e5cb..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: @@ -315,6 +317,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..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: @@ -548,6 +550,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..4401373ca 100644 --- a/spec/models/card_spec.rb +++ b/spec/models/card_spec.rb @@ -20,6 +20,27 @@ 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 + before do + card.card_type = card_type + card.validate + end + + context "when enough ports" do + let(:card_type) { card_types(:four) } + + it { expect(card).to be_valid } + end + + context "when not enough ports" do + let(:card) { cards(:one) } + 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) } + 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