Merge pull request #11123 from tchak/refactor-champs-migration-tasks
migration(champs): remove non fillable champs and add repetition rows
This commit is contained in:
commit
6eb8982db9
2 changed files with 66 additions and 0 deletions
|
@ -0,0 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Maintenance
|
||||
class T20241202migrateNonFillableAndRepetitionChampsTask < MaintenanceTasks::Task
|
||||
# Documentation : cette tâche ajoute les représentations des rows sur les répétitions et supprime les champs sans valeurs
|
||||
|
||||
include RunnableOnDeployConcern
|
||||
include StatementsHelpersConcern
|
||||
|
||||
def collection
|
||||
Dossier.includes(champs: [], revision: { revision_types_de_champ: { parent_type_de_champ: [], types_de_champ: [] }, types_de_champ_public: [], types_de_champ_private: [], types_de_champ: [] })
|
||||
end
|
||||
|
||||
def process(dossier)
|
||||
Champs::HeaderSectionChamp.where(dossier:).destroy_all
|
||||
Champs::ExplicationChamp.where(dossier:).destroy_all
|
||||
Champs::RepetitionChamp.where(dossier:, row_id: nil).destroy_all
|
||||
|
||||
create_rows(dossier)
|
||||
end
|
||||
|
||||
def create_rows(dossier)
|
||||
repetitions = dossier.revision.types_de_champ.filter(&:repetition?)
|
||||
repetitions.each do |type_de_champ|
|
||||
stable_id = type_de_champ.stable_id
|
||||
row_ids = dossier.repetition_row_ids(type_de_champ)
|
||||
row_ids.each do |row_id|
|
||||
Champ.create_with(**type_de_champ.params_for_champ)
|
||||
.find_or_create_by!(dossier:, stable_id:, row_id:, stream: 'main')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
module Maintenance
|
||||
RSpec.describe T20241202migrateNonFillableAndRepetitionChampsTask do
|
||||
describe "remove header_section, explication and repetition" do
|
||||
subject(:process) { described_class.process(dossier) }
|
||||
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :header_section }, { type: :explication }, {}, { type: :repetition, children: [{}] }]) }
|
||||
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
||||
|
||||
before {
|
||||
header_section, explication, _, repetition = dossier.revision.types_de_champ_public
|
||||
dossier.champs.create(**header_section.params_for_champ)
|
||||
dossier.champs.create(**explication.params_for_champ)
|
||||
dossier.champs.create(**repetition.params_for_champ)
|
||||
}
|
||||
|
||||
it { expect { subject }.to change { Champ.count }.by(-3) }
|
||||
end
|
||||
|
||||
describe "create rows" do
|
||||
subject(:process) { described_class.process(dossier) }
|
||||
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{}] }]) }
|
||||
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
||||
|
||||
before { dossier.champs.filter(&:row?).each(&:destroy!) }
|
||||
|
||||
it { expect { subject }.to change { Champs::RepetitionChamp.count }.by(2) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue