champs: show attachment actions only when the atachment is persisted

Fix #3961
This commit is contained in:
Pierre de La Morinerie 2019-06-17 16:13:12 +02:00
parent b98c6e1994
commit 9ce92d5c3b
3 changed files with 70 additions and 3 deletions

View file

@ -5,8 +5,10 @@
= link_to('le modèle suivant', url_for(template), target: '_blank', rel: 'noopener')
- attachment_id = attachment ? attachment.id : SecureRandom.uuid
- persisted = attachment && attachment.persisted?
- user_can_destroy = defined?(user_can_destroy) ? user_can_destroy : false
- if attachment
- if persisted
.piece-justificative-actions{ id: "piece_justificative_#{attachment_id}" }
.piece-justificative-action
= render partial: "shared/attachment/show", locals: { attachment: attachment, user_can_upload: true }
@ -18,5 +20,5 @@
= form.file_field :piece_justificative_file,
id: "piece_justificative_file_#{attachment_id}",
class: "piece-justificative-input #{'hidden' if attachment}",
class: "piece-justificative-input #{'hidden' if persisted}",
direct_upload: true

View file

@ -147,7 +147,7 @@ FactoryBot.define do
factory :champ_piece_justificative, class: 'Champs::PieceJustificativeChamp' do
type_de_champ { create(:type_de_champ_piece_justificative) }
after(:create) do |champ, _evaluator|
after(:build) do |champ, _evaluator|
champ.piece_justificative_file.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
end
end

View file

@ -0,0 +1,65 @@
require 'rails_helper'
describe 'shared/attachment/_update.html.haml', type: :view do
let(:champ) { build(:champ_piece_justificative, dossier: create(:dossier)) }
let(:attachment) { nil }
let(:virus_scan_result) { nil }
let(:user_can_destroy) { false }
subject do
form_for(champ.dossier) do |form|
render 'shared/attachment/update', {
attachment: attachment,
user_can_destroy: user_can_destroy,
form: form
}
end
end
it 'renders a form field for uploading a file' do
expect(subject).to have_selector('input[type=file]:not(.hidden)')
end
context 'when there is a attached file' do
let(:attachment) { champ.piece_justificative_file.attachment }
it 'renders a form field for uploading a file' do
expect(subject).to have_selector('input[type=file]:not(.hidden)')
end
it 'does not renders a link to the unsaved file' do
expect(subject).not_to have_content(attachment.filename.to_s)
end
it 'doesnt render action buttons' do
expect(subject).not_to have_link('Remplacer')
expect(subject).not_to have_link('Supprimer')
end
context 'and the attachment has been saved' do
before { champ.save! }
it 'renders a link to the file' do
expect(subject).to have_content(attachment.filename.to_s)
end
it 'renders action buttons' do
expect(subject).to have_button('Remplacer')
end
it 'hides the form field by default' do
expect(subject).to have_selector('input[type=file].hidden')
end
it 'hides the Delete button by default' do
is_expected.not_to have_link('Supprimer')
end
context 'and the user can delete the attachment' do
let(:user_can_destroy) { true }
it { is_expected.to have_link('Supprimer') }
end
end
end
end