Default to delete method for the destroy action

This is consistent with what Rails does.
This commit is contained in:
Javi Martín
2024-03-06 15:07:14 +01:00
parent 54977116e7
commit 251a5fb6e9
12 changed files with 42 additions and 14 deletions

View File

@@ -11,7 +11,11 @@ class Admin::ActionComponent < ApplicationComponent
private private
def button? def button?
options[:method] && options[:method] != :get method && method != :get
end
def method
options[:method] || (:delete if action == :destroy)
end end
def text def text
@@ -30,6 +34,7 @@ class Admin::ActionComponent < ApplicationComponent
def html_options def html_options
{ {
method: method,
class: html_class, class: html_class,
id: (dom_id(record, action) if record.respond_to?(:to_key)), id: (dom_id(record, action) if record.respond_to?(:to_key)),
"aria-describedby": describedby, "aria-describedby": describedby,
@@ -38,7 +43,7 @@ class Admin::ActionComponent < ApplicationComponent
confirm: confirmation_text, confirm: confirmation_text,
disable_with: (text if button?) disable_with: (text if button?)
} }
}.merge(options.except(:"aria-describedby", :"aria-label", :class, :confirm, :path, :text)) }.merge(options.except(:"aria-describedby", :"aria-label", :class, :confirm, :method, :path, :text))
end end
def html_class def html_class

View File

@@ -39,7 +39,6 @@ class Admin::Budgets::ActionsComponent < ApplicationComponent
def destroy_action def destroy_action
action(:destroy, action(:destroy,
text: t("admin.budgets.edit.delete"), text: t("admin.budgets.edit.delete"),
method: :delete,
confirm: t("admin.budgets.actions.confirm.destroy"), confirm: t("admin.budgets.actions.confirm.destroy"),
disabled: budget.investments.any? || budget.poll) disabled: budget.investments.any? || budget.poll)
end end

View File

@@ -4,7 +4,7 @@
<div class="groups-actions"> <div class="groups-actions">
<%= action(:edit, group, "aria-label": true) %> <%= action(:edit, group, "aria-label": true) %>
<%= action(:destroy, group, method: :delete, confirm: true, "aria-label": true) %> <%= action(:destroy, group, confirm: true, "aria-label": true) %>
<%= action(:new, <%= action(:new,
group, group,
text: t("admin.budgets.show.add_heading"), text: t("admin.budgets.show.add_heading"),

View File

@@ -12,8 +12,7 @@
<%= render Admin::ActionComponent.new( <%= render Admin::ActionComponent.new(
:destroy, :destroy,
image, image,
path: admin_site_customization_image_path(image), path: admin_site_customization_image_path(image)
method: :delete
) %> ) %>
<% end %> <% end %>
</div> </div>

View File

@@ -6,7 +6,6 @@
:destroy, :destroy,
@page, @page,
text: t("admin.site_customization.pages.index.delete"), text: t("admin.site_customization.pages.index.delete"),
method: :delete,
confirm: true, confirm: true,
class: "delete" class: "delete"
) %> ) %>

View File

@@ -38,7 +38,6 @@ class Admin::TableActionsComponent < ApplicationComponent
def destroy_options def destroy_options
{ {
method: :delete,
confirm: options[:destroy_confirmation] || true confirm: options[:destroy_confirmation] || true
}.merge(options[:destroy_options] || {}) }.merge(options[:destroy_options] || {})
end end

View File

@@ -18,7 +18,6 @@
<%= render Admin::ActionComponent.new( <%= render Admin::ActionComponent.new(
:destroy, :destroy,
@draft_version, @draft_version,
method: :delete,
confirm: true, confirm: true,
class: "button hollow alert" class: "button hollow alert"
) %> ) %>

View File

@@ -18,7 +18,6 @@
<%= render Admin::ActionComponent.new( <%= render Admin::ActionComponent.new(
:destroy, :destroy,
@question, @question,
method: :delete,
confirm: true, confirm: true,
class: "button hollow alert" class: "button hollow alert"
) %> ) %>

View File

@@ -23,7 +23,6 @@
@user, @user,
text: t("admin.officials.edit.destroy"), text: t("admin.officials.edit.destroy"),
path: admin_official_path(@user), path: admin_official_path(@user),
method: :delete,
class: "delete" class: "delete"
) %> ) %>
<% end %> <% end %>

View File

@@ -30,7 +30,6 @@
:destroy, :destroy,
image, image,
text: t("images.remove_image"), text: t("images.remove_image"),
method: :delete,
confirm: t("admin.actions.confirm_action", action: t("images.remove_image"), name: image.title), confirm: t("admin.actions.confirm_action", action: t("images.remove_image"), name: image.title),
class: "delete float-right" class: "delete float-right"
) %> ) %>

View File

@@ -10,7 +10,6 @@
text: t("admin.site_customization.content_blocks.index.delete"), text: t("admin.site_customization.content_blocks.index.delete"),
path: (@is_heading_content_block ? admin_site_customization_delete_heading_content_block_path(@content_block.id) : admin_site_customization_content_block_path(@content_block)), path: (@is_heading_content_block ? admin_site_customization_delete_heading_content_block_path(@content_block.id) : admin_site_customization_content_block_path(@content_block)),
confirm: true, confirm: true,
method: :delete,
class: "delete float-right" class: "delete float-right"
) %> ) %>

View File

@@ -1,6 +1,38 @@
require "rails_helper" require "rails_helper"
describe Admin::ActionComponent do describe Admin::ActionComponent do
describe "method" do
it "is not included by default for most actions" do
render_inline Admin::ActionComponent.new(:create, double, path: "/")
expect(page).to have_link count: 1
expect(page).not_to have_button
expect(page).not_to have_css "[data-method]"
end
it "is included in the link when the method is get" do
render_inline Admin::ActionComponent.new(:create, double, path: "/", method: :get)
expect(page).to have_link count: 1
expect(page).to have_css "a[data-method='get']"
expect(page).not_to have_button
end
it "defaults to :delete for the destroy action" do
render_inline Admin::ActionComponent.new(:destroy, double, path: "/")
expect(page).to have_css "input[name='_method']", visible: :all, count: 1
expect(page).to have_css "input[name='_method'][value='delete']", visible: :hidden
end
it "can be overriden for the destroy action" do
render_inline Admin::ActionComponent.new(:destroy, double, path: "/", method: :put)
expect(page).to have_css "input[name='_method']", visible: :all, count: 1
expect(page).to have_css "input[name='_method'][value='put']", visible: :hidden
end
end
describe "HTML class" do describe "HTML class" do
it "includes an HTML class for the action by default" do it "includes an HTML class for the action by default" do
render_inline Admin::ActionComponent.new(:edit, double, path: "/") render_inline Admin::ActionComponent.new(:edit, double, path: "/")
@@ -159,7 +191,7 @@ describe Admin::ActionComponent do
render_inline Admin::ActionComponent.new(:destroy, record, path: "/", confirm: true) render_inline Admin::ActionComponent.new(:destroy, record, path: "/", confirm: true)
expect(page).to have_link count: 1 expect(page).to have_button count: 1
expect(page).to have_css "[data-confirm='#{text}']" expect(page).to have_css "[data-confirm='#{text}']"
end end
end end