refactor(turbo): use turbo in procedure administrateurs controller

This commit is contained in:
Paul Chavard 2022-04-19 12:28:29 +02:00
parent 8d0383c6f8
commit cad63391db
8 changed files with 20 additions and 27 deletions

View file

@ -1,7 +1,7 @@
= form_for procedure.administrateurs.new(user: User.new),
url: { controller: 'procedure_administrateurs' },
html: { class: 'form', id: "procedure-#{procedure.id}-new_administrateur" } ,
remote: true do |f|
html: { class: 'form', id: "new_administrateur" },
data: { turbo: true } do |f|
= f.label :email do
Ajouter un administrateur
%p.notice Renseignez lemail dun administrateur déjà enregistré sur #{APPLICATION_NAME} pour lui permettre de modifier « #{procedure.libelle} ».

View file

@ -1,4 +1,4 @@
%tr{ id: "procedure-#{@procedure.id}-administrateur-#{administrateur.id}" }
%tr{ id: dom_id(administrateur) }
%td= administrateur.email
%td= try_format_datetime(administrateur.created_at)
%td= administrateur.registration_state
@ -6,8 +6,8 @@
- if administrateur == current_administrateur
Cest vous !
- else
= link_to 'Retirer',
admin_procedure_administrateur_path(@procedure, administrateur),
= button_to 'Retirer',
admin_procedure_administrateur_path(procedure, administrateur),
method: :delete,
'data-confirm': "Retirer « #{administrateur.email} » des administrateurs de « #{@procedure.libelle} » ?",
remote: true
class: 'button',
form: { data: { turbo: true, turbo_confirm: "Retirer « #{administrateur.email} » des administrateurs de « #{procedure.libelle} » ?" } }

View file

@ -1,9 +0,0 @@
= render_flash(sticky: true)
- if @administrateur
= append_to_element("#procedure-#{@procedure.id}-administrateurs",
partial: 'administrateur',
locals: { administrateur: @administrateur })
= render_to_element("#procedure-#{@procedure.id}-new_administrateur",
partial: 'add_admin_form',
outer: true,
locals: { procedure: @procedure })

View file

@ -0,0 +1,3 @@
- if @administrateur.present?
= turbo_stream.append "administrateurs", partial: 'administrateur', locals: { procedure: @procedure, administrateur: @administrateur }
= turbo_stream.replace "new_administrateur", partial: 'add_admin_form', locals: { procedure: @procedure }

View file

@ -1,4 +0,0 @@
= render_flash(sticky: true)
- if @administrateur
= remove_element("#procedure-#{@procedure.id}-administrateur-#{@administrateur.id}")

View file

@ -0,0 +1,2 @@
- if @administrateur.present?
= turbo_stream.remove(@administrateur)

View file

@ -10,8 +10,8 @@
%th= 'Adresse email'
%th= 'Enregistré le'
%th= 'État'
%tbody{ id: "procedure-#{@procedure.id}-administrateurs" }
= render partial: 'administrateur', collection: @procedure.administrateurs.order('users.email')
%tbody#administrateurs
= render partial: 'administrateur', collection: @procedure.administrateurs.order('users.email'), locals: { procedure: @procedure }
%tfoot
%tr
%th{ colspan: 4 }

View file

@ -2,6 +2,7 @@ describe Administrateurs::ProcedureAdministrateursController, type: :controller
let(:signed_in_admin) { create(:administrateur) }
let(:other_admin) { create(:administrateur) }
let(:procedure) { create(:procedure, administrateurs: [signed_in_admin, other_admin]) }
render_views
before do
sign_in(signed_in_admin.user)
@ -9,7 +10,7 @@ describe Administrateurs::ProcedureAdministrateursController, type: :controller
describe '#destroy' do
subject do
delete :destroy, params: { procedure_id: procedure.id, id: admin_to_remove.id }, format: :js, xhr: true
delete :destroy, params: { procedure_id: procedure.id, id: admin_to_remove.id }, format: :turbo_stream
end
context 'when removing another admin' do
@ -17,8 +18,8 @@ describe Administrateurs::ProcedureAdministrateursController, type: :controller
it 'removes the admin from the procedure' do
subject
expect(response.status).to eq(200)
expect(flash[:notice]).to be_present
expect(response).to have_http_status(:ok)
expect(subject.body).to include('alert-success')
expect(admin_to_remove.procedures.reload).not_to include(procedure)
end
end
@ -28,8 +29,8 @@ describe Administrateurs::ProcedureAdministrateursController, type: :controller
it 'denies the right for an admin to remove itself' do
subject
expect(response.status).to eq(200)
expect(flash[:alert]).to be_present
expect(response).to have_http_status(:ok)
expect(subject.body).to include('alert-danger')
expect(admin_to_remove.procedures.reload).to include(procedure)
end
end