Skip to content

Commit

Permalink
Add ability to delete an existing control
Browse files Browse the repository at this point in the history
  • Loading branch information
csutter committed Jul 8, 2024
1 parent b2b5092 commit ad83eea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/controls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def update
end
end

def destroy
@control = Control.find(params[:id])

if @control.destroy
redirect_to controls_path, notice: "Control deleted successfully"
else
redirect_to @control, alert: "Control could not be deleted. Try again later."
end
end

private

def control_params
Expand Down
1 change: 1 addition & 0 deletions app/views/controls/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
href: edit_control_path(@control),
inline_layout: true
} %>
<%= delete_button "Delete control", control_path(@control), is_inline: true %>
</div>
22 changes: 22 additions & 0 deletions spec/system/controls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
and_i_can_see_what_errors_i_need_to_fix
end

scenario "Deleting an existing control" do
given_a_control

when_i_go_to_view_the_control
and_i_choose_to_delete_it

then_the_control_has_been_deleted_locally
and_i_am_notified_of_the_deletion
end

def given_several_controls
@control1 = create(:control, display_name: "Control 1")
@control2 = create(:control, display_name: "Control 2")
Expand Down Expand Up @@ -97,6 +107,10 @@ def and_i_submit_the_form_with_updated_details
click_on "Save"
end

def and_i_choose_to_delete_it
click_on "Delete"
end

def then_the_control_has_not_been_created
expect(Control.count).to eq(0)
end
Expand Down Expand Up @@ -133,4 +147,12 @@ def and_i_can_see_the_new_details
def then_the_control_has_not_been_updated
expect(@control.reload.display_name).to eq("Control")
end

def then_the_control_has_been_deleted_locally
expect(Control.count).to eq(0)
end

def and_i_am_notified_of_the_deletion
expect(page).to have_content("Control deleted successfully")
end
end

0 comments on commit ad83eea

Please sign in to comment.