refactor(attachment): attachments controller to use turbo
This commit is contained in:
parent
32bd1b6d9f
commit
e9b02642c5
6 changed files with 20 additions and 23 deletions
|
@ -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
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
<%= render_flash(timeout: 5000, sticky: true) %>
|
||||
<%= remove_element(".attachment-actions-#{@attachment_id}") %>
|
||||
<%= show_element(".attachment-input-#{@attachment_id}") %>
|
2
app/views/attachments/destroy.turbo_stream.haml
Normal file
2
app/views/attachments/destroy.turbo_stream.haml
Normal file
|
@ -0,0 +1,2 @@
|
|||
= turbo_stream.remove dom_id(@attachment, :actions)
|
||||
= turbo_stream.show_all ".attachment-input-#{@attachment.id}"
|
|
@ -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 %>
|
2
app/views/attachments/show.turbo_stream.haml
Normal file
2
app/views/attachments/show.turbo_stream.haml
Normal 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)
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue