[fix #306] Possibilité de joindre un document à un avis

This commit is contained in:
clemkeirua 2019-03-01 17:16:56 +01:00 committed by simon lehericey
parent f0bd560fea
commit 0d71120f98
8 changed files with 90 additions and 8 deletions

View file

@ -134,7 +134,7 @@ module Gestionnaires
end end
def avis_params def avis_params
params.require(:avis).permit(:answer) params.require(:avis).permit(:answer, :piece_justificative_file)
end end
def commentaire_params def commentaire_params

View file

@ -1,10 +1,13 @@
class Avis < ApplicationRecord class Avis < ApplicationRecord
include EmailSanitizableConcern include EmailSanitizableConcern
include VirusScanConcern
belongs_to :dossier, touch: true belongs_to :dossier, touch: true
belongs_to :gestionnaire belongs_to :gestionnaire
belongs_to :claimant, class_name: 'Gestionnaire' belongs_to :claimant, class_name: 'Gestionnaire'
has_one_attached :piece_justificative_file
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
validates :claimant, presence: true validates :claimant, presence: true
@ -19,6 +22,9 @@ class Avis < ApplicationRecord
scope :by_latest, -> { order(updated_at: :desc) } scope :by_latest, -> { order(updated_at: :desc) }
scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) } scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) }
after_commit :create_avis_virus_scan
after_initialize { add_virus_scan_on(self.piece_justificative_file) }
# The form allows subtmitting avis requests to several emails at once, # The form allows subtmitting avis requests to several emails at once,
# hence this virtual attribute. # hence this virtual attribute.
attr_accessor :emails attr_accessor :emails
@ -48,4 +54,8 @@ class Avis < ApplicationRecord
self.email = nil self.email = nil
end end
end end
def create_avis_virus_scan
create_virus_scan(self.piece_justificative_file)
end
end end

View file

@ -0,0 +1,21 @@
module VirusScanConcern
extend ActiveSupport::Concern
attr_reader :attachment_attribute
def add_virus_scan_on(piece_justificative)
@attachment_attribute = piece_justificative
end
def virus_scan
VirusScan.find_by(blob_key: self.attachment_attribute.blob.key)
end
def create_virus_scan(piece_justificative)
if piece_justificative&.attachment&.blob.present?
VirusScan.find_or_create_by!(blob_key: piece_justificative.blob.key) do |virus_scan|
virus_scan.status = VirusScan.statuses.fetch(:pending)
end
end
end
end

View file

@ -13,6 +13,8 @@
= form_for @avis, url: gestionnaire_avis_path(@avis), html: { class: 'form' } do |f| = form_for @avis, url: gestionnaire_avis_path(@avis), html: { class: 'form' } do |f|
= f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true = f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true
= render partial: "shared/piece_jointe/pj_upload_field", locals: { pj: @avis.piece_justificative_file, object: @avis, form: f }
.flex.justify-between.align-baseline .flex.justify-between.align-baseline
%p.confidentiel.flex %p.confidentiel.flex
- if @avis.confidentiel? - if @avis.confidentiel?

View file

@ -28,4 +28,5 @@
Réponse donnée le #{l(avis.updated_at, format: '%d/%m/%y à %H:%M')} Réponse donnée le #{l(avis.updated_at, format: '%d/%m/%y à %H:%M')}
- else - else
%span.waiting En attente de réponse %span.waiting En attente de réponse
= render partial: 'shared/piece_jointe/pj_link', locals: { object: avis, pj: avis.piece_justificative_file }
%p= avis.answer %p= avis.answer

View file

@ -0,0 +1,20 @@
- if pj.attached?
.pj-link
- if object.virus_scan.blank? || object.virus_scan.safe?
= link_to url_for(pj), target: '_blank', title: "Télécharger la pièce jointe" do
%span.icon.attachment
= pj.filename.to_s
- if object.virus_scan.blank?
(ce fichier na pas été analysé par notre antivirus, téléchargez-le avec précaution)
- else
= pj.filename.to_s
- if object.virus_scan.pending?
(analyse antivirus en cours
= link_to "rafraichir", request.path
)
- elsif object.virus_scan.infected?
- if user_can_upload
(virus détecté, merci denvoyer un autre fichier)
- else
(virus détecté, le téléchargement de ce fichier est bloqué)

View file

@ -0,0 +1,12 @@
.piece-justificative
- if pj.attached?
.piece-justificative-actions{ id: "piece_justificative_#{object.id}" }
.piece-justificative-action
= render partial: "shared/piece_jointe/pj_link", locals: { object: object, pj: object.piece_justificative_file, user_can_upload: true }
.piece-justificative-action
= button_tag 'Remplacer', type: 'button', class: 'button small', data: { 'toggle-target': "#champs_#{object.id}" }
= form.file_field :piece_justificative_file,
id: "champs_#{object.id}",
class: "piece-justificative-input #{'hidden' if pj.attached?}",
direct_upload: true

View file

@ -53,14 +53,30 @@ describe Gestionnaires::AvisController, type: :controller do
end end
describe '#update' do describe '#update' do
before do describe 'without attachment' do
patch :update, params: { id: avis_without_answer.id, avis: { answer: 'answer' } } before do
avis_without_answer.reload patch :update, params: { id: avis_without_answer.id, avis: { answer: 'answer' } }
end avis_without_answer.reload
end
it { expect(response).to redirect_to(instruction_gestionnaire_avis_path(avis_without_answer)) } it { expect(response).to redirect_to(instruction_gestionnaire_avis_path(avis_without_answer)) }
it { expect(avis_without_answer.answer).to eq('answer') } it { expect(avis_without_answer.answer).to eq('answer') }
it { expect(flash.notice).to eq('Votre réponse est enregistrée.') } it { expect(avis_without_answer.piece_justificative_file).to_not be_attached }
it { expect(flash.notice).to eq('Votre réponse est enregistrée.') }
end
describe 'with attachment' do
let(:file) { Rack::Test::UploadedFile.new("./spec/fixtures/files/piece_justificative_0.pdf", 'application/pdf') }
before do
post :update, params: { id: avis_without_answer.id, avis: { answer: 'answer', piece_justificative_file: file } }
avis_without_answer.reload
end
it { expect(response).to redirect_to(instruction_gestionnaire_avis_path(avis_without_answer)) }
it { expect(avis_without_answer.answer).to eq('answer') }
it { expect(avis_without_answer.piece_justificative_file).to be_attached }
it { expect(flash.notice).to eq('Votre réponse est enregistrée.') }
end
end end
describe '#create_commentaire' do describe '#create_commentaire' do