Migrate procedure logo to active_storage

This commit is contained in:
Paul Chavard 2019-08-20 11:28:07 +02:00
parent ce97b8c359
commit cc35a04e26
14 changed files with 85 additions and 40 deletions

View file

@ -249,8 +249,11 @@ class Admin::ProceduresController < AdminController
end
def delete_logo
@procedure.remove_logo!
@procedure.save
if @procedure.logo
@procedure.remove_logo!
@procedure.save
end
@procedure.logo_active_storage.purge_later
flash.notice = 'le logo a bien été supprimé'
redirect_to edit_admin_procedure_path(@procedure)
@ -278,10 +281,12 @@ class Admin::ProceduresController < AdminController
def procedure_params
editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :cadre_juridique, :deliberation, :notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on, :monavis_embed]
if @procedure&.locked?
permited_params = if @procedure&.locked?
params.require(:procedure).permit(*editable_params)
else
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :ask_birthday, :path)
end
permited_params[:logo_active_storage] = permited_params.delete(:logo)
permited_params
end
end

View file

@ -7,7 +7,7 @@ module NewAdministrateur
dossier = Dossier.new(id: '1', procedure: procedure)
@dossier = dossier
@logo_url = procedure.logo.url
@logo_url = procedure.logo_url
@service = procedure.service
@rendered_template = sanitize(mail_template.body)
@actions = mail_template.actions_for_dossier(dossier)

View file

@ -20,20 +20,6 @@ module ProcedureHelper
t(action, scope: [:modal, :publish, key])
end
def logo_img(procedure)
logo = procedure.logo
if logo.blank?
ActionController::Base.helpers.image_url("marianne.svg")
else
if Rails.application.secrets.fog[:enabled]
RemoteDownloader.new(logo.filename).url
else
LocalDownloader.new(logo.path, 'logo').url
end
end
end
def types_de_champ_data(procedure)
{
isAnnotation: false,

View file

@ -9,8 +9,13 @@ class ApplicationMailer < ActionMailer::Base
return nil if !procedure.logo?
begin
logo_filename = procedure.logo.filename
attachments.inline[logo_filename] = procedure.logo.read
if procedure.logo_active_storage.attached?
logo_filename = procedure.logo_active_storage.filename
attachments.inline[logo_filename] = procedure.logo_active_storage.download
else
logo_filename = procedure.logo.filename
attachments.inline[logo_filename] = procedure.logo.read
end
attachments[logo_filename].url
rescue StandardError => e

View file

@ -25,6 +25,7 @@ class Procedure < ApplicationRecord
has_one :refused_mail, class_name: "Mails::RefusedMail", dependent: :destroy
has_one :without_continuation_mail, class_name: "Mails::WithoutContinuationMail", dependent: :destroy
has_one_attached :logo_active_storage
has_one_attached :notice
has_one_attached :deliberation
@ -215,7 +216,9 @@ class Procedure < ApplicationRecord
procedure.archived_at = nil
procedure.published_at = nil
procedure.logo_secure_token = nil
procedure.remote_logo_url = self.logo_url
if logo.present?
procedure.remote_logo_url = self.logo_url
end
procedure.lien_notice = nil
if is_different_admin || from_library
@ -252,6 +255,7 @@ class Procedure < ApplicationRecord
if original.is_a?(TypeDeChamp)
clone_attachment(:piece_justificative_template, original, kopy)
elsif original.is_a?(Procedure)
clone_attachment(:logo_active_storage, original, kopy)
clone_attachment(:notice, original, kopy)
clone_attachment(:deliberation, original, kopy)
end
@ -452,6 +456,24 @@ class Procedure < ApplicationRecord
end
end
def logo?
logo.present? || logo_active_storage.attached?
end
def logo_url
if !logo?
ActionController::Base.helpers.image_url("marianne.svg")
elsif logo_active_storage.attached?
Rails.application.routes.url_helpers.url_for(logo_active_storage)
else
if Rails.application.secrets.fog[:enabled]
RemoteDownloader.new(logo.filename).url
else
LocalDownloader.new(logo.path, 'logo').url
end
end
end
private
def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index)

