Merge pull request #7355 from tchak/refactor-types-de-champ-for-export

refactor(revision): types_de_champ for export
This commit is contained in:
Paul Chavard 2022-05-23 18:40:40 +02:00 committed by GitHub
commit c55eb84bfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 41 deletions

View file

@ -112,27 +112,39 @@ class Procedure < ApplicationRecord
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
end
def types_de_champ_for_procedure_presentation
def types_de_champ_for_procedure_presentation(parent = nil)
if brouillon?
if parent.nil?
TypeDeChamp.fillable
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision: draft_revision, parent_id: nil })
.where(revision_types_de_champ: { revision_id: draft_revision_id, parent_id: nil })
.order(:private, :position)
else
draft_revision.children_of(parent)
end
else
# all published revisions
revision_ids = revisions.ids - [draft_revision_id]
# fetch all parent types de champ
parent_ids = if parent.present?
ProcedureRevisionTypeDeChamp
.where(revision_id: revision_ids)
.joins(:type_de_champ)
.where(type_de_champ: { stable_id: parent.stable_id })
.ids
end
# fetch all type_de_champ.stable_id for all the revisions expect draft
# and for each stable_id take the bigger (more recent) type_de_champ.id
recent_ids = TypeDeChamp.fillable
.joins(:revisions)
.where(procedure_revisions: { procedure_id: id })
.where.not(procedure_revisions: { id: draft_revision_id })
.where(revision_types_de_champ: { parent_id: nil })
.group(:stable_id)
.select('MAX(types_de_champ.id)')
recent_ids = TypeDeChamp
.fillable
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { revision_id: revision_ids, parent_id: parent_ids })
.group(:stable_id).select('MAX(types_de_champ.id)')
# fetch the more recent procedure_revision_types_de_champ
# which includes recents_ids
recents_prtdc = ProcedureRevisionTypeDeChamp
.root
.where(type_de_champ_id: recent_ids)
.where.not(revision_id: draft_revision_id)
.group(:type_de_champ_id)

View file

@ -304,23 +304,6 @@ class TypeDeChamp < ApplicationRecord
options.slice(*TypesDeChamp::CarteTypeDeChamp::LAYERS)
end
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
revision.children_of(self).fillable
else
# otherwise return all types_de_champ in their latest state
types_de_champ = TypeDeChamp
.fillable
.joins(parent: :revision_types_de_champ)
.where(parent: { stable_id: stable_id }, revision_types_de_champ: { revision_id: revision })
TypeDeChamp
.where(id: types_de_champ.group(:stable_id).select('MAX(types_de_champ.id)'))
.order(:order_place, id: :desc)
end
end
FEATURE_FLAGS = {}
def self.type_de_champ_types_for(procedure, user)

View file

@ -90,15 +90,17 @@ class ProcedureExportService
end
def champs_repetables_options
revision = procedure.active_revision
champs_by_stable_id = dossiers
.flat_map { |dossier| (dossier.champs + dossier.champs_private).filter(&:repetition?) }
.group_by(&:stable_id)
procedure.types_de_champ_for_procedure_presentation.repetition
.map { |type_de_champ_repetition| [type_de_champ_repetition, type_de_champ_repetition.types_de_champ_for_revision(revision).to_a] }
.filter { |(_, types_de_champ)| types_de_champ.present? }
.map do |(type_de_champ_repetition, types_de_champ)|
procedure
.types_de_champ_for_procedure_presentation
.repetition
.filter_map do |type_de_champ_repetition|
types_de_champ = procedure.types_de_champ_for_procedure_presentation(type_de_champ_repetition).to_a
if types_de_champ.present?
{
sheet_name: type_de_champ_repetition.libelle_for_export,
instances: champs_by_stable_id.fetch(type_de_champ_repetition.stable_id, []).flat_map(&:rows_for_export),
@ -106,6 +108,7 @@ class ProcedureExportService
}
end
end
end
DEFAULT_STYLES = {
header_style: { background_color: "000000", color: "FFFFFF", font_size: 12, bold: true },

View file

@ -1481,7 +1481,7 @@ describe Dossier do
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
type_champs = proc_test.types_de_champ_for_procedure_presentation(repetition).to_a
expect(type_champs.size).to eq(1)
expect(Dossier.champs_for_export(dossier.champs, type_champs).size).to eq(2)
end