From ecad046a99d8a34c0a218f2bd75062cefa3b213e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 6 Mar 2024 04:53:15 +0100 Subject: [PATCH] Use buttons to destroy drafts and questions As mentioned in commits 5311daadf and bb958daf0, using links combined with JavaScript to generate POST (or, in this case, DELETE) requests to the server has a few issues. --- .../admin/legislation/draft_versions/edit.html.erb | 10 ++++++---- app/views/admin/legislation/questions/edit.html.erb | 9 ++++++--- config/initializers/routes_hierarchy.rb | 6 +++--- config/locales/en/admin.yml | 2 -- config/locales/es/admin.yml | 2 -- config/routes/admin.rb | 4 ++++ spec/system/admin/legislation/draft_versions_spec.rb | 2 +- spec/system/admin/legislation/questions_spec.rb | 2 +- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/views/admin/legislation/draft_versions/edit.html.erb b/app/views/admin/legislation/draft_versions/edit.html.erb index a49b9dbd2..a9fadfdc8 100644 --- a/app/views/admin/legislation/draft_versions/edit.html.erb +++ b/app/views/admin/legislation/draft_versions/edit.html.erb @@ -15,10 +15,12 @@

<%= @draft_version.title %>

- <%= link_to t("admin.legislation.draft_versions.index.delete"), - admin_legislation_process_draft_version_path(@process, @draft_version), - method: :delete, - class: "button hollow alert" %> + <%= render Admin::ActionComponent.new( + :destroy, + @draft_version, + method: :delete, + class: "button hollow alert" + ) %>
diff --git a/app/views/admin/legislation/questions/edit.html.erb b/app/views/admin/legislation/questions/edit.html.erb index 3151efa90..c98ce4eaf 100644 --- a/app/views/admin/legislation/questions/edit.html.erb +++ b/app/views/admin/legislation/questions/edit.html.erb @@ -15,9 +15,12 @@

<%= t("admin.legislation.questions.edit.title", question_title: @question.title) %>

- <%= link_to t("admin.legislation.questions.index.delete"), admin_legislation_process_question_path(@process, @question), - method: :delete, - class: "button hollow alert" %> + <%= render Admin::ActionComponent.new( + :destroy, + @question, + method: :delete, + class: "button hollow alert" + ) %>
diff --git a/config/initializers/routes_hierarchy.rb b/config/initializers/routes_hierarchy.rb index e66c8386c..298805088 100644 --- a/config/initializers/routes_hierarchy.rb +++ b/config/initializers/routes_hierarchy.rb @@ -24,9 +24,9 @@ module ActionDispatch::Routing::UrlFor end def namespaced_polymorphic_path(namespace, resource, options = {}) - if %w[Budget::Group Budget::Heading Poll::Booth Poll::BoothAssignment Poll::Officer - Poll::Question Poll::Question::Answer Poll::Question::Answer::Video Poll::Shift - SDG::LocalTarget].include?(resource.class.name) + if %w[Budget::Group Budget::Heading Legislation::DraftVersion Legislation::Question + Poll::Booth Poll::BoothAssignment Poll::Officer Poll::Question Poll::Question::Answer + Poll::Question::Answer::Video Poll::Shift SDG::LocalTarget].include?(resource.class.name) resolve = resolve_for(resource) resolve_options = resolve.pop diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 95f85c628..a7724c856 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -664,7 +664,6 @@ en: index: title: Draft versions create: Create version - delete: Delete preview: Preview new: back: Back @@ -697,7 +696,6 @@ en: back: Back title: Questions associated to this process create: Create question - delete: Delete new: back: Back title: Create new question diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index e7b7bb600..b1c80ee13 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -664,7 +664,6 @@ es: index: title: Versiones del borrador create: Crear versión - delete: Borrar preview: Previsualizar new: back: Volver @@ -697,7 +696,6 @@ es: back: Volver title: Preguntas asociadas a este proceso create: Crear pregunta - delete: Borrar new: back: Volver title: Crear nueva pregunta diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 7d97f99e3..5ffbf5eba 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -346,3 +346,7 @@ end resolve "Poll::Question::Answer::Video" do |video, options| [:answer, :video, options.merge(answer_id: video.answer, id: video)] end + +resolve "Legislation::DraftVersion" do |version, options| + [version.process, :draft_version, options.merge(id: version)] +end diff --git a/spec/system/admin/legislation/draft_versions_spec.rb b/spec/system/admin/legislation/draft_versions_spec.rb index f5957d2fd..2e5f8034e 100644 --- a/spec/system/admin/legislation/draft_versions_spec.rb +++ b/spec/system/admin/legislation/draft_versions_spec.rb @@ -78,7 +78,7 @@ describe "Admin legislation draft versions", :admin do version = create(:legislation_draft_version, body: "Version 1") visit edit_admin_legislation_process_draft_version_path(version.process, version) - click_link "Delete" + click_button "Delete" expect(page).to have_content "Draft deleted successfully" end diff --git a/spec/system/admin/legislation/questions_spec.rb b/spec/system/admin/legislation/questions_spec.rb index ed39d2f55..20c8fb1a7 100644 --- a/spec/system/admin/legislation/questions_spec.rb +++ b/spec/system/admin/legislation/questions_spec.rb @@ -75,7 +75,7 @@ describe "Admin legislation questions", :admin do visit edit_admin_legislation_process_question_path(process, question) - click_link "Delete" + click_button "Delete" expect(page).to have_content "Questions" expect(page).to have_content "Question 1"