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

View File

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

View File

@@ -4,7 +4,7 @@
<div class="groups-actions">
<%= 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,
group,
text: t("admin.budgets.show.add_heading"),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -10,7 +10,6 @@
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)),
confirm: true,
method: :delete,
class: "delete float-right"
) %>

View File

@@ -1,6 +1,38 @@
require "rails_helper"
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
it "includes an HTML class for the action by default" do
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)
expect(page).to have_link count: 1
expect(page).to have_button count: 1
expect(page).to have_css "[data-confirm='#{text}']"
end
end