From 95bd13872b1d38b08e1de356796328afc6749227 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 6 May 2022 11:59:28 +0200 Subject: [PATCH] refactor(dossier): change state with turbo --- .../instructeurs/dossiers_controller.rb | 12 ++++--- .../dossiers/_state_button.html.haml | 12 +++---- .../_state_button_motivation.html.haml | 4 +-- .../dossiers/_state_button_refresh.js.erb | 7 ---- .../dossiers/change_state.turbo_stream.haml | 1 + .../instructeurs/dossiers_controller_spec.rb | 32 +++++++++---------- 6 files changed, 33 insertions(+), 35 deletions(-) delete mode 100644 app/views/instructeurs/dossiers/_state_button_refresh.js.erb create mode 100644 app/views/instructeurs/dossiers/change_state.turbo_stream.haml diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 9e8feb9ae..3ac6681ae 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -124,7 +124,8 @@ module Instructeurs flash.alert = aasm_error_message(e, target_state: :en_instruction) end - render partial: 'state_button_refresh', locals: { dossier: dossier } + @dossier = dossier + render :change_state end def repasser_en_construction @@ -135,7 +136,8 @@ module Instructeurs flash.alert = aasm_error_message(e, target_state: :en_construction) end - render partial: 'state_button_refresh', locals: { dossier: dossier } + @dossier = dossier + render :change_state end def repasser_en_instruction @@ -146,7 +148,8 @@ module Instructeurs flash.alert = aasm_error_message(e, target_state: :en_instruction) end - render partial: 'state_button_refresh', locals: { dossier: dossier } + @dossier = dossier + render :change_state end def terminer @@ -174,7 +177,8 @@ module Instructeurs flash.alert = aasm_error_message(e, target_state: target_state) end - render partial: 'state_button_refresh', locals: { dossier: dossier } + @dossier = dossier + render :change_state end def create_commentaire diff --git a/app/views/instructeurs/dossiers/_state_button.html.haml b/app/views/instructeurs/dossiers/_state_button.html.haml index 8954d4308..f1b9580f6 100644 --- a/app/views/instructeurs/dossiers/_state_button.html.haml +++ b/app/views/instructeurs/dossiers/_state_button.html.haml @@ -18,8 +18,8 @@ %h4 En construction Vous permettez à l'usager de modifier ses réponses au formulaire - %li - = link_to passer_en_instruction_instructeur_dossier_path(dossier.procedure, dossier), method: :post, data: { remote: true, confirm: "Confirmez-vous le passage en instruction de ce dossier ?" } do + %li{ 'data-turbo': 'true' } + = link_to passer_en_instruction_instructeur_dossier_path(dossier.procedure, dossier), data: { turbo_method: :post, turbo_confirm: "Confirmez-vous le passage en instruction de ce dossier ?" } do %span.icon.in-progress .dropdown-description %h4 Passer en instruction @@ -31,8 +31,8 @@ -# ------------------------------------------------------ %ul.dropdown-items - %li - = link_to repasser_en_construction_instructeur_dossier_path(dossier.procedure, dossier), method: :post, data: { remote:true, confirm: "Confirmez-vous le passage en construction de ce dossier ?" } do + %li{ 'data-turbo': 'true' } + = link_to repasser_en_construction_instructeur_dossier_path(dossier.procedure, dossier), data: { turbo_method: :post, turbo_confirm: "Confirmez-vous le passage en construction de ce dossier ?" } do %span.icon.edit .dropdown-description %h4 Repasser en construction @@ -100,8 +100,8 @@ %p Cette attestation a été envoyée automatiquement au demandeur. - if dossier.can_repasser_en_instruction? - %li - = link_to repasser_en_instruction_instructeur_dossier_path(dossier.procedure, dossier), method: :post, data: { remote:true, confirm: "Voulez vous remettre le dossier #{dossier.id} en instruction ?" } do + %li{ 'data-turbo': 'true' } + = link_to repasser_en_instruction_instructeur_dossier_path(dossier.procedure, dossier), data: { turbo_method: :post, turbo_confirm: "Voulez vous remettre le dossier #{dossier.id} en instruction ?" } do %span.icon.in-progress .dropdown-description %h4 Repasser en instruction diff --git a/app/views/instructeurs/dossiers/_state_button_motivation.html.haml b/app/views/instructeurs/dossiers/_state_button_motivation.html.haml index 74e0e0664..46b55477d 100644 --- a/app/views/instructeurs/dossiers/_state_button_motivation.html.haml +++ b/app/views/instructeurs/dossiers/_state_button_motivation.html.haml @@ -3,7 +3,7 @@ %span.icon{ class: popup_class } #{popup_title} - = form_tag(terminer_instructeur_dossier_path(dossier.procedure, dossier), remote: true, method: :post, class: 'form') do + = form_tag(terminer_instructeur_dossier_path(dossier.procedure, dossier), data: { turbo: true, turbo_confirm: confirm }, method: :post, multipart: true, class: 'form') do - if title == 'Accepter' = text_area :dossier, :motivation, class: 'motivation-text-area', placeholder: placeholder, required: false - if dossier.procedure.attestation_template&.activated? @@ -37,4 +37,4 @@ = file_field :dossier, :justificatif_motivation, direct_upload: true .text-right %span.button{ onclick: 'DS.motivationCancel();' } Annuler - = button_tag 'Valider la décision', name: :process_action, value: process_action, class: 'button primary', title: title, data: { confirm: confirm } + = button_tag 'Valider la décision', name: :process_action, value: process_action, class: 'button primary', title: title diff --git a/app/views/instructeurs/dossiers/_state_button_refresh.js.erb b/app/views/instructeurs/dossiers/_state_button_refresh.js.erb deleted file mode 100644 index 1fa3e0319..000000000 --- a/app/views/instructeurs/dossiers/_state_button_refresh.js.erb +++ /dev/null @@ -1,7 +0,0 @@ -<%= render_flash %> -<%= render_to_element('.header-actions', partial: 'header_actions', locals: { dossier: dossier }) %> - -<% attachment = dossier.justificatif_motivation.attachment %> -<% if attachment && attachment.virus_scanner.pending? %> - <%= fire_event('attachment:update', { url: attachment_url(attachment.id, { signed_id: attachment.blob.signed_id }) }.to_json ) %> -<% end %> diff --git a/app/views/instructeurs/dossiers/change_state.turbo_stream.haml b/app/views/instructeurs/dossiers/change_state.turbo_stream.haml new file mode 100644 index 000000000..94ce488db --- /dev/null +++ b/app/views/instructeurs/dossiers/change_state.turbo_stream.haml @@ -0,0 +1 @@ += turbo_stream.update_all '.header-actions', partial: 'header_actions', locals: { dossier: @dossier } diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index 1f094c4de..821a19f7f 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -99,7 +99,7 @@ describe Instructeurs::DossiersController, type: :controller do before do sign_in(instructeur.user) - post :passer_en_instruction, params: { procedure_id: procedure.id, dossier_id: dossier.id }, format: 'js' + post :passer_en_instruction, params: { procedure_id: procedure.id, dossier_id: dossier.id }, format: :turbo_stream end it { expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction)) } @@ -113,7 +113,7 @@ describe Instructeurs::DossiersController, type: :controller do it 'warns about the error' do expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction)) expect(response).to have_http_status(:ok) - expect(response.body).to have_text('Le dossier est déjà en instruction.') + expect(response.body).to include('Le dossier est déjà en instruction.') end end @@ -126,7 +126,7 @@ describe Instructeurs::DossiersController, type: :controller do it 'warns about the error' do expect(response).to have_http_status(:ok) - expect(response.body).to have_text('Le dossier est en ce moment accepté : il n’est pas possible de le passer en instruction.') + expect(response.body).to include('Le dossier est en ce moment accepté : il n’est pas possible de le passer en instruction.') end end end @@ -138,7 +138,7 @@ describe Instructeurs::DossiersController, type: :controller do sign_in(instructeur.user) post :repasser_en_construction, params: { procedure_id: procedure.id, dossier_id: dossier.id }, - format: 'js' + format: :turbo_stream end it { expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) } @@ -151,7 +151,7 @@ describe Instructeurs::DossiersController, type: :controller do it 'warns about the error' do expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) expect(response).to have_http_status(:ok) - expect(response.body).to have_text('Le dossier est déjà en construction.') + expect(response.body).to include('Le dossier est déjà en construction.') end end end @@ -163,7 +163,7 @@ describe Instructeurs::DossiersController, type: :controller do subject do post :repasser_en_instruction, params: { procedure_id: procedure.id, dossier_id: dossier.id }, - format: 'js' + format: :turbo_stream end before do @@ -186,7 +186,7 @@ describe Instructeurs::DossiersController, type: :controller do it 'warns about the error' do expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction)) expect(response).to have_http_status(:ok) - expect(response.body).to have_text('Le dossier est déjà en instruction.') + expect(response.body).to include('Le dossier est déjà en instruction.') end end @@ -224,7 +224,7 @@ describe Instructeurs::DossiersController, type: :controller do end context 'simple refusal' do - subject { post :terminer, params: { process_action: "refuser", procedure_id: procedure.id, dossier_id: dossier.id }, format: 'js' } + subject { post :terminer, params: { process_action: "refuser", procedure_id: procedure.id, dossier_id: dossier.id }, format: :turbo_stream } it 'change state to refuse' do subject @@ -244,7 +244,7 @@ describe Instructeurs::DossiersController, type: :controller do end context 'refusal with a justificatif' do - subject { post :terminer, params: { process_action: "refuser", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: 'js' } + subject { post :terminer, params: { process_action: "refuser", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: :turbo_stream } it 'attachs a justificatif' do subject @@ -264,7 +264,7 @@ describe Instructeurs::DossiersController, type: :controller do sign_in(instructeur.user) end context 'without attachment' do - subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure.id, dossier_id: dossier.id }, format: 'js' } + subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure.id, dossier_id: dossier.id }, format: :turbo_stream } it 'change state to sans_suite' do subject @@ -286,7 +286,7 @@ describe Instructeurs::DossiersController, type: :controller do end context 'with attachment' do - subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: 'js' } + subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: :turbo_stream } it 'change state to sans_suite' do subject @@ -312,7 +312,7 @@ describe Instructeurs::DossiersController, type: :controller do expect(NotificationMailer).to receive(:deliver_later) end - subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id }, format: 'js' } + subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id }, format: :turbo_stream } it 'change state to accepte' do subject @@ -361,7 +361,7 @@ describe Instructeurs::DossiersController, type: :controller do procedure_id: procedure.id, dossier_id: dossier.id, dossier: { motivation: "Yallah" } - }, format: 'js' + }, format: :turbo_stream end before do @@ -374,7 +374,7 @@ describe Instructeurs::DossiersController, type: :controller do end context 'with an attachment' do - subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: 'js' } + subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: :turbo_stream } it 'change state to accepte' do subject @@ -393,7 +393,7 @@ describe Instructeurs::DossiersController, type: :controller do before { allow(dossier).to receive(:after_accepter) } - subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: 'js' } + subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: :turbo_stream } it 'does not close it again' do subject @@ -402,7 +402,7 @@ describe Instructeurs::DossiersController, type: :controller do expect(dossier.state).to eq(Dossier.states.fetch(:accepte)) expect(response).to have_http_status(:ok) - expect(response.body).to have_text('Le dossier est déjà accepté.') + expect(response.body).to include('Le dossier est déjà accepté.') end end end