Skip to content

Commit

Permalink
Admin: can update editors
Browse files Browse the repository at this point in the history
  • Loading branch information
skelz0r committed Jan 14, 2025
1 parent 1bbf870 commit 37ea143
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
30 changes: 30 additions & 0 deletions app/controllers/admin/editors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions app/views/admin/editors/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= form_with(model: @editor, url: admin_editor_path(@editor)) do |f| %>
<div class="fr-input-group">
<%= f.label :name, 'Nom du formulaire', class: %w[fr-label] %>
<%= f.text_field :name, class: %[fr-input] %>
</div>

<div class="fr-input-group">
<%= 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] %>
</div>

<div class="fr-checkbox-group">
<%= f.check_box :copy_token, class: %[fr-checkbox] %>
<%= f.label :copy_token, "Peut copier les jetons de ses clients ?", class: %w[fr-label] %>
</div>

<%= f.submit 'Sauvegarder', class: %w[fr-btn] %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/admin/editors/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'Formulaires',
'Features',
'Emails',
'Actions',
].each do |attr|
%>
<th scope="col">
Expand Down Expand Up @@ -56,6 +57,9 @@
</ul>
<% end %>
</td>
<td class="editor-actions">
<%= link_to 'Mettre à jour', edit_admin_editor_path(editor), id: dom_id(editor, :edit), class: %w[fr-btn] %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/features/admin/editors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 37ea143

Please sign in to comment.