Skip to content

Commit ca3abff

Browse files
committed
Allow admins to delete feedback
1 parent 2214b81 commit ca3abff

5 files changed

Lines changed: 57 additions & 3 deletions

File tree

app/controllers/feedbacks_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class FeedbacksController < ApplicationController
2-
before_action :require_editor
2+
before_action :require_editor, except: :admin_destroy
3+
before_action :require_admin, only: :admin_destroy
34

45
def create
56
@feedback = current_editor.given_feedbacks.build(feedback_params)
@@ -20,6 +21,12 @@ def destroy
2021
redirect_to reviewer_path(reviewer_id), notice: "Feedback deleted"
2122
end
2223

24+
def admin_destroy
25+
feedback = Feedback.find(params[:id])
26+
reviewer_id = feedback.user_id
27+
feedback.destroy
28+
redirect_to user_path(reviewer_id), notice: "Feedback deleted"
29+
end
2330

2431
private
2532

app/views/users/show.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
<td class="max-w-fit text-center">
137137
by: <%= github_link(feedback.editor&.github) %>
138138
<%= " | ".html_safe + link_to("Reference", feedback.link, target: "_blank", title: "Go to review") if feedback.link.present? %>
139+
<%= " | ".html_safe + link_to("Delete", admin_destroy_feedback_path(feedback), data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to delete this feedback?" }) %>
139140
</td>
140141
</tr>
141142
<% end %>

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
get :search, on: :collection
3636
end
3737

38-
resources :feedbacks, only: [:create, :destroy]
38+
resources :feedbacks, only: [:create, :destroy] do
39+
delete :admin_destroy, on: :member
40+
end
3941

4042
get '/join', to: 'home#reviewer_signup', as: :reviewer_signup
4143
get '/lookup', to: 'home#no_reviewer_signup', as: :no_reviewer_signup

spec/system/admin_dashboard_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@
265265
expect(page).to have_content("Domains:\nTrees, Forests")
266266
expect(page).to have_content("Programming languages:\nJulia")
267267
end
268+
269+
scenario "delete feedback" do
270+
editor = create(:editor)
271+
feedback = create(:feedback, user: @user, editor: editor, rating: "positive", comment: "A very nice comment")
272+
273+
visit user_path(@user)
274+
275+
within("#feedback-#{feedback.id}") do
276+
expect(page).to have_content("A very nice comment")
277+
expect(page).to have_content("Delete")
278+
click_link "Delete"
279+
end
280+
281+
expect(page).to have_content("Feedback deleted")
282+
expect(page).to_not have_content("A very nice comment")
283+
284+
visit user_path(@user)
285+
expect(page).to have_content("There's no feedback for this reviewer yet.")
286+
end
268287
end
269288

270289
describe "Show user page" do

spec/system/feedback_spec.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@user.areas << create(:area, name: "Plant Science")
2020
end
2121

22-
scenario "Is only available to editors" do
22+
scenario "Is only visible to editors" do
2323
visit reviewer_path(@user)
2424
expect(page).to have_current_path(root_path)
2525
expect(page).to_not have_content(@user.complete_name)
@@ -100,4 +100,29 @@
100100
end
101101
end
102102

103+
describe "Admins" do
104+
scenario "are the only ones that can delete anything" do
105+
feedback = create(:feedback, user: create(:user), editor: create(:editor), comment: "Feedback test")
106+
107+
expect {
108+
delete admin_destroy_feedback_path(feedback)
109+
}.to raise_exception(ActionController::RoutingError, "Not Found")
110+
111+
login_as create(:user)
112+
expect {
113+
delete admin_destroy_feedback_path(feedback)
114+
}.to raise_exception(ActionController::RoutingError, "Not Found")
115+
116+
login_as create(:editor)
117+
expect {
118+
delete admin_destroy_feedback_path(feedback)
119+
}.to raise_exception(ActionController::RoutingError, "Not Found")
120+
121+
login_as create(:admin)
122+
expect {
123+
delete admin_destroy_feedback_path(feedback)
124+
}.to change { Feedback.count }.by(-1)
125+
end
126+
end
127+
103128
end

0 commit comments

Comments
 (0)