Skip to content

Commit

Permalink
Permanently delete scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
noracato committed Oct 29, 2024
1 parent 45648f6 commit 11ad9ac
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/components/saved_scenarios/nav_item/component.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= link_to @path, class: "flex p-2 pl-6 w-full transition text-sm #{css_classes}" do %>
<%= link_to @path, class: "flex p-2 pl-6 w-full transition text-sm #{css_classes}", data: @data do %>
<span class="mr-5"><%= heroicon @icon, options: { class: 'w-5 h-5' } %></span>
<span><%= @title %></span>
<% end %>
1 change: 1 addition & 0 deletions app/components/saved_scenarios/nav_item/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Component < ApplicationComponent
option :icon
option :active, default: proc { false }
option :static, default: proc { false }
option :data, default: proc { {} }

def css_classes
if @active
Expand Down
23 changes: 9 additions & 14 deletions app/controllers/saved_scenarios_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class SavedScenariosController < ApplicationController
load_resource only: %i[discard undiscard publish unpublish]
load_resource only: %i[discard undiscard publish unpublish confirm_destroy]
load_and_authorize_resource only: %i[show new create edit update destroy]

before_action :require_user, only: %i[index]
Expand All @@ -8,7 +8,7 @@ class SavedScenariosController < ApplicationController
authorize!(:update, @saved_scenario)
end

before_action only: %i[discard undiscard] do
before_action only: %i[discard undiscard confirm_destroy] do
authorize!(:destroy, @saved_scenario)
end

Expand Down Expand Up @@ -64,20 +64,15 @@ def update
end
end

def confirm_destroy
render :confirm_destroy, layout: 'application'
end

# DELETE /saved_scenarios/1 or /saved_scenarios/1.json
def destroy
@saved_scenario.destroy!

respond_to do |format|
format.html do
redirect_to(
saved_scenarios_path,
status: :see_other,
notice: "Scenario was successfully destroyed."
)
end
format.json { head :no_content }
end
@saved_scenario.destroy
flash.notice = t('scenario.trash.deleted_flash')
redirect_to discarded_path
end

# Makes a scenario public.
Expand Down
3 changes: 3 additions & 0 deletions app/views/discarded/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
- content_for :menu_title, t('trash.title')
- content_for :block_right, "FILTERS"

- if notice_message
= render(NoticeBanner::Component.new(text: notice_message))

- if @resources.present?
- @resources.each do |resource|
- if resource.is_a?(SavedScenario)
Expand Down
3 changes: 0 additions & 3 deletions app/views/featured_scenarios/confirm_destroy.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<%# We have to do the hovercard tutorial! With using turbo to insert the confirm into a modal /popup box
also check other tutorials for confirm destroy! %>

<%= render(ModalComponent.new(title: t('.title'))) do |modal| %>
<%= turbo_frame_tag :modal do %>
<%= form_for(@featured_scenario, url: saved_scenario_feature_path(@featured_scenario.saved_scenario), html: { method: :delete, data: { turbo: false } }) do |f| %>
Expand Down
1 change: 0 additions & 1 deletion app/views/featured_scenarios/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@
= submit_tag t('scenario.save'), class: 'button w-1/4 bg-midnight-900 text-midnight-200'
= link_to t('scenario.discard_changes'), saved_scenario_path(@featured_scenario.saved_scenario), class: 'button w-1/4 ml-auto mr-0'
- if @featured_scenario.persisted?
-# = button_to 'Destroy', saved_scenario_feature_path(@featured_scenario.saved_scenario), method: :delete, form: { data: { turbo_confirm: 'Are you sure?' } }
= link_to t('featured_scenario.unfeature'), confirm_destroy_saved_scenario_feature_path(@featured_scenario.saved_scenario), data: { turbo_frame: 'modal' } , class: 'button w-1/4 ml-auto mr-0'
14 changes: 14 additions & 0 deletions app/views/saved_scenarios/_block_right_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@
))%>
<% end %>
<% end %>
<% if saved_scenario.collaborator?(current_user) && saved_scenario.discarded? %>
<%= render(Hovercard::Component.new(
path: '',
text: t("scenario_bar.destroy.description")
)) do %>
<%= render(SavedScenarios::NavItem::Component.new(
path: confirm_destroy_saved_scenario_path(saved_scenario),
title: t("scenario_bar.destroy.title"),
icon: 'x-mark',
static: true,
data: { turbo_frame: 'modal' }
))%>
<% end %>
<% end %>
<% end %>
24 changes: 24 additions & 0 deletions app/views/saved_scenarios/confirm_destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%= render(ModalComponent.new(title: t('.title'))) do |modal| %>
<%= turbo_frame_tag :modal do %>
<%= form_for(@saved_scenario, url: saved_scenario_path(@saved_scenario), html: { method: :delete, data: { turbo: false } }) do |f| %>
<div class="flex-col mb-10">
<div class="flex text-red-600 mb-5">
<span class="mr-5"><%= heroicon 'exclamation-triangle', options: { class: 'w-6 h-6' } %></span>
<span class="text-base">
<%= t('.warning_header') %>
</span>
</div>
<p class="mb-2">
<%= t('.warning') %>
</p>
<p class="text-sm text-gray-400">
<%= t('.irreversible') %>
</p>
</div>
<div class="mt-6 pb-2 flex items-center gap-2">
<%= button_tag t('.submit'), class: button_classes("text-base", size: :lg, color: :warning) %>
<%= modal.close_link(t('identity.cancel'), saved_scenario_path(@saved_scenario), class: button_classes("text-base", size: :lg)) %>
</div>
<% end %>
<% end %>
<% end %>
11 changes: 11 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ en:
description:
true: This scenario has been featured on a home page. Edit settings here.
false: Feature this scenario on the home page
destroy:
title: Delete permanently
description: Permanently delete this scenario.
scenario:
succesful_update: Your scenario was succesfully updated
title: Title
Expand All @@ -105,6 +108,14 @@ en:

saved_scenarios:
empty: You don't have any scenarios.
confirm_destroy:
title: Permanently deleting scenario
warning_header: You are permanently deleting your scenario
warning: |
Deleting this scenario will remove all data including history,
any grants for access, title and description.
irreversible: This action is irreversible
submit: Delete this scenario
featured_scenarios:
confirm_destroy:
title: Unfeaturing scenario
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
put :unpublish
put :discard
put :undiscard
get :confirm_destroy
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/requests/saved_scenarios_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@
}.to change(SavedScenario, :count).by(-1)
end

it "redirects to the saved_scenarios list" do
it "redirects to the trash can" do
delete saved_scenario_url(user_scenario)
expect(response).to redirect_to(saved_scenarios_url)
expect(response).to redirect_to(discarded_url)
end
end

Expand Down

0 comments on commit 11ad9ac

Please sign in to comment.