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), = form_for procedure.administrateurs.new(user: User.new),
url: { controller: 'procedure_administrateurs' }, url: { controller: 'procedure_administrateurs' },
html: { class: 'form', id: "procedure-#{procedure.id}-new_administrateur" } , html: { class: 'form', id: "new_administrateur" },
remote: true do |f| data: { turbo: true } do |f|
= f.label :email do = f.label :email do
Ajouter un administrateur Ajouter un administrateur
%p.notice Renseignez lemail dun administrateur déjà enregistré sur #{APPLICATION_NAME} pour lui permettre de modifier « #{procedure.libelle} ». %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= administrateur.email
%td= try_format_datetime(administrateur.created_at) %td= try_format_datetime(administrateur.created_at)
%td= administrateur.registration_state %td= administrateur.registration_state
@ -6,8 +6,8 @@
- if administrateur == current_administrateur - if administrateur == current_administrateur
Cest vous ! Cest vous !
- else - else
= link_to 'Retirer', = button_to 'Retirer',
admin_procedure_administrateur_path(@procedure, administrateur), admin_procedure_administrateur_path(procedure, administrateur),
method: :delete, method: :delete,
'data-confirm': "Retirer « #{administrateur.email} » des administrateurs de « #{@procedure.libelle} » ?", class: 'button',
remote: true 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= 'Adresse email'
%th= 'Enregistré le' %th= 'Enregistré le'
%th= 'État' %th= 'État'
%tbody{ id: "procedure-#{@procedure.id}-administrateurs" } %tbody#administrateurs
= render partial: 'administrateur', collection: @procedure.administrateurs.order('users.email') = render partial: 'administrateur', collection: @procedure.administrateurs.order('users.email'), locals: { procedure: @procedure }
%tfoot %tfoot
%tr %tr
%th{ colspan: 4 } %th{ colspan: 4 }

View file

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