demarches-normaliennes/app/models/export_template.rb

89 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2024-03-02 22:13:09 +01:00
class ExportTemplate < ApplicationRecord
include TagsSubstitutionConcern
self.ignored_columns += ["content"]
2024-03-02 22:13:09 +01:00
belongs_to :groupe_instructeur
has_one :procedure, through: :groupe_instructeur
2024-03-12 17:28:43 +01:00
has_many :exports, dependent: :nullify
2024-03-03 10:19:02 +01:00
enum kind: { zip: 'zip', csv: 'csv', xlsx: 'xlsx', ods: 'ods' }, _prefix: :template
2024-03-02 22:13:09 +01:00
attribute :dossier_folder, :export_item
attribute :export_pdf, :export_item
attribute :pjs, :export_item, array: true
2024-03-02 22:13:09 +01:00
attribute :exported_columns, :exported_column, array: true
before_validation :ensure_pjs_are_legit
2024-03-02 22:13:09 +01:00
validates_with ExportTemplateValidator
2024-03-03 10:03:12 +01:00
DOSSIER_STATE = Dossier.states.fetch(:en_construction)
2024-03-03 10:03:12 +01:00
# when a pj has been added to a revision, it will not be present in the previous pjs
# a default value is provided.
def pj(tdc)
pjs.find { _1.stable_id == tdc.stable_id } || ExportItem.default_pj(tdc)
2024-03-02 22:13:09 +01:00
end
def self.default(name: nil, kind: 'zip', groupe_instructeur:)
# TODO: remove default values for tabular export
dossier_folder = ExportItem.default(prefix: 'dossier')
export_pdf = ExportItem.default(prefix: 'export')
pjs = groupe_instructeur.procedure.exportables_pieces_jointes.map { |tdc| ExportItem.default_pj(tdc) }
2024-03-02 22:13:09 +01:00
new(name:, kind:, groupe_instructeur:, dossier_folder:, export_pdf:, pjs:)
2024-03-02 22:13:09 +01:00
end
def tabular?
kind != 'zip'
end
def tags
2024-03-03 11:13:07 +01:00
tags_categorized.slice(:individual, :etablissement, :dossier).values.flatten
end
def pj_tags
tags.push(
2024-03-19 11:03:29 +01:00
libelle: 'nom original du fichier',
id: 'original-filename'
)
2024-03-20 16:34:46 +01:00
end
def attachment_path(dossier, attachment, index: 0, row_index: nil, champ: nil)
file_path = if attachment.name == 'pdf_export_for_instructeur'
export_pdf.path(dossier, attachment:)
elsif attachment.record_type == 'Champ' && pj(champ.type_de_champ).enabled?
pj(champ.type_de_champ).path(dossier, attachment:, index:, row_index:)
2024-03-02 22:13:09 +01:00
else
nil
2024-03-02 22:13:09 +01:00
end
File.join(dossier_folder.path(dossier), file_path) if file_path.present?
2024-03-02 22:13:09 +01:00
end
def dossier_exported_columns = exported_columns.filter { _1.column.dossier_column? }
def columns_for_stable_id(stable_id)
exported_columns
.filter { _1.column.champ_column? }
.filter { _1.column.stable_id == stable_id }
end
def in_export?(exported_column)
@template_exported_columns ||= exported_columns.map(&:column)
@template_exported_columns.include?(exported_column.column)
end
private
2024-03-02 22:13:09 +01:00
def ensure_pjs_are_legit
legitimate_pj_stable_ids = procedure.exportables_pieces_jointes_for_all_versions.map(&:stable_id)
2024-03-02 22:13:09 +01:00
self.pjs = pjs.filter { _1.stable_id.in?(legitimate_pj_stable_ids) }
2024-03-02 22:13:09 +01:00
end
end