From ee1be7154d47a28c4fac5acd0cd5f22abf95743a Mon Sep 17 00:00:00 2001 From: barbara oliveira Date: Tue, 20 Jan 2026 10:54:44 +0100 Subject: [PATCH 1/3] [NYC]fix brocken file backoffice display --- app/decorators/decidim/attachment_patch.rb | 26 ++++++++++++++++++++++ config/application.rb | 14 ++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 app/decorators/decidim/attachment_patch.rb diff --git a/app/decorators/decidim/attachment_patch.rb b/app/decorators/decidim/attachment_patch.rb new file mode 100644 index 0000000..dde3a6c --- /dev/null +++ b/app/decorators/decidim/attachment_patch.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# Patch pour corriger la méthode file_type dans Decidim::Attachment +# afin d'afficher le bon type MIME sans les query params AWS S3. + +Rails.logger.info "✅ Patch chargé : Decidim::Attachment#file_type" + +Decidim::Attachment.class_eval do + def file_type + # Si le fichier est attaché via ActiveStorage + if file.attached? && file.respond_to?(:blob) + return file.blob.content_type.split("/").last.upcase rescue "UNKNOWN" + end + + # Fallback : si la colonne content_type est présente + if respond_to?(:content_type) && content_type.present? + return content_type.split("/").last.upcase + end + + # Sinon on renvoie une valeur neutre + "UNKNOWN" + rescue => e + Rails.logger.warn("[Attachment#file_type patch] #{e.class}: #{e.message}") + "UNKNOWN" + end +end diff --git a/config/application.rb b/config/application.rb index 40d45c3..8c74ba6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -20,6 +20,10 @@ class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 6.1 + # Empêche Zeitwerk d'autoload le dossier decorators + config.autoload_paths -= Dir[Rails.root.join("app/decorators")] + config.eager_load_paths -= Dir[Rails.root.join("app/decorators")] + # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files @@ -33,5 +37,15 @@ class Application < Rails::Application require "extends/controllers/decidim/devise/omniauth_registrations_controller_extends" require "extends/controllers/decidim/errors_controller_extends" end + # --- FORCE LE CHARGEMENT DES DÉCORATEURS --- + # Chargement après initialisation complète de Rails + config.after_initialize do + decorators_path = Rails.root.join("app/decorators/**/*.rb") + Dir[decorators_path].each do |decorator| + puts "💡 Chargement manuel du décorateur : #{File.basename(decorator)}" + require decorator + end + end + end end From 10c046520c27d7a6a8657e508886175df1b646c6 Mon Sep 17 00:00:00 2001 From: barbara oliveira Date: Tue, 20 Jan 2026 15:27:01 +0100 Subject: [PATCH 2/3] lint code --- app/decorators/decidim/attachment_patch.rb | 15 ++++----------- config/application.rb | 7 +++---- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/app/decorators/decidim/attachment_patch.rb b/app/decorators/decidim/attachment_patch.rb index dde3a6c..1763109 100644 --- a/app/decorators/decidim/attachment_patch.rb +++ b/app/decorators/decidim/attachment_patch.rb @@ -3,23 +3,16 @@ # Patch pour corriger la méthode file_type dans Decidim::Attachment # afin d'afficher le bon type MIME sans les query params AWS S3. -Rails.logger.info "✅ Patch chargé : Decidim::Attachment#file_type" +Rails.logger.info "Patch chargé : Decidim::Attachment#file_type" Decidim::Attachment.class_eval do def file_type - # Si le fichier est attaché via ActiveStorage - if file.attached? && file.respond_to?(:blob) - return file.blob.content_type.split("/").last.upcase rescue "UNKNOWN" - end + return file.blob.content_type.split("/").last.upcase if file.attached? && file.respond_to?(:blob) - # Fallback : si la colonne content_type est présente - if respond_to?(:content_type) && content_type.present? - return content_type.split("/").last.upcase - end + return content_type.split("/").last.upcase if respond_to?(:content_type) && content_type.present? - # Sinon on renvoie une valeur neutre "UNKNOWN" - rescue => e + rescue StandardError => e Rails.logger.warn("[Attachment#file_type patch] #{e.class}: #{e.message}") "UNKNOWN" end diff --git a/config/application.rb b/config/application.rb index 8c74ba6..bf20334 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,8 +21,8 @@ class Application < Rails::Application config.load_defaults 6.1 # Empêche Zeitwerk d'autoload le dossier decorators - config.autoload_paths -= Dir[Rails.root.join("app/decorators")] - config.eager_load_paths -= Dir[Rails.root.join("app/decorators")] + config.autoload_paths -= Rails.root.glob("app/decorators") + config.eager_load_paths -= Rails.root.glob("app/decorators") # Configuration for the application, engines, and railties goes here. # @@ -42,10 +42,9 @@ class Application < Rails::Application config.after_initialize do decorators_path = Rails.root.join("app/decorators/**/*.rb") Dir[decorators_path].each do |decorator| - puts "💡 Chargement manuel du décorateur : #{File.basename(decorator)}" + Rails.logger.info "💡 Chargement manuel du décorateur : #{File.basename(decorator)}" require decorator end end - end end From e0071d08f2d8bd39d78d983f26f0f29cc015437a Mon Sep 17 00:00:00 2001 From: barbara oliveira Date: Tue, 20 Jan 2026 15:36:59 +0100 Subject: [PATCH 3/3] fix CI --- app/decorators/decidim/attachment_patch.rb | 7 +++++++ lib/tasks/{decidim-app.rake => decidim_app.rake} | 0 2 files changed, 7 insertions(+) rename lib/tasks/{decidim-app.rake => decidim_app.rake} (100%) diff --git a/app/decorators/decidim/attachment_patch.rb b/app/decorators/decidim/attachment_patch.rb index 1763109..8bfb59d 100644 --- a/app/decorators/decidim/attachment_patch.rb +++ b/app/decorators/decidim/attachment_patch.rb @@ -3,6 +3,13 @@ # Patch pour corriger la méthode file_type dans Decidim::Attachment # afin d'afficher le bon type MIME sans les query params AWS S3. +# app/decorators/decidim/attachment_patch.rb + +module Decidim + module AttachmentPatch + end +end + Rails.logger.info "Patch chargé : Decidim::Attachment#file_type" Decidim::Attachment.class_eval do diff --git a/lib/tasks/decidim-app.rake b/lib/tasks/decidim_app.rake similarity index 100% rename from lib/tasks/decidim-app.rake rename to lib/tasks/decidim_app.rake