demarches-normaliennes/app/models/export_template.rb

69 lines
2 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" }, _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
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:)
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 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
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