refactor(repetition): use less champs and public_id methods

This commit is contained in:
Paul Chavard 2024-08-22 17:29:49 +02:00
parent b4ed11c788
commit 3817f334c9
No known key found for this signature in database
4 changed files with 47 additions and 44 deletions

View file

@ -94,7 +94,7 @@ class Champ < ApplicationRecord
end
def child?
parent_id.present?
row_id.present?
end
# used for the `required` html attribute

View file

@ -37,7 +37,7 @@ class Champs::RepetitionChamp < Champ
end
def blank?
champs.empty?
row_ids.empty?
end
def search_terms

View file

@ -8,31 +8,50 @@ describe Champ do
let(:champ) { Champ.new(value: value) }
let(:value) { '' }
let(:mandatory) { true }
before { allow(champ).to receive(:type_de_champ).and_return(type_de_champ) }
context 'when mandatory and blank' do
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'with champ' do
before { allow(champ).to receive(:type_de_champ).and_return(type_de_champ) }
context 'when carte mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) }
let(:champ) { Champs::CarteChamp.new(value: value) }
let(:value) { nil }
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when mandatory and blank' do
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when multiple_drop_down_list mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, mandatory: mandatory) }
let(:champ) { Champs::MultipleDropDownListChamp.new(value: value) }
let(:value) { '[]' }
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when carte mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) }
let(:champ) { Champs::CarteChamp.new(value: value) }
let(:value) { nil }
it { expect(champ.mandatory_blank?).to be(true) }
end
context 'when repetition blank' do
let(:type_de_champ) { build(:type_de_champ_repetition) }
let(:champ) { Champs::RepetitionChamp.new }
context 'when multiple_drop_down_list mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, mandatory: mandatory) }
let(:champ) { Champs::MultipleDropDownListChamp.new(value: value) }
let(:value) { '[]' }
it { expect(champ.mandatory_blank?).to be(true) }
end
it { expect(champ.blank?).to be(true) }
context 'when repetition blank' do
let(:type_de_champ) { build(:type_de_champ_repetition) }
let(:champ) { Champs::RepetitionChamp.new(dossier: Dossier.new(revision: ProcedureRevision.new)) }
it { expect(champ.blank?).to be(true) }
end
context 'when not blank' do
let(:value) { 'yop' }
it { expect(champ.mandatory_blank?).to be(false) }
end
context 'when not mandatory' do
let(:mandatory) { false }
it { expect(champ.mandatory_blank?).to be(false) }
end
context 'when not mandatory or blank' do
let(:value) { 'u' }
let(:mandatory) { false }
it { expect(champ.mandatory_blank?).to be(false) }
end
end
context 'when repetition not blank' do
@ -42,22 +61,6 @@ describe Champ do
it { expect(champ.blank?).to be(false) }
end
context 'when not blank' do
let(:value) { 'yop' }
it { expect(champ.mandatory_blank?).to be(false) }
end
context 'when not mandatory' do
let(:mandatory) { false }
it { expect(champ.mandatory_blank?).to be(false) }
end
context 'when not mandatory or blank' do
let(:value) { 'u' }
let(:mandatory) { false }
it { expect(champ.mandatory_blank?).to be(false) }
end
end
describe "associations" do
@ -125,7 +128,7 @@ describe Champ do
let(:standalone_champ) { build(:champ, type_de_champ: build(:type_de_champ), dossier: build(:dossier)) }
let(:public_sections) { dossier.champs_public.filter(&:header_section?) }
let(:private_sections) { dossier.champs_private.filter(&:header_section?) }
let(:sections_in_repetition) { champ_in_repetition.parent.champs.filter(&:header_section?) }
let(:sections_in_repetition) { dossier.champs.filter(&:child?).filter(&:header_section?) }
it 'returns the sibling sections of a champ' do
expect(public_sections).not_to be_empty

View file

@ -390,11 +390,11 @@ describe ProcedureExportService do
context 'with long libelle composed of utf8 characteres' do
before do
procedure.active_revision.types_de_champ_public.each do |c|
c.update!(libelle: "#{c.id} - ?/[] ééé ééé ééééééé ééééééé éééééééé. ééé éé éééééééé éé ééé. ééééé éééééééé ééé ééé.")
procedure.active_revision.types_de_champ_public.each do |type_de_champ|
type_de_champ.update!(libelle: "#{type_de_champ.id} - ?/[] ééé ééé ééééééé ééééééé éééééééé. ééé éé éééééééé éé ééé. ééééé éééééééé ééé ééé.")
end
champ_repetition.champs.each do |c|
c.type_de_champ.update!(libelle: "#{c.id} - Quam rem nam maiores numquam dolorem nesciunt. Cum et possimus et aut. Fugit voluptas qui qui.")
procedure.active_revision.children_of(champ_repetition.type_de_champ).each do |type_de_champ|
type_de_champ.update!(libelle: "#{type_de_champ.id} - Quam rem nam maiores numquam dolorem nesciunt. Cum et possimus et aut. Fugit voluptas qui qui.")
end
end
@ -417,7 +417,7 @@ describe ProcedureExportService do
context 'with empty repetition' do
before do
dossiers.flat_map { |dossier| dossier.champs_public.filter(&:repetition?) }.each do |champ|
champ.champs.destroy_all
Champ.where(row_id: champ.row_ids).destroy_all
end
end