From e9ba7700d4fc24f261356eafb1938eefaaad2dce Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 14 Aug 2019 13:00:52 +0100 Subject: [PATCH] Remove commentaire file uploader --- app/models/commentaire.rb | 12 +--- app/uploaders/commentaire_file_uploader.rb | 23 ------- .../2019_05_29_migrate_commentaire_pj_spec.rb | 62 ------------------- 3 files changed, 2 insertions(+), 95 deletions(-) delete mode 100644 app/uploaders/commentaire_file_uploader.rb delete mode 100644 spec/lib/tasks/2019_05_29_migrate_commentaire_pj_spec.rb diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 376296064..4ff31f768 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -4,7 +4,6 @@ class Commentaire < ApplicationRecord belongs_to :user belongs_to :instructeur - mount_uploader :file, CommentaireFileUploader validate :messagerie_available?, on: :create has_one_attached :piece_jointe @@ -48,15 +47,8 @@ class Commentaire < ApplicationRecord end def file_url - if piece_jointe.attached? - if piece_jointe.virus_scanner.safe? - Rails.application.routes.url_helpers.url_for(piece_jointe) - end - elsif Rails.application.secrets.fog[:enabled] - RemoteDownloader.new(file.path).url - elsif file&.url - # FIXME: this is horrible but used only in dev and will be removed after migration - File.join(LOCAL_DOWNLOAD_URL, file.url) + if piece_jointe.attached? && piece_jointe.virus_scanner.safe? + Rails.application.routes.url_helpers.url_for(piece_jointe) end end diff --git a/app/uploaders/commentaire_file_uploader.rb b/app/uploaders/commentaire_file_uploader.rb deleted file mode 100644 index 93131fb94..000000000 --- a/app/uploaders/commentaire_file_uploader.rb +++ /dev/null @@ -1,23 +0,0 @@ -class CommentaireFileUploader < BaseUploader - def root - Rails.root.join("public") - end - - if Rails.application.secrets.fog[:enabled] - storage :fog - else - storage :file - end - - def store_dir - "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" - end - - def extension_whitelist - ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp', 'jpg', 'jpeg', 'png', 'zip', 'txt'] - end - - def accept_extension_list - extension_whitelist.map { |e| ".#{e}" }.join(",") - end -end diff --git a/spec/lib/tasks/2019_05_29_migrate_commentaire_pj_spec.rb b/spec/lib/tasks/2019_05_29_migrate_commentaire_pj_spec.rb deleted file mode 100644 index b05875879..000000000 --- a/spec/lib/tasks/2019_05_29_migrate_commentaire_pj_spec.rb +++ /dev/null @@ -1,62 +0,0 @@ -describe '2019_05_29_migrate_commentaire_pj.rake' do - let(:rake_task) { Rake::Task['2019_05_29_migrate_commentaire_pj:run'] } - - let(:commentaires) do - [ - create(:commentaire), - create(:commentaire, :with_file), - create(:commentaire, :with_file) - ] - end - - before do - commentaires.each do |commentaire| - if commentaire.file.present? - stub_request(:get, commentaire.file_url) - .to_return(status: 200, body: File.read(commentaire.file.path)) - end - end - end - - after do - ENV['LIMIT'] = nil - rake_task.reenable - end - - it 'should migrate pj' do - comment_updated_at = Commentaire.last.updated_at - dossier_updated_at = Commentaire.last.dossier.updated_at - expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, false, false]) - rake_task.invoke - expect(Commentaire.where(file: nil).count).to eq(1) - expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, true, true]) - expect(Commentaire.last.updated_at).to eq(comment_updated_at) - expect(Commentaire.last.dossier.updated_at).to eq(dossier_updated_at) - end - - it 'should migrate pj within limit' do - expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, false, false]) - ENV['LIMIT'] = '1' - rake_task.invoke - expect(Commentaire.where(file: nil).count).to eq(1) - expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, true, false]) - end - - context 'when a commentaire’s dossier is hidden' do - let(:hidden_dossier) { create(:dossier, :en_construction, :hidden) } - let(:commentaire) { create(:commentaire, :with_file, dossier: hidden_dossier) } - let(:commentaires) { [commentaire] } - - it 'should migrate the pj' do - comment_updated_at = commentaire.reload.updated_at - dossier_updated_at = hidden_dossier.reload.updated_at - - rake_task.invoke - commentaires.each(&:reload) - - expect(commentaire.piece_jointe.attached?).to be true - expect(commentaire.updated_at).to eq(comment_updated_at) - expect(hidden_dossier.updated_at).to eq(dossier_updated_at) - end - end -end