refactor(attachment): attachments controller to use turbo

This commit is contained in:
Paul Chavard 2022-05-06 12:25:58 +02:00
parent 32bd1b6d9f
commit e9b02642c5
6 changed files with 20 additions and 23 deletions

View file

@ -7,15 +7,19 @@ class AttachmentsController < ApplicationController
@user_can_upload = params[:user_can_upload] @user_can_upload = params[:user_can_upload]
respond_to do |format| respond_to do |format|
format.js format.turbo_stream
format.html { redirect_back(fallback_location: root_url) } format.html { redirect_back(fallback_location: root_url) }
end end
end end
def destroy def destroy
attachment = @blob.attachments.find(params[:id]) @attachment = @blob.attachments.find(params[:id])
@attachment_id = attachment.id @attachment.purge_later
attachment.purge_later flash.notice = 'La pièce jointe a bien été supprimée.'
flash.now.notice = 'La pièce jointe a bien été supprimée.'
respond_to do |format|
format.turbo_stream
format.html { redirect_back(fallback_location: root_url) }
end
end end
end end

View file

@ -1,3 +0,0 @@
<%= render_flash(timeout: 5000, sticky: true) %>
<%= remove_element(".attachment-actions-#{@attachment_id}") %>
<%= show_element(".attachment-input-#{@attachment_id}") %>

View file

@ -0,0 +1,2 @@
= turbo_stream.remove dom_id(@attachment, :actions)
= turbo_stream.show_all ".attachment-input-#{@attachment.id}"

View file

@ -1,8 +0,0 @@
<%= render_to_element(".attachment-link[data-attachment-id=\"#{@attachment.id}\"]",
partial: 'shared/attachment/show',
outer: true,
locals: { attachment: @attachment, user_can_upload: @user_can_upload }) %>
<% if @attachment.virus_scanner.pending? || @attachment.watermark_pending? %>
<%= fire_event('attachment:update', { url: attachment_url(@attachment.id, { signed_id: @attachment.blob.signed_id, user_can_upload: @user_can_upload }) }.to_json ) %>
<% end %>

View file

@ -0,0 +1,2 @@
= turbo_stream.replace dom_id(@attachment, :show) do
= render Attachment::ShowComponent.new(attachment: @attachment, user_can_upload: @user_can_upload)

View file

@ -8,24 +8,24 @@ describe AttachmentsController, type: :controller do
describe '#show' do describe '#show' do
render_views render_views
let(:format) { :js } let(:format) { :turbo_stream }
subject do subject do
request.headers['HTTP_REFERER'] = dossier_url(dossier) request.headers['HTTP_REFERER'] = dossier_url(dossier)
get :show, params: { id: attachment.id, signed_id: signed_id }, format: format, xhr: (format == :js) get :show, params: { id: attachment.id, signed_id: signed_id }, format: format
end end
context 'when authenticated' do context 'when authenticated' do
before { sign_in(user) } before { sign_in(user) }
context 'when requesting Javascript' do context 'when requesting turbo_stream' do
let(:format) { :js } let(:format) { :turbo_stream }
it { is_expected.to have_http_status(200) } it { is_expected.to have_http_status(200) }
it 'renders JS that replaces the attachment HTML' do it 'renders turbo_stream that replaces the attachment HTML' do
subject subject
expect(response.body).to have_text(".attachment-link[data-attachment-id=\"#{attachment.id}\"]") expect(response.body).to include(ActionView::RecordIdentifier.dom_id(attachment, :show))
end end
end end
@ -51,7 +51,7 @@ describe AttachmentsController, type: :controller do
let(:signed_id) { attachment.blob.signed_id } let(:signed_id) { attachment.blob.signed_id }
subject do subject do
delete :destroy, params: { id: attachment.id, signed_id: signed_id }, format: :js delete :destroy, params: { id: attachment.id, signed_id: signed_id }, format: :turbo_stream
end end
context "when authenticated" do context "when authenticated" do