Skip to content

Commit

Permalink
deleting experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Jan 31, 2025
1 parent 1528ae7 commit dbbcf2f
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 1 deletion.
25 changes: 25 additions & 0 deletions app/controllers/ui/experiments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ def create
end
end

def delete
find_experiment!
return unless authorize_experiment! :destroy?, :delete_experiment_forbidden
@title = I18n.t(:delete_experiment_title, name: @experiment.name)
add_breadcrumbs(
[I18n.t(:show_user_title, owner: helpers.possessive(@experiment.owner, current_user)), ui_user_path(@experiment.owner.username)],
[I18n.t(:show_experiment_title, name: @experiment.name), ui_experiment_path(@experiment.id)],
[I18n.t(:edit_experiment_title, name: @experiment.name), edit_ui_experiment_path(@experiment.id)],
[@title, delete_ui_experiment_path(@experiment.id)]
)
end

def destroy
find_experiment!
return unless authorize_experiment! :destroy?, :delete_experiment_forbidden
if @experiment.name != params[:name]
flash[:alert] = I18n.t(:delete_experiment_wrong_name)
redirect_to delete_ui_experiment_path(@experiment.id)
return
end
@experiment.destroy!
flash[:success] = I18n.t(:delete_experiment_success)
redirect_to ui_user_path(current_user.username)
end

private

def find_experiment!
Expand Down
2 changes: 1 addition & 1 deletion app/views/ui/experiments/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p class="mb-0">
<% if authorize? experiment, :update? %>
<%= link_to(t(:show_experiment_edit_cta, name: experiment.name), edit_ui_experiment_path(experiment), class: "btn btn-dark mb-3 mb-lg-0 me-md-2 w-100 w-md-auto") %>
<%= link_to(t(:show_experiment_edit_cta, name: experiment.name), edit_ui_experiment_path(experiment), class: "btn btn-dark mb-0 me-md-2 w-100 w-md-auto") %>
<% end %>
</p>
7 changes: 7 additions & 0 deletions app/views/ui/experiments/delete.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= bootstrap_form_tag url: ui_experiment_path(@experiment.id), method: :delete do |f| %>
<p><%= t(:delete_experiment_warning_html, name: @experiment.name) %></p>
<%= f.text_field :name, label: t(:delete_experiment_name_label) %>
<div class="mt-4">
<%= f.primary t(:delete_experiment_submit), class: "btn btn-danger w-100 w-md-auto" %>
</div>
<% end %>
4 changes: 4 additions & 0 deletions app/views/ui/experiments/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
<%= form.primary t(:edit_experiment_submit), class: "btn btn-primary w-100 w-md-auto" %>
</div>
<% end %>
<% if authorize? @experiment, :destroy? %>
<h2 class="mt-5 mb-3"><%= t(:edit_experiment_other_actions_subhead) %></h2>
<div><%= link_to t(:edit_experiment_delete_experiment_submit), delete_ui_experiment_path(@experiment.id), class: "btn btn-danger w-100 w-md-auto" %></div>
<% end %>
4 changes: 4 additions & 0 deletions config/locales/controllers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ en:
new_experiment_title: "Create an experiment"
new_experiment_success: "Your experiment has been created!"
new_experiment_failure: "Some errors prevented us from registering your experiment. Please check below and try again!"
delete_experiment_title: "Delete experiment: %{name}"
delete_experiment_forbidden: "You are not allowed to delete that experiment!"
delete_experiment_success: "The experiment has been deleted!"
delete_experiment_wrong_name: "That experiment name did not match! Please try again."


5 changes: 5 additions & 0 deletions config/locales/views/experiments/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ en:
experiment_form_search_devices_label: "Search for and add a kit"
experiment_form_is_test_label: "Mark as a test experiment"
new_experiment_submit: "Create"
edit_experiment_other_actions_subhead: "Other actions"
edit_experiment_delete_experiment_submit: "Delete this experiment"
delete_experiment_warning_html: "🚨<strong>Warning!</strong> This will permanently delete the experiment <strong>%{name}</strong>.🚨"
delete_experiment_name_label: "To confirm, type the experiment name below:"
delete_experiment_submit: "I understand, delete the experiment"
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
resources :experiments, as: "experiments" do
member do
get :readings
get :delete
end
end

