Merge pull request #4203 from tchak/remove-commentaire-file-uploader
Remove commentaire file uploader
This commit is contained in:
commit
19bf1bebaf
3 changed files with 2 additions and 95 deletions
|
@ -4,7 +4,6 @@ class Commentaire < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :instructeur
|
belongs_to :instructeur
|
||||||
|
|
||||||
mount_uploader :file, CommentaireFileUploader
|
|
||||||
validate :messagerie_available?, on: :create
|
validate :messagerie_available?, on: :create
|
||||||
|
|
||||||
has_one_attached :piece_jointe
|
has_one_attached :piece_jointe
|
||||||
|
@ -48,16 +47,9 @@ class Commentaire < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_url
|
def file_url
|
||||||
if piece_jointe.attached?
|
if piece_jointe.attached? && piece_jointe.virus_scanner.safe?
|
||||||
if piece_jointe.virus_scanner.safe?
|
|
||||||
Rails.application.routes.url_helpers.url_for(piece_jointe)
|
Rails.application.routes.url_helpers.url_for(piece_jointe)
|
||||||
end
|
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)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
Loading…
Add table
Reference in a new issue