Merge pull request #7355 from tchak/refactor-types-de-champ-for-export
refactor(revision): types_de_champ for export
This commit is contained in:
commit
c55eb84bfb
4 changed files with 39 additions and 41 deletions
|
@ -112,27 +112,39 @@ class Procedure < ApplicationRecord
|
||||||
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
|
brouillon? ? draft_types_de_champ_private : published_types_de_champ_private
|
||||||
end
|
end
|
||||||
|
|
||||||
def types_de_champ_for_procedure_presentation
|
def types_de_champ_for_procedure_presentation(parent = nil)
|
||||||
if brouillon?
|
if brouillon?
|
||||||
TypeDeChamp.fillable
|
if parent.nil?
|
||||||
.joins(:revision_types_de_champ)
|
TypeDeChamp.fillable
|
||||||
.where(revision_types_de_champ: { revision: draft_revision, parent_id: nil })
|
.joins(:revision_types_de_champ)
|
||||||
.order(:private, :position)
|
.where(revision_types_de_champ: { revision_id: draft_revision_id, parent_id: nil })
|
||||||
|
.order(:private, :position)
|
||||||
|
else
|
||||||
|
draft_revision.children_of(parent)
|
||||||
|
end
|
||||||
else
|
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
|
# 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
|
# and for each stable_id take the bigger (more recent) type_de_champ.id
|
||||||
recent_ids = TypeDeChamp.fillable
|
recent_ids = TypeDeChamp
|
||||||
.joins(:revisions)
|
.fillable
|
||||||
.where(procedure_revisions: { procedure_id: id })
|
.joins(:revision_types_de_champ)
|
||||||
.where.not(procedure_revisions: { id: draft_revision_id })
|
.where(revision_types_de_champ: { revision_id: revision_ids, parent_id: parent_ids })
|
||||||
.where(revision_types_de_champ: { parent_id: nil })
|
.group(:stable_id).select('MAX(types_de_champ.id)')
|
||||||
.group(:stable_id)
|
|
||||||
.select('MAX(types_de_champ.id)')
|
|
||||||
|
|
||||||
# fetch the more recent procedure_revision_types_de_champ
|
# fetch the more recent procedure_revision_types_de_champ
|
||||||
# which includes recents_ids
|
# which includes recents_ids
|
||||||
recents_prtdc = ProcedureRevisionTypeDeChamp
|
recents_prtdc = ProcedureRevisionTypeDeChamp
|
||||||
.root
|
|
||||||
.where(type_de_champ_id: recent_ids)
|
.where(type_de_champ_id: recent_ids)
|
||||||
.where.not(revision_id: draft_revision_id)
|
.where.not(revision_id: draft_revision_id)
|
||||||
.group(:type_de_champ_id)
|
.group(:type_de_champ_id)
|
||||||
|
|
|
@ -304,23 +304,6 @@ class TypeDeChamp < ApplicationRecord
|
||||||
options.slice(*TypesDeChamp::CarteTypeDeChamp::LAYERS)
|
options.slice(*TypesDeChamp::CarteTypeDeChamp::LAYERS)
|
||||||
end
|
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 = {}
|
FEATURE_FLAGS = {}
|
||||||
|
|
||||||
def self.type_de_champ_types_for(procedure, user)
|
def self.type_de_champ_types_for(procedure, user)
|
||||||
|
|
|
@ -90,20 +90,23 @@ class ProcedureExportService
|
||||||
end
|
end
|
||||||
|
|
||||||
def champs_repetables_options
|
def champs_repetables_options
|
||||||
revision = procedure.active_revision
|
|
||||||
champs_by_stable_id = dossiers
|
champs_by_stable_id = dossiers
|
||||||
.flat_map { |dossier| (dossier.champs + dossier.champs_private).filter(&:repetition?) }
|
.flat_map { |dossier| (dossier.champs + dossier.champs_private).filter(&:repetition?) }
|
||||||
.group_by(&:stable_id)
|
.group_by(&:stable_id)
|
||||||
|
|
||||||
procedure.types_de_champ_for_procedure_presentation.repetition
|
procedure
|
||||||
.map { |type_de_champ_repetition| [type_de_champ_repetition, type_de_champ_repetition.types_de_champ_for_revision(revision).to_a] }
|
.types_de_champ_for_procedure_presentation
|
||||||
.filter { |(_, types_de_champ)| types_de_champ.present? }
|
.repetition
|
||||||
.map do |(type_de_champ_repetition, types_de_champ)|
|
.filter_map do |type_de_champ_repetition|
|
||||||
{
|
types_de_champ = procedure.types_de_champ_for_procedure_presentation(type_de_champ_repetition).to_a
|
||||||
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),
|
if types_de_champ.present?
|
||||||
spreadsheet_columns: Proc.new { |instance| instance.spreadsheet_columns(types_de_champ) }
|
{
|
||||||
}
|
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),
|
||||||
|
spreadsheet_columns: Proc.new { |instance| instance.spreadsheet_columns(types_de_champ) }
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1481,7 +1481,7 @@ describe Dossier do
|
||||||
|
|
||||||
dossier_test = create(:dossier, procedure: proc_test)
|
dossier_test = create(:dossier, procedure: proc_test)
|
||||||
repetition = proc_test.types_de_champ_for_procedure_presentation.repetition.first
|
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(type_champs.size).to eq(1)
|
||||||
expect(Dossier.champs_for_export(dossier.champs, type_champs).size).to eq(2)
|
expect(Dossier.champs_for_export(dossier.champs, type_champs).size).to eq(2)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue