Merge pull request #7344 from betagouv/remove_tdc_on_tdc

Refactor(revision): enleve l'usage de types_de_champ dans le modèle type_de_champ
This commit is contained in:
LeSim 2022-05-18 12:21:00 +02:00 committed by GitHub
commit 61c22e6324
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 16 deletions

View file

@ -157,9 +157,13 @@ class ProcedureRevision < ApplicationRecord
def children_of(tdc)
parent_revision_type_de_champ = revision_types_de_champ.find_by(type_de_champ: tdc)
types_de_champ
.where(procedure_revision_types_de_champ: { parent_id: parent_revision_type_de_champ.id })
.order("procedure_revision_types_de_champ.position")
if parent_revision_type_de_champ.present?
types_de_champ
.where(procedure_revision_types_de_champ: { parent_id: parent_revision_type_de_champ.id })
.order("procedure_revision_types_de_champ.position")
else
TypeDeChamp.none
end
end
private

View file

@ -307,7 +307,7 @@ class TypeDeChamp < ApplicationRecord
def types_de_champ_for_revision(revision)
if revision.draft?
# if we are asking for children on a draft revision, just use current child types_de_champ
types_de_champ.fillable
revision.children_of(self).fillable
else
# otherwise return all types_de_champ in their latest state
types_de_champ = TypeDeChamp
@ -433,8 +433,11 @@ class TypeDeChamp < ApplicationRecord
end
def remove_repetition
if !repetition?
types_de_champ.destroy_all
if !repetition? && procedure.present?
procedure
.draft_revision # action occurs only on draft
.children_of(self)
.destroy_all
end
end
end

View file

@ -190,12 +190,6 @@ FactoryBot.define do
end
end
trait :with_repetition_commune do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_repetition, types_de_champ: [build(:type_de_champ_communes)], procedure: procedure)
end
end
trait :with_number do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_number, procedure: procedure)

View file

@ -1470,7 +1470,13 @@ describe Dossier do
context 'within a repetition having a type de champs commune (multiple values for export)' do
it 'works' do
proc_test = create(:procedure, :with_repetition_commune)
proc_test = create(:procedure)
draft = proc_test.draft_revision
tdc_repetition = draft.add_type_de_champ(type_champ: :repetition, libelle: "repetition")
draft.add_type_de_champ(type_champ: :communes, libelle: "communes", parent_id: tdc_repetition.stable_id)
dossier_test = create(:dossier, procedure: proc_test)
repetition = proc_test.types_de_champ_for_procedure_presentation.repetition.first
type_champs = repetition.types_de_champ_for_revision(proc_test.active_revision).to_a
@ -1734,7 +1740,7 @@ describe Dossier do
let(:rebased_datetime_champ) { dossier.champs.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:date) } }
let(:repetition_type_de_champ) { procedure.types_de_champ.find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } }
let(:repetition_text_type_de_champ) { repetition_type_de_champ.types_de_champ.find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:text) } }
let(:repetition_text_type_de_champ) { procedure.active_revision.children_of(repetition_type_de_champ).find { |tdc| tdc.type_champ == TypeDeChamp.type_champs.fetch(:text) } }
let(:repetition_champ) { dossier.champs.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } }
let(:rebased_repetition_champ) { dossier.champs.find { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } }

View file

@ -409,7 +409,7 @@ describe ProcedureRevision do
let(:procedure) { create(:procedure, :with_repetition) }
before do
child = new_draft.types_de_champ_public.last.types_de_champ.first
child = new_draft.children_of(new_draft.types_de_champ_public.last).first
new_draft.find_or_clone_type_de_champ(child.stable_id).update(type_champ: :drop_down_list, drop_down_options: ['one', 'two'])
end

View file

@ -101,7 +101,7 @@ shared_examples 'type_de_champ_spec' do
let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:text) }
it 'removes the children types de champ' do
expect(tdc.types_de_champ).to be_empty
expect(procedure.draft_revision.children_of(tdc)).to be_empty
end
end
end