Skip to content

Commit

Permalink
Two-step record deletion without JS
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Jun 25, 2020
1 parent f161ed2 commit 1674ced
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
12 changes: 9 additions & 3 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ def index
end

def show
render locals: {
page: Administrate::Page::Show.new(dashboard, requested_resource),
}
if params[:destroy]
render :destroy, locals: {
page: Administrate::Page::Destroy.new(dashboard, requested_resource),
}
else
render locals: {
page: Administrate::Page::Show.new(dashboard, requested_resource),
}
end
end

def new
Expand Down
4 changes: 3 additions & 1 deletion app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def sanitized_order_params(page, current_field_name)
association_params = collection_names.map do |assoc_name|
{ assoc_name => %i[order direction page per_page] }
end
params.permit(:search, :id, :page, :per_page, association_params)
params.
permit(:destroy, :search, :id, :page, :per_page, association_params).
except(:destroy)
end

def clear_search_params
Expand Down
10 changes: 5 additions & 5 deletions app/views/administrate/application/_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ to display a collection of resources in an HTML table.
<% if valid_action? :destroy, collection_presenter.resource_name %>
<td><%= link_to(
t("administrate.actions.destroy"),
[namespace, resource],
class: "text-color-red",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) if show_action? :destroy, resource %></td>
polymorphic_path([namespace, resource], destroy: true),
html_options: {
class: "text-color-red",
},
) if show_action?(:destroy, resource) %></td>
<% end %>
</tr>
<% end %>
Expand Down
42 changes: 42 additions & 0 deletions app/views/administrate/application/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%#
# Destroy
This view is the template for the destroy page.
It renders a confirmation form, asking the user
whether they actually intend to destroy the
requested record.
## Local variables:
- `page`:
An instance of [Administrate::Page::Destroy][1].
Contains methods for accessing the resource to be displayed on the page,
helping compose a confirmation message.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Destroy
%>

<% content_for(:title) { t("administrate.actions.destroy_resource", name: page.page_title) } %>

<header class="main-content__header" role="banner">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>

<div>
<%= link_to(
t("administrate.actions.show_resource", name: page.page_title),
[namespace, page.resource],
class: "button",
) if valid_action?(:show) && show_action?(:show, page.resource) %>
</div>
</header>

<section class="main-content__body">
<%= form_for([namespace, page.resource], method: :delete, html: { class: "form" }) do |f| %>
<div class="form-actions">
<p><%= t("administrate.actions.confirm") %></p>
<%= f.submit t("administrate.actions.destroy") %>
</div>
<% end %>
</section>
1 change: 1 addition & 0 deletions config/locales/administrate.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ en:
edit_resource: Edit %{name}
show_resource: Show %{name}
new_resource: New %{name}
destroy_resource: Destroy %{name}
back: Back
controller:
create:
Expand Down
1 change: 1 addition & 0 deletions lib/administrate/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "administrate/page/form"
require "administrate/page/show"
require "administrate/page/collection"
require "administrate/page/destroy"
require "administrate/order"
require "administrate/resource_resolver"
require "administrate/search"
Expand Down
8 changes: 8 additions & 0 deletions lib/administrate/page/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative "base"

module Administrate
module Page
class Destroy < Page::Show
end
end
end

0 comments on commit 1674ced

Please sign in to comment.