add procedure_revision children_of

This commit is contained in:
simon lehericey 2022-05-06 15:47:57 +02:00
parent 7426e8b16f
commit d2509e7492
2 changed files with 59 additions and 0 deletions

View file

@ -140,6 +140,14 @@ class ProcedureRevision < ApplicationRecord
dossier
end
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")
end
private
def compare_attestation_template(from_at, to_at)

View file

@ -440,4 +440,55 @@ describe ProcedureRevision do
end
end
end
describe 'children_of' do
context 'with a simple tdc' do
let(:procedure) { create(:procedure, :with_type_de_champ) }
it { expect(draft.children_of(draft.types_de_champ.first)).to be_empty }
end
context 'with a repetition tdc' do
let(:procedure) { create(:procedure, :with_repetition) }
let!(:parent) { draft.types_de_champ.find(&:repetition?) }
let!(:child) { draft.types_de_champ.reject(&:repetition?).first }
it { expect(draft.children_of(parent)).to match([child]) }
context 'with multiple child' do
let(:child_position_2) { create(:type_de_champ_text) }
let(:child_position_1) { create(:type_de_champ_text) }
before do
parent_coordinate = draft.revision_types_de_champ.find_by(type_de_champ_id: parent.id)
draft.revision_types_de_champ.create(type_de_champ: child_position_2, position: 2, parent_id: parent_coordinate.id)
draft.revision_types_de_champ.create(type_de_champ: child_position_1, position: 1, parent_id: parent_coordinate.id)
end
it 'returns the children in order' do
expect(draft.children_of(parent)).to eq([child, child_position_1, child_position_2])
end
end
context 'with multiple revision' do
let(:new_child) { create(:type_de_champ_text) }
let(:new_draft) do
procedure.publish!
procedure.draft_revision
end
before do
new_draft
.revision_types_de_champ
.where(type_de_champ: child)
.update(type_de_champ: new_child)
end
it 'returns the children regarding the revision' do
expect(draft.children_of(parent)).to match([child])
expect(new_draft.children_of(parent)).to match([new_child])
end
end
end
end
end