View file

@ -77,13 +77,16 @@
.row
.col-md-6
%h4 Logo de la démarche
- if @procedure.logo.present?
= image_tag logo_img(@procedure), { style: 'height: 40px; display: inline; margin-right: 6px;', id: 'preview_procedure_logo' }
- if @procedure.logo?
= image_tag @procedure.logo_url, { style: 'height: 40px; display: inline; margin-right: 6px;', id: 'preview_procedure_logo' }
\-
- if @procedure.persisted?
= link_to 'supprimer', delete_logo_admin_procedure_path(@procedure), method: :delete
= f.file_field :logo, accept: 'image/png, image/jpg, image/jpeg', style: 'display: inline'
= f.file_field :logo,
direct_upload: true,
accept: 'image/png, image/jpg, image/jpeg',
style: 'display: inline'
%div{ style: 'margin-top: 5px;' }
%i

View file

@ -4,7 +4,7 @@
.sub-header
.container.flex
.procedure-logo{ style: @procedure.logo.present? ? "background-image: url(#{@procedure.logo.url})" : nil,
.procedure-logo{ style: @procedure.logo? ? "background-image: url(#{@procedure.logo_url})" : nil,
role: 'img', 'aria-label': "logo de la démarche #{@procedure.libelle}" }
.procedure-header

View file

@ -1,5 +1,5 @@
.procedure-logos
= image_tag logo_img(procedure)
= image_tag procedure.logo_url
- if procedure.euro_flag
= image_tag "flag_of_europe.svg"
%h2.procedure-title

View file

@ -1,7 +1,7 @@
#first-block
.en-cours
- if @procedure.logo.present?
= image_tag logo_img(@procedure), style: 'width: 30px;'
- if @procedure.logo?
= image_tag @procedure.logo_url, style: 'width: 30px;'
%b
= @procedure.libelle

View file

@ -8,7 +8,7 @@
= image_tag('drapeau_europe.png')
#logo_procedure.flag
= image_tag(logo_img(dossier.procedure))
= image_tag(dossier.procedure.logo_url)
%h2#titre-procedure.text-info
= @dossier.procedure.libelle

View file

@ -0,0 +1,33 @@
namespace :'2019_08_20_migrate_procedure_logo' do
task run: :environment do
procedures = Procedure.where
.not(logo: nil)
.left_joins(:logo_new_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
procedures.limit!(limit.to_i)
end
progress = ProgressReport.new(procedures.count)
procedures.find_each do |procedure|
if procedure.logo.present?
uri = URI.parse(URI.escape(procedure.logo_url))
response = Typhoeus.get(uri)
if response.success?
filename = procedure.logo.filename || procedure.logo_identifier
procedure.logo_new.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: procedure.logo.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
end
progress.inc
end
progress.finish
end
end

View file

@ -14,7 +14,7 @@ describe NewAdministrateur::MailTemplatesController, type: :controller do
it { expect(response).to have_http_status(:ok) }
it 'displays the procedure logo' do
expect(response.body).to have_css("img[src*='#{procedure.logo.filename}']")
expect(response.body).to have_css("img[src*='#{procedure.logo_url}']")
end
it 'displays the action buttons' do

View file

@ -43,7 +43,7 @@ FactoryBot.define do
end
trait :with_logo do
logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
logo_active_storage { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') }
end
trait :with_path do

View file

@ -1,9 +0,0 @@
RSpec.describe ProcedureHelper, type: :helper do
let(:procedure) { create(:procedure) }
describe ".logo_img" do
subject { logo_img(procedure) }
it { is_expected.to match(/#{ActionController::Base.helpers.image_url("marianne.svg")}/) }
end
end