Merge pull request #7342 from betagouv/remove_repetition_types_de_champ
Refactor(revision): retire l'usage de repetition.types_de_champ
This commit is contained in:
commit
49aece457e
11 changed files with 62 additions and 37 deletions
|
@ -3,7 +3,7 @@ class Champs::RepetitionController < ApplicationController
|
|||
|
||||
def add
|
||||
@champ = policy_scope(Champ).includes(:champs).find(params[:champ_id])
|
||||
@champs = @champ.add_row
|
||||
@champs = @champ.add_row(@champ.dossier.revision)
|
||||
end
|
||||
|
||||
def remove
|
||||
|
|
|
@ -27,11 +27,11 @@ class Champs::RepetitionChamp < Champ
|
|||
champs.group_by(&:row).values
|
||||
end
|
||||
|
||||
def add_row
|
||||
def add_row(revision)
|
||||
added_champs = []
|
||||
transaction do
|
||||
row = (blank? ? -1 : champs.last.row) + 1
|
||||
type_de_champ.types_de_champ.each do |type_de_champ|
|
||||
revision.children_of(type_de_champ).each do |type_de_champ|
|
||||
added_champs << type_de_champ.champ.build(row: row)
|
||||
end
|
||||
self.champs << added_champs
|
||||
|
|
|
@ -112,7 +112,7 @@ module DossierRebaseConcern
|
|||
end
|
||||
end
|
||||
else
|
||||
champ = published_type_de_champ.build_champ
|
||||
champ = published_type_de_champ.build_champ(revision: procedure.published_revision)
|
||||
self.champs << champ
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,11 +31,11 @@ class ProcedureRevision < ApplicationRecord
|
|||
scope :ordered, -> { order(:created_at) }
|
||||
|
||||
def build_champs
|
||||
types_de_champ_public.map(&:build_champ)
|
||||
types_de_champ_public.map { |tdc| tdc.build_champ(revision: self) }
|
||||
end
|
||||
|
||||
def build_champs_private
|
||||
types_de_champ_private.map(&:build_champ)
|
||||
types_de_champ_private.map { |tdc| tdc.build_champ(revision: self) }
|
||||
end
|
||||
|
||||
def add_type_de_champ(params)
|
||||
|
|
|
@ -102,10 +102,12 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
|
||||
def build(params = {})
|
||||
params.delete(:revision)
|
||||
super(params.merge(proxy_association.owner.params_for_champ))
|
||||
end
|
||||
|
||||
def create(params = {})
|
||||
params.delete(:revision)
|
||||
super(params.merge(proxy_association.owner.params_for_champ))
|
||||
end
|
||||
end
|
||||
|
@ -153,8 +155,8 @@ class TypeDeChamp < ApplicationRecord
|
|||
}
|
||||
end
|
||||
|
||||
def build_champ
|
||||
dynamic_type.build_champ
|
||||
def build_champ(params)
|
||||
dynamic_type.build_champ(params)
|
||||
end
|
||||
|
||||
def check_mandatory
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
def build_champ
|
||||
def build_champ(params)
|
||||
revision = params[:revision]
|
||||
champ = super
|
||||
champ.add_row
|
||||
champ.add_row(revision)
|
||||
champ
|
||||
end
|
||||
|
||||
|
|
|
@ -25,8 +25,8 @@ class TypesDeChamp::TypeDeChampBase
|
|||
libelle
|
||||
end
|
||||
|
||||
def build_champ
|
||||
@type_de_champ.champ.build
|
||||
def build_champ(params)
|
||||
@type_de_champ.champ.build(params)
|
||||
end
|
||||
|
||||
def filter_to_human(filter_value)
|
||||
|
|
|
@ -74,7 +74,12 @@ describe Champ do
|
|||
create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition, types_de_champ_count: 1, types_de_champ_private_count: 1).tap do |procedure|
|
||||
create(:type_de_champ_header_section, procedure: procedure)
|
||||
create(:type_de_champ_header_section, procedure: procedure, private: true)
|
||||
create(:type_de_champ_header_section, parent: procedure.types_de_champ.find(&:repetition?))
|
||||
|
||||
procedure.active_revision.add_type_de_champ(
|
||||
libelle: 'header',
|
||||
type_champ: 'header_section',
|
||||
parent_id: procedure.types_de_champ.find(&:repetition?).stable_id
|
||||
)
|
||||
end
|
||||
end
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
@ -508,11 +513,9 @@ describe Champ do
|
|||
end
|
||||
|
||||
describe 'repetition' do
|
||||
let(:procedure) { create(:procedure, :published, :with_type_de_champ, :with_type_de_champ_private, types_de_champ: [build(:type_de_champ_repetition, types_de_champ: [tdc_text, tdc_integer])]) }
|
||||
let(:revision) { procedure.published_revision }
|
||||
let(:rtdc_repetition) { revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ.repetition? } }
|
||||
let(:tdc_text) { build(:type_de_champ_text) }
|
||||
let(:tdc_integer) { build(:type_de_champ_integer_number) }
|
||||
let(:procedure) { create(:procedure, :published, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
|
||||
let(:tdc_repetition) { procedure.types_de_champ.find(&:repetition?) }
|
||||
let(:tdc_text) { procedure.active_revision.children_of(tdc_repetition).first }
|
||||
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
let(:champ) { dossier.champs.find(&:repetition?) }
|
||||
|
@ -521,8 +524,7 @@ describe Champ do
|
|||
let(:champ_text_attrs) { attributes_for(:champ_text, type_de_champ: tdc_text, row: 1) }
|
||||
|
||||
before do
|
||||
create(:procedure_revision_type_de_champ, revision: revision, type_de_champ: tdc_text, parent: rtdc_repetition, position: 1)
|
||||
create(:procedure_revision_type_de_champ, revision: revision, type_de_champ: tdc_integer, parent: rtdc_repetition, position: 0)
|
||||
procedure.active_revision.add_type_de_champ(libelle: 'sub integer', type_champ: 'integer_number', parent_id: tdc_repetition.stable_id)
|
||||
end
|
||||
|
||||
context 'when creating the model directly' do
|
||||
|
@ -557,12 +559,12 @@ describe Champ do
|
|||
champ.champs << champ_integer
|
||||
first_row = champ.reload.rows.first
|
||||
expect(first_row.size).to eq(2)
|
||||
expect(first_row.first).to eq(champ_integer)
|
||||
expect(first_row.second).to eq(champ_integer)
|
||||
|
||||
champ.champs << champ_text
|
||||
first_row = champ.reload.rows.first
|
||||
expect(first_row.size).to eq(2)
|
||||
expect(first_row.second).to eq(champ_text)
|
||||
expect(first_row.first).to eq(champ_text)
|
||||
|
||||
expect(champ.rows.size).to eq(2)
|
||||
end
|
||||
|
|
|
@ -23,12 +23,26 @@ describe Champs::HeaderSectionChamp do
|
|||
end
|
||||
|
||||
context 'for repetition champs' do
|
||||
let(:procedure) { create(:procedure, types_de_champ: [build(:type_de_champ_repetition, types_de_champ: types_de_champ)]) }
|
||||
let(:procedure) { create(:procedure, :with_repetition) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
let(:first_header) { dossier.champs.first.champs[0] }
|
||||
let(:second_header) { dossier.champs.first.champs[3] }
|
||||
|
||||
before do
|
||||
revision = procedure.active_revision
|
||||
tdc_repetition = revision.types_de_champ_public.first
|
||||
revision.remove_type_de_champ(revision.children_of(tdc_repetition))
|
||||
|
||||
types_de_champ.each do |tdc|
|
||||
revision.add_type_de_champ(
|
||||
libelle: tdc.libelle,
|
||||
type_champ: tdc.type_champ,
|
||||
parent_id: tdc_repetition.stable_id
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns the index of the section in the repetition (starting from 1)' do
|
||||
expect(first_header.section_index).to eq 1
|
||||
expect(second_header.section_index).to eq 2
|
||||
|
|
|
@ -161,19 +161,23 @@ describe TagsSubstitutionConcern, type: :model do
|
|||
|
||||
context 'when the procedure has a type de champ repetition' do
|
||||
let(:template) { '--Répétition--' }
|
||||
let(:types_de_champ) do
|
||||
[
|
||||
build(:type_de_champ_repetition, libelle: 'Répétition', types_de_champ: [
|
||||
build(:type_de_champ_text, libelle: 'Nom', order_place: 1),
|
||||
build(:type_de_champ_text, libelle: 'Prénom', order_place: 2)
|
||||
])
|
||||
]
|
||||
let(:repetition) do
|
||||
repetition_tdc = procedure.active_revision.add_type_de_champ(type_champ: 'repetition', libelle: 'Répétition')
|
||||
procedure.active_revision.add_type_de_champ(type_champ: 'text', libelle: 'Nom', parent_id: repetition_tdc.stable_id)
|
||||
procedure.active_revision.add_type_de_champ(type_champ: 'text', libelle: 'Prénom', parent_id: repetition_tdc.stable_id)
|
||||
|
||||
repetition_tdc
|
||||
end
|
||||
|
||||
let(:dossier) do
|
||||
repetition
|
||||
create(:dossier, procedure: procedure)
|
||||
end
|
||||
|
||||
before do
|
||||
repetition = dossier.champs
|
||||
.find { |champ| champ.libelle == 'Répétition' }
|
||||
repetition.add_row
|
||||
repetition.add_row(dossier.revision)
|
||||
paul_champs, pierre_champs = repetition.rows
|
||||
|
||||
paul_champs.first.update(value: 'Paul')
|
||||
|
|
|
@ -1190,11 +1190,13 @@ describe Dossier do
|
|||
end
|
||||
|
||||
context "with champ repetition" do
|
||||
let(:procedure) { create(:procedure, types_de_champ: [type_de_champ_repetition]) }
|
||||
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, mandatory: true) }
|
||||
let(:procedure) { create(:procedure, :with_repetition) }
|
||||
let(:revision) { procedure.active_revision }
|
||||
let(:type_de_champ_repetition) { revision.types_de_champ.first }
|
||||
|
||||
before do
|
||||
create(:type_de_champ_text, mandatory: true, parent: type_de_champ_repetition)
|
||||
type_de_champ_repetition.update(mandatory: true)
|
||||
revision.children_of(type_de_champ_repetition).first.update(mandatory: true)
|
||||
end
|
||||
|
||||
context "when no champs" do
|
||||
|
@ -1215,7 +1217,7 @@ describe Dossier do
|
|||
let(:champ_with_error) { dossier.champs.first.champs.first }
|
||||
|
||||
before do
|
||||
dossier.champs.first.add_row
|
||||
dossier.champs.first.add_row(dossier.revision)
|
||||
end
|
||||
|
||||
it 'should have errors' do
|
||||
|
@ -1763,8 +1765,8 @@ describe Dossier do
|
|||
datetime_champ.update(value: Date.today.to_s)
|
||||
text_champ.update(value: 'bonjour')
|
||||
# Add two rows then remove previous to last row in order to create a "hole" in the sequence
|
||||
repetition_champ.add_row
|
||||
repetition_champ.add_row
|
||||
repetition_champ.add_row(repetition_champ.dossier.revision)
|
||||
repetition_champ.add_row(repetition_champ.dossier.revision)
|
||||
repetition_champ.champs.where(row: repetition_champ.champs.last.row - 1).destroy_all
|
||||
repetition_champ.reload
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue