Skip to content

Commit

Permalink
refactor(attestation_template): make code more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
krichtof committed Sep 29, 2023
1 parent da06295 commit 6c4d040
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions app/models/attestation_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,27 @@ def signature_url
end

def render_attributes_for(params = {})
dossier = params.fetch(:dossier, false)

{
attributes = {
created_at: Time.zone.now,
title: dossier ? replace_tags(title, dossier) : params.fetch(:title, title),
body: dossier ? replace_tags(body, dossier) : params.fetch(:body, body),
footer: params.fetch(:footer, footer),
logo: params.fetch(:logo, logo.attached? ? logo : nil),
signature: signature_to_render(params)
}

dossier = params[:dossier]

if dossier.present?
attributes.merge({
title: replace_tags(title, dossier),
body: replace_tags(body, dossier),
signature: signature_to_render(dossier.groupe_instructeur)
})
else
attributes.merge({
title: params.fetch(:title, title),
body: params.fetch(:body, body),
signature: signature_to_render(params[:groupe_instructeur])
})
end
end

def logo_checksum
Expand All @@ -90,10 +101,8 @@ def signature_filename

private

def signature_to_render(params)
dossier = params.fetch(:dossier, false)
groupe_instructeur = dossier ? dossier.groupe_instructeur : params[:groupe_instructeur]
if groupe_instructeur && groupe_instructeur.signature.attached?
def signature_to_render(groupe_instructeur)
if groupe_instructeur&.signature&.attached?
groupe_instructeur.signature
else
signature
Expand Down

0 comments on commit 6c4d040

Please sign in to comment.