From b32ed88f9b1d5f100c8a4eb7dbdaf2c377eac857 Mon Sep 17 00:00:00 2001 From: kara Diaby Date: Tue, 3 Mar 2020 12:52:35 +0100 Subject: [PATCH] add attachment when instructor asking for a review --- app/controllers/concerns/create_avis_concern.rb | 4 ++-- app/models/avis.rb | 1 + app/views/instructeurs/avis/instruction.html.haml | 4 ++++ app/views/instructeurs/shared/avis/_form.html.haml | 6 ++++++ .../instructeurs/avis_controller_spec.rb | 14 +++++++++++++- spec/features/instructeurs/expert_spec.rb | 2 +- 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index e12f007ef..ecc0d412a 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -23,6 +23,7 @@ module CreateAvisConcern { email: email, introduction: create_avis_params[:introduction], + introduction_file: create_avis_params[:introduction_file], claimant: current_instructeur, dossier: dossier, confidentiel: confidentiel @@ -43,7 +44,6 @@ module CreateAvisConcern sent_emails_addresses << avis.email_to_display end end - flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses.uniq.join(", ")}" end @@ -61,6 +61,6 @@ module CreateAvisConcern end def create_avis_params - params.require(:avis).permit(:introduction, :confidentiel, :invite_linked_dossiers, emails: []) + params.require(:avis).permit(:introduction_file, :introduction, :confidentiel, :invite_linked_dossiers, emails: []) end end diff --git a/app/models/avis.rb b/app/models/avis.rb index 26b9bbf5a..fae8f3a35 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -6,6 +6,7 @@ class Avis < ApplicationRecord belongs_to :claimant, class_name: 'Instructeur' has_one_attached :piece_justificative_file + has_one_attached :introduction_file validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true validates :claimant, presence: true diff --git a/app/views/instructeurs/avis/instruction.html.haml b/app/views/instructeurs/avis/instruction.html.haml index 62344dcc9..c891620c1 100644 --- a/app/views/instructeurs/avis/instruction.html.haml +++ b/app/views/instructeurs/avis/instruction.html.haml @@ -11,6 +11,10 @@ %span.date Demande d'avis envoyée le #{l(@avis.created_at, format: '%d/%m/%y')} %p.introduction= @avis.introduction + - if @avis.introduction_file.attached? + = render partial: 'shared/attachment/show', locals: { attachment: @avis.introduction_file.attachment } + %br/ + = form_for @avis, url: instructeur_avis_path(@avis), html: { class: 'form' } do |f| = f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true = render 'shared/attachment/edit', diff --git a/app/views/instructeurs/shared/avis/_form.html.haml b/app/views/instructeurs/shared/avis/_form.html.haml index b86172d8b..e06b538c4 100644 --- a/app/views/instructeurs/shared/avis/_form.html.haml +++ b/app/views/instructeurs/shared/avis/_form.html.haml @@ -5,6 +5,12 @@ = form_for avis, url: url, html: { class: 'form' } do |f| = f.email_field :emails, placeholder: 'Adresses email, séparées par des virgules', required: true, multiple: true, onchange: "javascript:DS.replaceSemicolonByComma(event);" = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true + %p.tab-title Ajouter une pièce jointe + .form-group + = render 'shared/attachment/edit', + { form: f, + attached_file: avis.introduction_file, + user_can_destroy: true } - if linked_dossiers.present? = f.check_box :invite_linked_dossiers, {}, true, false diff --git a/spec/controllers/instructeurs/avis_controller_spec.rb b/spec/controllers/instructeurs/avis_controller_spec.rb index e0ea52292..bdefc0009 100644 --- a/spec/controllers/instructeurs/avis_controller_spec.rb +++ b/spec/controllers/instructeurs/avis_controller_spec.rb @@ -126,7 +126,9 @@ describe Instructeurs::AvisController, type: :controller do let(:invite_linked_dossiers) { nil } before do - post :create_avis, params: { id: previous_avis.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel, invite_linked_dossiers: invite_linked_dossiers } } + @introduction_file = Rack::Test::UploadedFile.new("./spec/fixtures/files/piece_justificative_0.pdf", 'application/pdf') + post :create_avis, params: { id: previous_avis.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel, invite_linked_dossiers: invite_linked_dossiers, introduction_file: @introduction_file } } + created_avis.reload end context 'when an invalid email' do @@ -139,6 +141,16 @@ describe Instructeurs::AvisController, type: :controller do it { expect(Avis.last).to eq(previous_avis) } end + context 'ask review with attachment' do + let(:previous_avis_confidentiel) { false } + let(:asked_confidentiel) { false } + let(:emails) { ["toto@totomail.com"] } + + it { expect(created_avis.introduction_file).to be_attached } + it { expect(created_avis.introduction_file.filename).to eq("piece_justificative_0.pdf") } + it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à toto@totomail.com") } + end + context 'with multiple emails' do let(:asked_confidentiel) { false } let(:previous_avis_confidentiel) { false } diff --git a/spec/features/instructeurs/expert_spec.rb b/spec/features/instructeurs/expert_spec.rb index 2e365f10f..ea81ab324 100644 --- a/spec/features/instructeurs/expert_spec.rb +++ b/spec/features/instructeurs/expert_spec.rb @@ -113,7 +113,7 @@ feature 'Inviting an expert:' do expect(page).to have_text('Cet avis est confidentiel') fill_in 'avis_answer', with: 'Ma réponse d’expert : c’est un oui.' - find('.attachment input[type=file]').attach_file(Rails.root + 'spec/fixtures/files/RIB.pdf') + find('.attachment input[name="avis[piece_justificative_file]"]').attach_file(Rails.root + 'spec/fixtures/files/RIB.pdf') click_on 'Envoyer votre avis' expect(page).to have_content('Votre réponse est enregistrée')