2017-06-08 14:16:48 +02:00
|
|
|
class AttestationTemplate < ApplicationRecord
|
2019-09-17 10:16:52 +02:00
|
|
|
self.ignored_columns = ['logo', 'signature', 'logo_secure_token', 'signature_secure_token']
|
2019-08-28 16:14:20 +02:00
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2018-01-02 15:33:11 +01:00
|
|
|
include TagsSubstitutionConcern
|
2017-06-08 14:16:48 +02:00
|
|
|
|
|
|
|
belongs_to :procedure
|
|
|
|
|
2019-08-28 13:11:58 +02:00
|
|
|
has_one_attached :logo
|
2019-08-27 17:42:52 +02:00
|
|
|
has_one_attached :logo_active_storage
|
2019-08-28 13:11:58 +02:00
|
|
|
has_one_attached :signature
|
2019-08-27 17:42:52 +02:00
|
|
|
has_one_attached :signature_active_storage
|
|
|
|
|
2017-06-27 13:07:06 +02:00
|
|
|
validates :footer, length: { maximum: 190 }
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2018-08-28 14:10:55 +02:00
|
|
|
DOSSIER_STATE = Dossier.states.fetch(:accepte)
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
def attestation_for(dossier)
|
2019-08-27 17:42:52 +02:00
|
|
|
attestation = Attestation.new(title: replace_tags(title, dossier))
|
2019-08-28 13:11:58 +02:00
|
|
|
attestation.pdf.attach(
|
2019-08-27 17:42:52 +02:00
|
|
|
io: build_pdf(dossier),
|
|
|
|
filename: "attestation-dossier-#{dossier.id}.pdf",
|
|
|
|
content_type: 'application/pdf',
|
|
|
|
# we don't want to run virus scanner on this file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
attestation
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2018-04-06 13:09:37 +02:00
|
|
|
def unspecified_champs_for_dossier(dossier)
|
|
|
|
all_champs_with_libelle_index = (dossier.champs + dossier.champs_private)
|
|
|
|
.reduce({}) do |acc, champ|
|
|
|
|
acc[champ.libelle] = champ
|
|
|
|
acc
|
|
|
|
end
|
|
|
|
|
|
|
|
used_tags.map do |used_tag|
|
|
|
|
corresponding_champ = all_champs_with_libelle_index[used_tag]
|
|
|
|
|
2018-04-06 19:22:42 +02:00
|
|
|
if corresponding_champ && corresponding_champ.value.blank?
|
|
|
|
corresponding_champ
|
|
|
|
end
|
2018-04-06 13:09:37 +02:00
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
def dup
|
2019-08-27 17:42:52 +02:00
|
|
|
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
|
|
|
|
|
2019-08-28 13:11:58 +02:00
|
|
|
if logo.attached?
|
|
|
|
attestation_template.logo.attach(
|
|
|
|
io: StringIO.new(logo.download),
|
|
|
|
filename: logo.filename.to_s,
|
|
|
|
content_type: logo.content_type,
|
|
|
|
# we don't want to run virus scanner on duplicated file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
elsif logo_active_storage.attached?
|
|
|
|
attestation_template.logo.attach(
|
2019-08-27 17:42:52 +02:00
|
|
|
io: StringIO.new(logo_active_storage.download),
|
2019-08-28 13:11:58 +02:00
|
|
|
filename: logo_active_storage.filename.to_s,
|
2019-08-27 17:42:52 +02:00
|
|
|
content_type: logo_active_storage.content_type,
|
|
|
|
# we don't want to run virus scanner on duplicated file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-08-28 13:11:58 +02:00
|
|
|
if signature.attached?
|
|
|
|
attestation_template.signature.attach(
|
|
|
|
io: StringIO.new(signature.download),
|
|
|
|
filename: signature.filename.to_s,
|
|
|
|
content_type: signature.content_type,
|
|
|
|
# we don't want to run virus scanner on duplicated file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
elsif signature_active_storage.attached?
|
|
|
|
attestation_template.signature.attach(
|
2019-08-27 17:42:52 +02:00
|
|
|
io: StringIO.new(signature_active_storage.download),
|
2019-08-28 13:11:58 +02:00
|
|
|
filename: signature_active_storage.filename.to_s,
|
2019-08-27 17:42:52 +02:00
|
|
|
content_type: signature_active_storage.content_type,
|
|
|
|
# we don't want to run virus scanner on duplicated file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
attestation_template
|
|
|
|
end
|
|
|
|
|
|
|
|
def logo?
|
2019-08-28 13:11:58 +02:00
|
|
|
logo.attached? || logo_active_storage.attached?
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def signature?
|
2019-08-28 13:11:58 +02:00
|
|
|
signature.attached? || signature_active_storage.attached?
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def logo_url
|
2019-08-28 13:11:58 +02:00
|
|
|
if logo.attached?
|
|
|
|
Rails.application.routes.url_helpers.url_for(logo)
|
|
|
|
elsif logo_active_storage.attached?
|
2019-08-27 17:42:52 +02:00
|
|
|
Rails.application.routes.url_helpers.url_for(logo_active_storage)
|
|
|
|
end
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
def signature_url
|
2019-08-28 13:11:58 +02:00
|
|
|
if signature.attached?
|
|
|
|
Rails.application.routes.url_helpers.url_for(signature)
|
|
|
|
elsif signature_active_storage.attached?
|
2019-08-27 17:42:52 +02:00
|
|
|
Rails.application.routes.url_helpers.url_for(signature_active_storage)
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
def proxy_logo
|
2019-08-28 13:11:58 +02:00
|
|
|
if logo.attached?
|
2019-08-27 17:42:52 +02:00
|
|
|
logo
|
2019-08-28 13:11:58 +02:00
|
|
|
elsif logo_active_storage.attached?
|
|
|
|
logo_active_storage
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
def proxy_signature
|
2019-08-28 13:11:58 +02:00
|
|
|
if signature.attached?
|
2019-08-27 17:42:52 +02:00
|
|
|
signature
|
2019-08-28 13:11:58 +02:00
|
|
|
elsif signature_active_storage.attached?
|
|
|
|
signature_active_storage
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|
|
|
|
|
2019-08-28 16:33:51 +02:00
|
|
|
def title_for_dossier(dossier)
|
|
|
|
replace_tags(title, dossier)
|
|
|
|
end
|
|
|
|
|
|
|
|
def body_for_dossier(dossier)
|
|
|
|
replace_tags(body, dossier)
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
private
|
|
|
|
|
2018-04-06 13:09:37 +02:00
|
|
|
def used_tags
|
|
|
|
delimiters_regex = /--(?<capture>((?!--).)*)--/
|
|
|
|
|
2018-04-06 16:21:48 +02:00
|
|
|
# We can't use flat_map as scan will return 3 levels of array,
|
|
|
|
# using flat_map would give us 2, whereas flatten will
|
|
|
|
# give us 1, which is what we want
|
2018-04-06 13:09:37 +02:00
|
|
|
[title, body]
|
|
|
|
.map { |str| str.scan(delimiters_regex) }
|
|
|
|
.flatten
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
def build_pdf(dossier)
|
|
|
|
action_view = ActionView::Base.new(ActionController::Base.view_paths,
|
2019-08-27 17:42:52 +02:00
|
|
|
logo: proxy_logo,
|
2019-08-28 16:33:51 +02:00
|
|
|
title: title_for_dossier(dossier),
|
|
|
|
body: body_for_dossier(dossier),
|
2019-08-27 17:42:52 +02:00
|
|
|
signature: proxy_signature,
|
2017-06-08 14:04:47 +02:00
|
|
|
footer: footer,
|
2018-10-25 15:11:12 +02:00
|
|
|
created_at: Time.zone.now)
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
attestation_view = action_view.render(file: 'admin/attestation_templates/show', formats: [:pdf])
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
StringIO.new(attestation_view)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|