2017-06-08 14:16:48 +02:00
|
|
|
class AttestationTemplate < ApplicationRecord
|
|
|
|
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
|
|
|
|
|
2017-10-19 15:07:18 +02:00
|
|
|
mount_uploader :logo, AttestationTemplateLogoUploader
|
|
|
|
mount_uploader :signature, AttestationTemplateSignatureUploader
|
2017-06-08 14:16:48 +02:00
|
|
|
|
|
|
|
validate :logo_signature_file_size
|
2017-06-27 13:07:06 +02:00
|
|
|
validates :footer, length: { maximum: 190 }
|
2017-06-08 14:16:48 +02:00
|
|
|
|
|
|
|
FILE_MAX_SIZE_IN_MB = 0.5
|
2018-01-10 15:03:56 +01:00
|
|
|
DOSSIER_STATE = 'accepte'
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
def attestation_for(dossier)
|
|
|
|
Attestation.new(title: replace_tags(title, dossier), pdf: build_pdf(dossier))
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
def dup
|
|
|
|
result = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
|
|
|
|
|
|
|
|
if logo.present?
|
|
|
|
CopyCarrierwaveFile::CopyFileService.new(self, result, :logo).set_file
|
|
|
|
end
|
|
|
|
|
|
|
|
if signature.present?
|
|
|
|
CopyCarrierwaveFile::CopyFileService.new(self, result, :signature).set_file
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def logo_signature_file_size
|
|
|
|
%i[logo signature]
|
|
|
|
.select { |file_name| send(file_name).present? }
|
|
|
|
.each { |file_name| file_size_check(file_name) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_size_check(file_name)
|
|
|
|
if send(file_name).file.size.to_f > FILE_MAX_SIZE_IN_MB.megabyte.to_f
|
|
|
|
errors.add(file_name, " : vous ne pouvez pas charger une image de plus de #{number_with_delimiter(FILE_MAX_SIZE_IN_MB, locale: :fr)} Mo")
|
|
|
|
end
|
|
|
|
end
|
2017-06-09 13:28:47 +02:00
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
def build_pdf(dossier)
|
|
|
|
action_view = ActionView::Base.new(ActionController::Base.view_paths,
|
|
|
|
logo: logo,
|
|
|
|
title: replace_tags(title, dossier),
|
|
|
|
body: replace_tags(body, dossier),
|
|
|
|
signature: signature,
|
|
|
|
footer: footer,
|
|
|
|
created_at: Time.now)
|
|
|
|
|
|
|
|
attestation_view = action_view.render(file: 'admin/attestation_templates/show',
|
|
|
|
formats: [:pdf])
|
|
|
|
|
|
|
|
view_to_memory_file(attestation_view)
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_to_memory_file(view)
|
|
|
|
pdf = StringIO.new(view)
|
|
|
|
|
|
|
|
def pdf.original_filename
|
|
|
|
'attestation'
|
|
|
|
end
|
|
|
|
|
|
|
|
pdf
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|