Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions config/locales/activerecord.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ en:
port_type: Port type
port_type_id: Port type
number: Number
cards:
card_type_id: Card type

errors:
models:
Expand Down Expand Up @@ -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}"

Expand Down
7 changes: 7 additions & 0 deletions config/locales/activerecord.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}"

Expand Down
21 changes: 21 additions & 0 deletions spec/models/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Comment thread
nicolas-brousse marked this conversation as resolved.
describe "#to_s" do
it { expect(card.to_s).to eq("Carte ServerName1 / Card1 / compo1") }
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/card_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ one:
id: 1
name: Card1
port_type_id: 3
port_quantity: 2
port_quantity: 3

two:
id: 2
Expand Down
Loading