models: require belong_to associations on champ

- Make `champ.dossier` a requirement;
- Move the dossier_id assignation to `before_validation` (otherwise
the record is invalid, and never gets saved);
- Allow specs to only build the champ (instead of saving it to the
database), which bypasses the requirement to have a dossier.
This commit is contained in:
Pierre de La Morinerie 2020-07-20 15:18:44 +00:00
parent eb22dc9d8f
commit 6328011f60
13 changed files with 318 additions and 274 deletions

View file

@ -13,11 +13,11 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
context "there are some champs" do
let(:dossier) { create(:dossier) }
let(:avis) { create :avis, dossier: dossier, instructeur: instructeur }
let(:champ1) { create(:champ, :checkbox, value: "on") }
let(:champ2) { create(:champ, :header_section, value: "Section") }
let(:champ3) { create(:champ, :explication, value: "mazette") }
let(:champ4) { create(:champ, :dossier_link, value: dossier.id) }
let(:champ5) { create(:champ_textarea, value: "Some long text in a textarea.") }
let(:champ1) { create(:champ_checkbox, dossier: dossier, value: "on") }
let(:champ2) { create(:champ_header_section, dossier: dossier, value: "Section") }
let(:champ3) { create(:champ_explication, dossier: dossier, value: "mazette") }
let(:champ4) { create(:champ_dossier_link, dossier: dossier, value: dossier.id) }
let(:champ5) { create(:champ_textarea, dossier: dossier, value: "Some long text in a textarea.") }
let(:champs) { [champ1, champ2, champ3, champ4, champ5] }
before { dossier.avis << avis }
@ -71,7 +71,7 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
context "with seen_at" do
let(:dossier) { create(:dossier) }
let(:nouveau_groupe_instructeur) { create(:groupe_instructeur, procedure: dossier.procedure) }
let(:champ1) { create(:champ, :checkbox, value: "on") }
let(:champ1) { create(:champ_checkbox, dossier: dossier, value: "on") }
let(:champs) { [champ1] }
context "with a demande_seen_at after groupe_instructeur_updated_at" do
@ -96,7 +96,7 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
context "with a dossier champ, but we are not authorized to acces the dossier" do
let(:dossier) { create(:dossier) }
let(:champ) { create(:champ, :dossier_link, value: dossier.id) }
let(:champ) { create(:champ_dossier_link, dossier: dossier, value: dossier.id) }
let(:champs) { [champ] }
it { is_expected.not_to have_link("Dossier nº #{dossier.id}") }
@ -106,7 +106,7 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
context "with a dossier_link champ but without value" do
let(:dossier) { create(:dossier) }
let(:champ) { create(:champ, :dossier_link, value: nil) }
let(:champ) { create(:champ_dossier_link, dossier: dossier, value: nil) }
let(:champs) { [champ] }
it { is_expected.to include("Pas de dossier associé") }
@ -114,7 +114,7 @@ describe 'shared/dossiers/champs.html.haml', type: :view do
context "with seen_at" do
let(:dossier) { create(:dossier) }
let(:champ1) { create(:champ, :checkbox, value: "on") }
let(:champ1) { create(:champ_checkbox, dossier: dossier, value: "on") }
let(:champs) { [champ1] }
context "with a demande_seen_at after champ updated_at" do