Expose removed types_de_champ in exports

This commit is contained in:
Paul Chavard 2021-06-22 10:17:10 +02:00
parent eadae7af6b
commit 19195008e8
6 changed files with 22 additions and 45 deletions

View file

@ -764,19 +764,19 @@ class Dossier < ApplicationRecord
log_dossier_operation(avis.claimant, :demander_un_avis, avis)
end
def spreadsheet_columns_csv(types_de_champ:, types_de_champ_private:)
spreadsheet_columns(with_etablissement: true, types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private)
def spreadsheet_columns_csv(types_de_champ:)
spreadsheet_columns(with_etablissement: true, types_de_champ: types_de_champ)
end
def spreadsheet_columns_xlsx(types_de_champ:, types_de_champ_private:)
spreadsheet_columns(types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private)
def spreadsheet_columns_xlsx(types_de_champ:)
spreadsheet_columns(types_de_champ: types_de_champ)
end
def spreadsheet_columns_ods(types_de_champ:, types_de_champ_private:)
spreadsheet_columns(types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private)
def spreadsheet_columns_ods(types_de_champ:)
spreadsheet_columns(types_de_champ: types_de_champ)
end
def spreadsheet_columns(with_etablissement: false, types_de_champ:, types_de_champ_private:)
def spreadsheet_columns(with_etablissement: false, types_de_champ:)
columns = [
['ID', id.to_s],
['Email', user_email_for(:display)]
@ -843,26 +843,12 @@ class Dossier < ApplicationRecord
columns << ['Groupe instructeur', groupe_instructeur.label]
end
columns + champs_for_export(types_de_champ) + champs_private_for_export(types_de_champ_private)
columns + champs_for_export(types_de_champ)
end
def champs_for_export(types_de_champ)
# Index values by stable_id
values = champs.reject(&:exclude_from_export?).reduce({}) do |champs, champ|
champs[champ.stable_id] = champ.for_export
champs
end
# Get all the champs values for the types de champ in the final list.
# Dossier might not have corresponding champ display nil.
types_de_champ.map do |type_de_champ|
[type_de_champ.libelle, values[type_de_champ.stable_id]]
end
end
def champs_private_for_export(types_de_champ)
# Index values by stable_id
values = champs_private.reject(&:exclude_from_export?).reduce({}) do |champs, champ|
values = (champs + champs_private).reject(&:exclude_from_export?).reduce({}) do |champs, champ|
champs[champ.stable_id] = champ.for_export
champs
end

View file

@ -88,14 +88,14 @@ class Procedure < ApplicationRecord
end
def types_de_champ_for_procedure_presentation
explanatory_types_de_champ = [:header_section, :explication].map { |k| TypeDeChamp.type_champs.fetch(k) }
explanatory_types_de_champ = [:header_section, :explication, :repetition].map { |k| TypeDeChamp.type_champs.fetch(k) }
if brouillon?
TypeDeChamp
.joins(:revisions)
.where.not(type_champ: explanatory_types_de_champ)
.where(procedure_revisions: { id: draft_revision_id })
.order(:position)
.order(:private, :position)
else
# 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
@ -118,7 +118,7 @@ class Procedure < ApplicationRecord
TypeDeChamp
.joins(:revision_types_de_champ)
.where(revision_types_de_champ: { id: recents_prtdc })
.order(:position, 'revision_types_de_champ.revision_id': :desc)
.order(:private, :position, 'revision_types_de_champ.revision_id': :desc)
end
end
@ -146,14 +146,6 @@ class Procedure < ApplicationRecord
end
end
def types_de_champ_for_export
types_de_champ.reject(&:exclude_from_export?)
end
def types_de_champ_private_for_export
types_de_champ_private.reject(&:exclude_from_export?)
end
has_many :administrateurs_procedures
has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! }
has_many :groupe_instructeurs, dependent: :destroy

View file

@ -67,8 +67,8 @@ class ProcedurePresentation < ApplicationRecord
end
fields.concat procedure.types_de_champ_for_procedure_presentation
.pluck(:libelle, :stable_id)
.map { |(libelle, stable_id)| field_hash(libelle, TYPE_DE_CHAMP, stable_id.to_s) }
.pluck(:libelle, :private, :stable_id)
.map { |(libelle, is_private, stable_id)| field_hash(libelle, is_private ? TYPE_DE_CHAMP_PRIVATE : TYPE_DE_CHAMP, stable_id.to_s) }
fields
end

View file

@ -84,11 +84,10 @@ class ProcedureExportService
end
def spreadsheet_columns(format)
types_de_champ = @procedure.types_de_champ_for_export
types_de_champ_private = @procedure.types_de_champ_private_for_export
types_de_champ = @procedure.types_de_champ_for_procedure_presentation.to_a
Proc.new do |instance|
instance.send(:"spreadsheet_columns_#{format}", types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private)
instance.send(:"spreadsheet_columns_#{format}", types_de_champ: types_de_champ)
end
end
end

View file

@ -1364,8 +1364,8 @@ describe Dossier do
it "should have champs from all revisions" do
expect(dossier.types_de_champ.map(&:libelle)).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Yes/no", explication_type_de_champ.libelle])
expect(dossier_second_revision.types_de_champ.map(&:libelle)).to eq([datetime_type_de_champ.libelle, "Updated yes/no", explication_type_de_champ.libelle, "New text field"])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export).map { |(libelle)| libelle }).to eq([datetime_type_de_champ.libelle, "Updated yes/no", "New text field"])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export)).to eq(dossier_second_revision.champs_for_export(dossier_second_revision.procedure.types_de_champ_for_export))
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_procedure_presentation).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Updated yes/no", "New text field"])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_procedure_presentation)).to eq(dossier_second_revision.champs_for_export(dossier_second_revision.procedure.types_de_champ_for_procedure_presentation))
end
end
@ -1373,7 +1373,7 @@ describe Dossier do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_explication) }
it "should not contain non-exportable types de champ" do
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_procedure_presentation).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle])
end
end
end
@ -1451,6 +1451,6 @@ describe Dossier do
describe "#spreadsheet_columns" do
let(:dossier) { create(:dossier) }
it { expect(dossier.spreadsheet_columns(types_de_champ: [], types_de_champ_private: [])).to include(["État du dossier", "Brouillon"]) }
it { expect(dossier.spreadsheet_columns(types_de_champ: [])).to include(["État du dossier", "Brouillon"]) }
end
end

View file

@ -56,7 +56,7 @@ describe ProcedureRevision do
revision.reload
expect(revision.types_de_champ.index(type_de_champ)).to eq(2)
expect(revision.procedure.types_de_champ.index(type_de_champ)).to eq(2)
expect(revision.procedure.types_de_champ_for_export.index(type_de_champ)).to eq(2)
expect(revision.procedure.types_de_champ_for_procedure_presentation.index(type_de_champ)).to eq(2)
end
it 'move up' do
@ -66,7 +66,7 @@ describe ProcedureRevision do
revision.reload
expect(revision.types_de_champ.index(last_type_de_champ)).to eq(0)
expect(revision.procedure.types_de_champ.index(last_type_de_champ)).to eq(0)
expect(revision.procedure.types_de_champ_for_export.index(last_type_de_champ)).to eq(0)
expect(revision.procedure.types_de_champ_for_procedure_presentation.index(last_type_de_champ)).to eq(0)
end
context 'repetition' do