Expand Down
80 changes: 80 additions & 0 deletions spec/controllers/ui/experiments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,84 @@
end
end
end

describe "delete" do
context "when the experiment's owner is logged in" do
it "displays the delete experiment form" do
get :delete, params: { id: experiment.id }, session: { user_id: user.try(:id) }
expect(response).to have_http_status(:success)
expect(response).to render_template(:delete)
end
end

context "when a different user is logged in" do
let(:owner) { create(:user) }
it "redirects to the ui users page" do
get :delete, params: { id: experiment.id }, session: { user_id: user.try(:id) }
expect(response).to redirect_to(ui_user_path(user.username))
expect(flash[:alert]).to be_present
end
end

context "when no user is logged in" do
let(:user) { nil }
it "redirects to the login page" do
get :delete, params: { id: experiment.id }, session: { user_id: user.try(:id) }
expect(response).to redirect_to(login_path)
expect(flash[:alert]).to be_present
end
end
end

describe "destroy" do
context "when the experiment's owner is logged in" do
context "when the correct experiment name is provided" do
it "destroys the experimentr, and redirects to the user's profile" do
expect_any_instance_of(Experiment).to receive(:destroy!)
delete :destroy,
params: { id: experiment.id, name: experiment.name },
session: { user_id: user.try(:id) }
expect(response).to redirect_to(ui_user_path(user.username))
expect(flash[:success]).to be_present
end
end

context "when an incorrect experiment name is provided" do
it "does not destroy the experiment and redirects to the delete page" do
expect_any_instance_of(Experiment).not_to receive(:destroy!)
delete :destroy,
params: { id: experiment.id, name: "a wrong experiment name" },
session: { user_id: user.try(:id) }
expect(response).to redirect_to(delete_ui_experiment_path(experiment.id))
expect(flash[:alert]).to be_present
end
end
end

context "when a different user is logged in" do
let(:owner) { create(:user) }

it "does not destroy the experiment and redirects to the ui users page" do
expect_any_instance_of(Experiment).not_to receive(:destroy!)
delete :destroy,
params: { id: experiment.id, name: experiment.name },
session: { user_id: user.try(:id) }
expect(response).to redirect_to(ui_user_path(user.username))
expect(flash[:alert]).to be_present
end
end

context "when no user is logged in" do
let(:user) { nil }

it "does not destroy the user and redirets to the login page" do
expect_any_instance_of(Experiment).not_to receive(:destroy!)
delete :destroy,
params: { id: experiment.id, name: experiment.name },
session: { user_id: user.try(:id) }
expect(response).to redirect_to(login_path)
expect(flash[:alert]).to be_present
end
end
end
end
25 changes: 25 additions & 0 deletions spec/features/experiment_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,29 @@
expect(page).to have_current_path(ui_experiment_path(Experiment.last.id))
expect(page).to have_content("new experiment name")
end


scenario "User deletes an experiment" do
password = "password123"
username = "username"
device_name = "devicename"
experiment_name = "experimentname"
user = create(:user, username: username, password: password, password_confirmation: password)
device = create(:device, name: device_name, owner: user)
experiment = create(:experiment, owner: user, devices: [device], name: experiment_name)
visit "/login"
fill_in "Username or email", with: user.email
fill_in "Password", with: password
click_on "Sign into your account"
click_on experiment_name
click_on "Edit experiment settings"
click_on "Delete this experiment"
expect(page).to have_current_path(delete_ui_experiment_path(experiment.id))
fill_in "To confirm, type the experiment name below:", with: experiment_name
click_on "I understand, delete the experiment"
expect(page).to have_current_path(ui_user_path(username))
expect(page).to have_content("The experiment has been deleted!")
expect(page).not_to have_content(experiment_name)
expect(Experiment.where(id: experiment.id).first).to be_nil
end
end

0 comments on commit dbbcf2f

Please sign in to comment.