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

@ -1,5 +1,5 @@
describe ActiveStorage::DownloadableFile do
let(:dossier) { create(:dossier) }
let(:dossier) { create(:dossier, :en_construction) }
subject(:list) { ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) }
@ -10,7 +10,7 @@ describe ActiveStorage::DownloadableFile do
context 'when there is a piece_justificative' do
before do
dossier.champs << create(:champ, :piece_justificative, :with_piece_justificative_file)
dossier.champs << create(:champ_piece_justificative, :with_piece_justificative_file, dossier: dossier)
end
it { expect(list.length).to eq 1 }
@ -18,15 +18,16 @@ describe ActiveStorage::DownloadableFile do
context 'when there is a private piece_justificative' do
before do
dossier.champs_private << create(:champ, :piece_justificative, :with_piece_justificative_file, private: true)
dossier.champs_private << create(:champ_piece_justificative, :with_piece_justificative_file, private: true, dossier: dossier)
end
it { expect(list.length).to eq 1 }
end
context 'when there is a repetition bloc' do
let(:champ) { build(:champ_repetition_with_piece_jointe) }
let(:dossier) { create(:dossier, :en_construction, champs: [champ]) }
before do
dossier.champs << build(:champ_repetition_with_piece_jointe, dossier: dossier)
end
it 'should have 4 piece_justificatives' do
expect(list.size).to eq 4
@ -35,7 +36,7 @@ describe ActiveStorage::DownloadableFile do
context 'when there is a message with no attachment' do
before do
dossier.commentaires << build(:commentaire, dossier: dossier)
dossier.commentaires << create(:commentaire, dossier: dossier)
end
it { expect(list.length).to eq 0 }
@ -43,7 +44,7 @@ describe ActiveStorage::DownloadableFile do
context 'when there is a message with an attachment' do
before do
dossier.commentaires << build(:commentaire, :with_file, dossier: dossier)
dossier.commentaires << create(:commentaire, :with_file, dossier: dossier)
end
it { expect(list.length).to eq 1 }