From e9b02642c583b63f2b0565596bc6b0a9b2b49a08 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 6 May 2022 12:25:58 +0200 Subject: [PATCH] refactor(attachment): attachments controller to use turbo --- app/controllers/attachments_controller.rb | 14 +++++++++----- app/views/attachments/destroy.js.erb | 3 --- app/views/attachments/destroy.turbo_stream.haml | 2 ++ app/views/attachments/show.js.erb | 8 -------- app/views/attachments/show.turbo_stream.haml | 2 ++ spec/controllers/attachments_controller_spec.rb | 14 +++++++------- 6 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 app/views/attachments/destroy.js.erb create mode 100644 app/views/attachments/destroy.turbo_stream.haml delete mode 100644 app/views/attachments/show.js.erb create mode 100644 app/views/attachments/show.turbo_stream.haml diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index a3a2fc471..be5d3ace2 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -7,15 +7,19 @@ class AttachmentsController < ApplicationController @user_can_upload = params[:user_can_upload] respond_to do |format| - format.js + format.turbo_stream format.html { redirect_back(fallback_location: root_url) } end end def destroy - attachment = @blob.attachments.find(params[:id]) - @attachment_id = attachment.id - attachment.purge_later - flash.now.notice = 'La pièce jointe a bien été supprimée.' + @attachment = @blob.attachments.find(params[:id]) + @attachment.purge_later + flash.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 diff --git a/app/views/attachments/destroy.js.erb b/app/views/attachments/destroy.js.erb deleted file mode 100644 index 0d4b5e5ff..000000000 --- a/app/views/attachments/destroy.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= render_flash(timeout: 5000, sticky: true) %> -<%= remove_element(".attachment-actions-#{@attachment_id}") %> -<%= show_element(".attachment-input-#{@attachment_id}") %> diff --git a/app/views/attachments/destroy.turbo_stream.haml b/app/views/attachments/destroy.turbo_stream.haml new file mode 100644 index 000000000..323dd8f03 --- /dev/null +++ b/app/views/attachments/destroy.turbo_stream.haml @@ -0,0 +1,2 @@ += turbo_stream.remove dom_id(@attachment, :actions) += turbo_stream.show_all ".attachment-input-#{@attachment.id}" diff --git a/app/views/attachments/show.js.erb b/app/views/attachments/show.js.erb deleted file mode 100644 index 0a58c0af5..000000000 --- a/app/views/attachments/show.js.erb +++ /dev/null @@ -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 %> diff --git a/app/views/attachments/show.turbo_stream.haml b/app/views/attachments/show.turbo_stream.haml new file mode 100644 index 000000000..dc7cd008f --- /dev/null +++ b/app/views/attachments/show.turbo_stream.haml @@ -0,0 +1,2 @@ += turbo_stream.replace dom_id(@attachment, :show) do + = render Attachment::ShowComponent.new(attachment: @attachment, user_can_upload: @user_can_upload) diff --git a/spec/controllers/attachments_controller_spec.rb b/spec/controllers/attachments_controller_spec.rb index fe10c5f03..8b1f8dff8 100644 --- a/spec/controllers/attachments_controller_spec.rb +++ b/spec/controllers/attachments_controller_spec.rb @@ -8,24 +8,24 @@ describe AttachmentsController, type: :controller do describe '#show' do render_views - let(:format) { :js } + let(:format) { :turbo_stream } subject do 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 context 'when authenticated' do before { sign_in(user) } - context 'when requesting Javascript' do - let(:format) { :js } + context 'when requesting turbo_stream' do + let(:format) { :turbo_stream } 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 - 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 @@ -51,7 +51,7 @@ describe AttachmentsController, type: :controller do let(:signed_id) { attachment.blob.signed_id } 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 context "when authenticated" do