Skip to content

Commit f6bc154

Browse files
JuanVqzrishijain
andauthored
[MKT-575] Prevent accidental deletion in points (#310)
* [MKT-575] Prevent accidental deletion in points https://ombulabs.atlassian.net/browse/MKT-575 * added spec for checking destroy when title does not match --------- Co-authored-by: rishijain <[email protected]>
1 parent 1be286f commit f6bc154

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed

app/assets/stylesheets/stories.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
padding-bottom: 1.3em;
6161
}
6262

63+
.modal strong {
64+
font-weight: bold;
65+
}
66+
6367
.new_story,
6468
.edit_story {
6569
display: grid;

app/controllers/projects_controller.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class ProjectsController < ApplicationController
22
before_action :authenticate_user!
3-
before_action :find_project, only: [:show, :edit, :update, :sort, :sort_stories, :destroy, :new_sub_project, :toggle_archive, :toggle_locked]
3+
before_action :find_project, only: [:show, :edit, :update, :sort, :sort_stories, :destroy, :new_sub_project, :toggle_archive, :toggle_locked, :open_delete_modal]
44
before_action :ensure_unarchived!, only: [:edit, :new_sub_project, :update]
55

66
def index
@@ -70,9 +70,14 @@ def create
7070
end
7171

7272
def destroy
73-
@project.destroy
7473
respond_to do |format|
75-
format.html { redirect_to projects_path, notice: "Project was successfully destroyed." }
74+
if @project.title.strip.eql?(params.dig(:project, :title)&.strip)
75+
@project.destroy
76+
flash[:success] = "Project was successfully destroyed."
77+
else
78+
flash[:error] = "Make sure you added the correct project's title"
79+
end
80+
format.html { redirect_to projects_path }
7681
end
7782
end
7883

@@ -104,6 +109,10 @@ def new_sub_project
104109
@sub = Project.new(parent_id: @project)
105110
end
106111

112+
# GET /projects/1/open_delete_modal.js
113+
def open_delete_modal
114+
end
115+
107116
private
108117

109118
def find_project
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%= form_with(model: project, method: :delete) do |f| %>
2+
This action cannot be undone.
3+
This will permanently delete the <strong><%= project.title %></strong> project,
4+
stories, and associated estimations.
5+
6+
<div class="field">
7+
<%= f.label :title, raw("Please type <strong>#{project.title}</strong> to confirm.") %>
8+
<%= f.text_field :title, value: "", placeholder: "Project's title", autofocus: true, required: true %>
9+
</div>
10+
11+
<%= f.submit "I understand the consequences, delete this project", class: "button magenta" %>
12+
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(function(){
2+
showModal("Are you absolutely sure?", "<%= j(render('delete_form', project: @project)) %>")
3+
})()

app/views/projects/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108

109109
<% if is_unlocked?(@project) %>
110110
<%= link_unless_archived(@project, "Add Sub-Project", project_new_sub_project_path(@project), classes: :green) unless @project.parent_id.present? %>
111-
<%= link_unless_archived(@project, "Delete Project", project_path(@project.id), classes: "delete magenta", method: :delete, remote: true, data_attr: { confirm: 'Are you sure?' }, id: "delete") %>
111+
<%= link_unless_archived(@project, "Delete Project", open_delete_modal_project_path(@project.id), classes: "delete magenta", remote: true) %>
112112
<% end %>
113113

114114
<% unless @project.parent_id %>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
patch :toggle_locked
2525
get :new_clone
2626
post :clone
27+
get :open_delete_modal
2728
end
2829
get :new_sub_project
2930

spec/controllers/projects_controller_spec.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,35 @@
124124
describe "#destroy" do
125125
it "deletes the project" do
126126
expect {
127-
delete :destroy, params: {id: project.id}
127+
delete :destroy, params: {id: project.id, project: {title: project.title}}
128+
}.to change(Project, :count).by(-1)
129+
end
130+
131+
it "deletes stripped project's title" do
132+
project.update(title: " foo bar ")
133+
expect {
134+
delete :destroy, params: {id: project.id, project: {title: "foo bar"}}
135+
}.to change(Project, :count).by(-1)
136+
end
137+
138+
it "deletes stripped project's params" do
139+
project.update(title: "foo bar")
140+
expect {
141+
delete :destroy, params: {id: project.id, project: {title: "foo bar "}}
128142
}.to change(Project, :count).by(-1)
129143
end
144+
145+
it "does not delete the project" do
146+
expect {
147+
delete :destroy, params: {id: project.id}
148+
}.not_to change(Project, :count)
149+
end
150+
151+
it "does not delete the project when the title does not match" do
152+
expect {
153+
delete :destroy, params: {id: project.id, project: {title: "random title"}}
154+
}.not_to change(Project, :count)
155+
end
130156
end
131157

132158
describe "#show" do

spec/features/projects_manage_spec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,27 @@
5050
end
5151

5252
context "when the project is unarchived" do
53-
it "allows me to delete a project", js: false do
53+
it "does not delete a project" do
54+
project.update(title: "Awesome Project's Title")
5455
visit project_path(id: project.id)
56+
5557
click_link "Delete Project"
56-
expect(Project.count).to eq 0
58+
expect(page).to have_content "Are you absolutely sure?"
59+
fill_in "project_title", with: "Random Project's Title"
60+
click_button "I understand the consequences, delete this project"
61+
62+
expect(page).to have_content "Make sure you added the correct project's title"
5763
end
5864

5965
it "allows me to delete a project" do
6066
visit project_path(id: project.id)
61-
accept_confirm do
62-
click_link "Delete Project"
63-
end
64-
expect(page).not_to have_content "Delete Project"
65-
expect(Project.count).to eq 0
67+
68+
click_link "Delete Project"
69+
expect(page).to have_content "Are you absolutely sure?"
70+
fill_in "project_title", with: project.title
71+
click_button "I understand the consequences, delete this project"
72+
73+
expect(page).to have_content "Project was successfully destroyed."
6674
end
6775

6876
it "allows editing the project's title inline" do

0 commit comments

Comments
 (0)