commit
1ad2789a86
6 changed files with 65 additions and 10 deletions
|
@ -5,6 +5,11 @@ class AttachmentsController < ApplicationController
|
||||||
def show
|
def show
|
||||||
@attachment = @blob.attachments.find(params[:id])
|
@attachment = @blob.attachments.find(params[:id])
|
||||||
@user_can_upload = params[:user_can_upload]
|
@user_can_upload = params[:user_can_upload]
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.js
|
||||||
|
format.html { redirect_back(fallback_location: @attachment.record&.dossier || root_path) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|
|
@ -93,7 +93,18 @@ export default class AutoUploadController {
|
||||||
this.input.disabled = false;
|
this.input.disabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_isError422(error) {
|
||||||
|
// Ajax errors have an xhr attribute
|
||||||
|
if (error && error.xhr && error.xhr.status == 422) return true;
|
||||||
|
// Rails DirectUpload errors are returned as a String, e.g. 'Error creating Blob for "Demain.txt". Status: 422'
|
||||||
|
if (error && error.toString().includes('422')) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
_messageFromError(error) {
|
_messageFromError(error) {
|
||||||
|
let allowRetry = !this._isError422(error);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
error.xhr &&
|
error.xhr &&
|
||||||
error.xhr.status == 422 &&
|
error.xhr.status == 422 &&
|
||||||
|
@ -104,13 +115,13 @@ export default class AutoUploadController {
|
||||||
return {
|
return {
|
||||||
title: error.response.errors[0],
|
title: error.response.errors[0],
|
||||||
description: '',
|
description: '',
|
||||||
retry: false
|
retry: allowRetry
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
title: 'Une erreur s’est produite pendant l’envoi du fichier.',
|
title: 'Une erreur s’est produite pendant l’envoi du fichier.',
|
||||||
description: error.message || error.toString(),
|
description: error.message || error.toString(),
|
||||||
retry: true
|
retry: allowRetry
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ delegate('change', fileInputSelector, event => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const retryButtonSelector = `button.attachment-error-retry`;
|
const retryButtonSelector = `button.attachment-error-retry`;
|
||||||
delegate('click', retryButtonSelector, event => {
|
delegate('click', retryButtonSelector, function() {
|
||||||
const inputSelector = event.target.dataset.inputTarget;
|
const inputSelector = this.dataset.inputTarget;
|
||||||
const input = document.querySelector(inputSelector);
|
const input = document.querySelector(inputSelector);
|
||||||
startUpload(input);
|
startUpload(input);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
try {
|
try {
|
||||||
window.scroll({ top: 0, left: 0, behavior: 'smooth' });
|
window.scroll({ top: 0, left: 0, behavior: 'smooth' });
|
||||||
} catch {
|
} catch(e) {
|
||||||
window.scroll(0, 0);
|
window.scroll(0, 0);
|
||||||
}
|
}
|
||||||
<%= remove_element('#user-satisfaction') %>
|
<%= remove_element('#user-satisfaction') %>
|
||||||
|
|
|
@ -124,8 +124,7 @@ Rails.application.routes.draw do
|
||||||
put 'piece_justificative/:champ_id', to: 'piece_justificative#update', as: :piece_justificative
|
put 'piece_justificative/:champ_id', to: 'piece_justificative#update', as: :piece_justificative
|
||||||
end
|
end
|
||||||
|
|
||||||
get 'attachments/:id', to: 'attachments#show', as: :attachment
|
resources :attachments, only: [:show, :destroy]
|
||||||
delete 'attachments/:id', to: 'attachments#destroy'
|
|
||||||
|
|
||||||
get "patron" => "root#patron"
|
get "patron" => "root#patron"
|
||||||
get "accessibilite" => "root#accessibilite"
|
get "accessibilite" => "root#accessibilite"
|
||||||
|
|
|
@ -1,5 +1,45 @@
|
||||||
describe AttachmentsController, type: :controller do
|
describe AttachmentsController, type: :controller do
|
||||||
let(:user) { create(:user) }
|
let(:user) { create(:user) }
|
||||||
|
let(:attachment) { champ.piece_justificative_file.attachment }
|
||||||
|
let(:dossier) { create(:dossier, user: user) }
|
||||||
|
let(:champ) { create(:champ_piece_justificative, dossier_id: dossier.id) }
|
||||||
|
let(:signed_id) { attachment.blob.signed_id }
|
||||||
|
|
||||||
|
describe '#show' do
|
||||||
|
render_views
|
||||||
|
|
||||||
|
let(:format) { :js }
|
||||||
|
|
||||||
|
subject do
|
||||||
|
get :show, params: { id: attachment.id, signed_id: signed_id }, format: format
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when authenticated' do
|
||||||
|
before { sign_in(user) }
|
||||||
|
|
||||||
|
context 'when requesting Javascript' do
|
||||||
|
let(:format) { :js }
|
||||||
|
|
||||||
|
it { is_expected.to have_http_status(200) }
|
||||||
|
|
||||||
|
it 'renders JS that replaces the attachment HTML' do
|
||||||
|
subject
|
||||||
|
expect(response.body).to have_text(".attachment-link[data-attachment-id=\"#{attachment.id}\"]")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the user opens the delete link in a new tab' do
|
||||||
|
let(:format) { :html }
|
||||||
|
|
||||||
|
it { is_expected.to have_http_status(302) }
|
||||||
|
it { is_expected.to redirect_to(dossier_path(dossier)) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when not authenticated' do
|
||||||
|
it { is_expected.to have_http_status(401) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#destroy' do
|
describe '#destroy' do
|
||||||
render_views
|
render_views
|
||||||
|
@ -19,7 +59,7 @@ describe AttachmentsController, type: :controller do
|
||||||
context 'and dossier is owned by user' do
|
context 'and dossier is owned by user' do
|
||||||
it { is_expected.to have_http_status(200) }
|
it { is_expected.to have_http_status(200) }
|
||||||
|
|
||||||
it do
|
it 'removes the attachment' do
|
||||||
subject
|
subject
|
||||||
expect(champ.reload.piece_justificative_file.attached?).to be(false)
|
expect(champ.reload.piece_justificative_file.attached?).to be(false)
|
||||||
end
|
end
|
||||||
|
@ -30,7 +70,7 @@ describe AttachmentsController, type: :controller do
|
||||||
|
|
||||||
it { is_expected.to have_http_status(404) }
|
it { is_expected.to have_http_status(404) }
|
||||||
|
|
||||||
it do
|
it 'doesn’t remove the attachment' do
|
||||||
subject
|
subject
|
||||||
expect(champ.reload.piece_justificative_file.attached?).to be(true)
|
expect(champ.reload.piece_justificative_file.attached?).to be(true)
|
||||||
end
|
end
|
||||||
|
@ -40,7 +80,7 @@ describe AttachmentsController, type: :controller do
|
||||||
context 'when not authenticated' do
|
context 'when not authenticated' do
|
||||||
it { is_expected.to have_http_status(401) }
|
it { is_expected.to have_http_status(401) }
|
||||||
|
|
||||||
it do
|
it 'doesn’t remove the attachment' do
|
||||||
subject
|
subject
|
||||||
expect(champ.reload.piece_justificative_file.attached?).to be(true)
|
expect(champ.reload.piece_justificative_file.attached?).to be(true)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue