Skip to content

Commit

Permalink
correctif(dolist): utilise des liens vers les logos des procedures pl…
Browse files Browse the repository at this point in the history
…utôt que des attachements.inlined
  • Loading branch information
Martin authored and mfo committed Aug 14, 2023
1 parent 28de1a7 commit a3c6a2c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ gem 'lograge'
gem 'logstash-event'
gem 'mailjet', require: false
gem 'matrix' # needed by prawn and not default in ruby 3.1
gem 'mini_magick'
gem 'net-imap', require: false # See https://github.com/mikel/mail/pull/1439
gem 'net-pop', require: false # same
gem 'net-smtp', require: false # same
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ DEPENDENCIES
matrix
memory_profiler
mina
mini_magick
net-imap
net-pop
net-smtp
Expand Down
14 changes: 0 additions & 14 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,4 @@ class ApplicationMailer < ActionMailer::Base
layout 'mailer'

before_action -> { Sentry.set_tags(mailer: mailer_name, action: action_name) }

# Attach the procedure logo to the email (if any).
# Returns the attachment url.
def attach_logo(procedure)
if procedure.logo.attached?
logo_filename = procedure.logo.filename.to_s
attachments.inline[logo_filename] = procedure.logo.download
attachments[logo_filename].url
end
rescue StandardError => e
# A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ?
Sentry.capture_exception(e, extra: { procedure_id: procedure.id })
nil
end
end
8 changes: 4 additions & 4 deletions app/mailers/dossier_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def notify_new_draft
@dossier = params[:dossier]
I18n.with_locale(@dossier.user_locale) do
@service = @dossier.procedure.service
@logo_url = attach_logo(@dossier.procedure)
@logo_url = @dossier.procedure.email_logo_url
@subject = default_i18n_subject(libelle_demarche: @dossier.procedure.libelle)

mail(to: @dossier.user_email_for(:notification), subject: @subject) do |format|
Expand All @@ -27,7 +27,7 @@ def notify_new_answer
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@service = dossier.procedure.service
@logo_url = attach_logo(dossier.procedure)
@logo_url = @dossier.procedure.email_logo_url
@body = commentaire.body
@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)

Expand All @@ -52,7 +52,7 @@ def notify_pending_correction
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@service = dossier.procedure.service
@logo_url = attach_logo(dossier.procedure)
@logo_url = @dossier.procedure.email_logo_url
@correction = commentaire.dossier_correction

@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)
Expand Down Expand Up @@ -85,7 +85,7 @@ def notify_revert_to_instruction(dossier)
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@service = dossier.procedure.service
@logo_url = attach_logo(dossier.procedure)
@logo_url = @dossier.procedure.email_logo_url
@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)

mail(to: dossier.user_email_for(:notification), subject: @subject) do |format|
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NotificationMailer < ApplicationMailer

def send_notification
@service = @dossier.procedure.service
@logo_url = attach_logo(@dossier.procedure)
@logo_url = @dossier.procedure.email_logo_url
attachments[@attachment[:filename]] = @attachment[:content] if @attachment.present?
I18n.with_locale(@dossier.user_locale) do
mail(subject: @subject, to: @email, template_name: 'send_notification')
Expand Down
12 changes: 11 additions & 1 deletion app/models/procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def revisions_with_pending_dossiers

belongs_to :defaut_groupe_instructeur, class_name: 'GroupeInstructeur', inverse_of: false, optional: true

has_one_attached :logo
has_one_attached :logo do |attachable|
attachable.variant :email, resize_to_limit: [150, 150]
end
has_one_attached :notice
has_one_attached :deliberation

Expand Down Expand Up @@ -633,6 +635,14 @@ def missing_steps
result
end

def email_logo_url
if logo.attached?
logo.variant(:email).processed.blob.url(expires_in: 1.year)
else
ActionController::Base.helpers.image_url(PROCEDURE_DEFAULT_LOGO_SRC)
end
end

def logo_url
if logo.attached?
Rails.application.routes.url_helpers.url_for(logo)
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/new_framework_defaults_7_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
# generate variants to use image processing macros and ruby-vips
# operations. See the upgrading guide for detail on the changes required.
# The `:mini_magick` option is not deprecated; it's fine to keep using it.
Rails.application.config.active_storage.variant_processor = :vips
Rails.application.config.active_storage.variant_processor = :mini_magick

# Enable parameter wrapping for JSON.
# Previously this was set in an initializer. It's fine to keep using that initializer if you've customized it.
Expand Down
14 changes: 14 additions & 0 deletions spec/models/procedure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,20 @@
end
end

describe '#email_logo_url' do
context 'with logo' do
let!(:procedure) { create(:procedure, :with_logo) }
it 'creates a blob synchronously one time' do
expect { procedure.email_logo_url }.to change { ActiveStorage::Blob.count }.by(1)
end

it 'creates the blob only the first time' do
procedure.email_logo_url
expect { procedure.email_logo_url }.not_to change { ActiveStorage::Blob.count }
end
end
end

describe '#average_dossier_weight' do
let(:procedure) { create(:procedure, :published) }

Expand Down

0 comments on commit a3c6a2c

Please sign in to comment.