From 5b6044803b73d8a55db7190bfec33986e99ab2d3 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 2 Apr 2020 20:17:26 +0200 Subject: [PATCH] forbid to create champ with same type_de_champ and same row --- app/models/champ.rb | 2 ++ spec/models/champ_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/champ.rb b/app/models/champ.rb index 35936912b..7c21fdae2 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -21,6 +21,8 @@ class Champ < ApplicationRecord before_create :set_dossier_id, if: :needs_dossier_id? + validates :type_de_champ_id, uniqueness: { scope: [:dossier_id, :row] } + def public? !private? end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 8d4c6b0af..f0368e424 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -3,6 +3,16 @@ describe Champ do it_should_behave_like "champ_spec" + describe "validations" do + let(:row) { 1 } + let(:champ) { create(:champ, type_de_champ: create(:type_de_champ), row: row) } + let(:champ2) { build(:champ, type_de_champ: champ.type_de_champ, row: champ.row, dossier: champ.dossier) } + + it "returns false when champ with same type_de_champ and row already exist" do + expect(champ2).not_to be_valid + end + end + describe '#public?' do let(:type_de_champ) { build(:type_de_champ) } let(:champ) { type_de_champ.champ.build }