From 37ea143b38ff4b693ef43252b46fcd94c545613f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20Delmaire?= Date: Tue, 14 Jan 2025 08:48:16 +0100 Subject: [PATCH] Admin: can update editors --- app/controllers/admin/editors_controller.rb | 30 +++++++++++++++++++++ app/views/admin/editors/edit.html.erb | 18 +++++++++++++ app/views/admin/editors/index.html.erb | 4 +++ config/routes.rb | 2 +- spec/features/admin/editors_spec.rb | 19 +++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/editors/edit.html.erb diff --git a/app/controllers/admin/editors_controller.rb b/app/controllers/admin/editors_controller.rb index 281d5a691..57cbeb7cf 100644 --- a/app/controllers/admin/editors_controller.rb +++ b/app/controllers/admin/editors_controller.rb @@ -2,4 +2,34 @@ class Admin::EditorsController < AdminController def index @editors = Editor.includes(:users).page(params[:page]) end + + def edit + @editor = Editor.find(params[:id]) + end + + def update + @editor = Editor.find(params[:id]) + + if @editor.update(editor_update_params) + success_message(title: 'Éditeur mis à jour') + + redirect_to admin_editors_path + else + error_message(title: 'Erreur lors de la mise à jour de l\'éditeur') + + render 'edit', status: :unprocessable_entity + end + end + + private + + def editor_update_params + params.require(:editor).permit( + :name, + :form_uids, + :copy_token + ).tap do |whitelisted| + whitelisted[:form_uids] = (whitelisted[:form_uids] || '').split(',').map(&:strip) + end + end end diff --git a/app/views/admin/editors/edit.html.erb b/app/views/admin/editors/edit.html.erb new file mode 100644 index 000000000..ffc82ba96 --- /dev/null +++ b/app/views/admin/editors/edit.html.erb @@ -0,0 +1,18 @@ +<%= form_with(model: @editor, url: admin_editor_path(@editor)) do |f| %> +
+ <%= f.label :name, 'Nom du formulaire', class: %w[fr-label] %> + <%= f.text_field :name, class: %[fr-input] %> +
+ +
+ <%= f.label :form_uids, 'IDs des formulaires, séparés par des virgules', class: %w[fr-label] %> + <%= f.textarea :form_uids, value: @editor.form_uids.join(', '), class: %[fr-input] %> +
+ +
+ <%= f.check_box :copy_token, class: %[fr-checkbox] %> + <%= f.label :copy_token, "Peut copier les jetons de ses clients ?", class: %w[fr-label] %> +
+ + <%= f.submit 'Sauvegarder', class: %w[fr-btn] %> +<% end %> diff --git a/app/views/admin/editors/index.html.erb b/app/views/admin/editors/index.html.erb index cbf4a8567..f1580ca99 100644 --- a/app/views/admin/editors/index.html.erb +++ b/app/views/admin/editors/index.html.erb @@ -12,6 +12,7 @@ 'Formulaires', 'Features', 'Emails', + 'Actions', ].each do |attr| %> @@ -56,6 +57,9 @@ <% end %> + + <%= link_to 'Mettre à jour', edit_admin_editor_path(editor), id: dom_id(editor, :edit), class: %w[fr-btn] %> + <% end %> diff --git a/config/routes.rb b/config/routes.rb index 258aff484..5e6cc4367 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,7 @@ post :impersonate, on: :member post :stop_impersonating, on: :collection end - resources :editors, only: %i[index] + resources :editors, only: %i[index edit update] end get '/editeur', to: redirect('/editeur/habilitations'), as: :editor diff --git a/spec/features/admin/editors_spec.rb b/spec/features/admin/editors_spec.rb index 3a07081e6..72781c305 100644 --- a/spec/features/admin/editors_spec.rb +++ b/spec/features/admin/editors_spec.rb @@ -21,4 +21,23 @@ expect(page).to have_content(editor.form_uids.first) end end + + describe 'update' do + subject do + visit edit_admin_editor_path(editor) + + fill_in 'editor_form_uids', with: new_forms + + click_on 'Sauvegarder' + end + + let(:editor) { create(:editor) } + let(:new_forms) { 'new_form1, new_form2' } + + it 'works and displays flash message' do + expect { subject }.to change { editor.reload.form_uids }.to(new_forms.split(', ')) + + expect(page).to have_css('.fr-alert.fr-alert--success') + end + end end