Skip to content

Commit 5cdeaf5

Browse files
committed
Prevent errors when destroying not-found records
1 parent 573c830 commit 5cdeaf5

6 files changed

+48
-6
lines changed

app/controllers/measure_categories_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def destroy
4646
def set_and_authorize_measure_category
4747
@measure_category = policy_scope(base_object).find(params[:id])
4848
authorize @measure_category
49+
rescue ActiveRecord::RecordNotFound
50+
raise unless action_name == "destroy"
51+
head :no_content
4952
end
5053

5154
def base_object

app/controllers/recommendation_categories_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def destroy
4545
def set_and_authorize_recommendation_category
4646
@recommendation_category = policy_scope(base_object).find(params[:id])
4747
authorize @recommendation_category
48+
rescue ActiveRecord::RecordNotFound
49+
raise unless action_name == "destroy"
50+
head :no_content
4851
end
4952

5053
def base_object

app/controllers/user_categories_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def destroy
4545
def set_and_authorize_user_category
4646
@user_category = policy_scope(base_object).find(params[:id])
4747
authorize @user_category
48+
rescue ActiveRecord::RecordNotFound
49+
raise unless action_name == "destroy"
50+
head :no_content
4851
end
4952

5053
def base_object

spec/controllers/measure_categories_controller_spec.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797

9898
context "when user signed in" do
9999
let(:guest) { FactoryBot.create(:user) }
100-
let(:user) { FactoryBot.create(:user, :manager) }
100+
let(:manager) { FactoryBot.create(:user, :manager) }
101101
let(:contributor) { FactoryBot.create(:user, :contributor) }
102102

103103
it "will not allow a guest to delete a measure_category" do
@@ -111,9 +111,20 @@
111111
end
112112

113113
it "will allow a manager to delete a measure_category" do
114-
sign_in user
114+
sign_in manager
115115
expect(subject).to be_no_content
116116
end
117+
118+
context "when the measure_category does not exist" do
119+
let(:measure_category) do
120+
{id: -1}
121+
end
122+
123+
it "returns the same response as a successful deletion" do
124+
sign_in manager
125+
expect(subject).to be_no_content
126+
end
127+
end
117128
end
118129
end
119130
end

spec/controllers/recommendation_categories_controller_spec.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696

9797
context "when user signed in" do
9898
let(:guest) { FactoryBot.create(:user) }
99-
let(:user) { FactoryBot.create(:user, :manager) }
99+
let(:manager) { FactoryBot.create(:user, :manager) }
100100
let(:contributor) { FactoryBot.create(:user, :contributor) }
101101

102102
it "will not allow a guest to delete a recommendation_category" do
@@ -110,9 +110,20 @@
110110
end
111111

112112
it "will allow a manager to delete a recommendation_category" do
113-
sign_in user
113+
sign_in manager
114114
expect(subject).to be_no_content
115115
end
116+
117+
context "when the recommendation_category does not exist" do
118+
let(:recommendation_category) do
119+
{id: -1}
120+
end
121+
122+
it "returns the same response as a successful deletion" do
123+
sign_in manager
124+
expect(subject).to be_no_content
125+
end
126+
end
116127
end
117128
end
118129
end

spec/controllers/user_categories_controller_spec.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
context "when user signed in" do
106106
let(:guest) { FactoryBot.create(:user) }
107-
let(:user) { FactoryBot.create(:user, :manager) }
107+
let(:manager) { FactoryBot.create(:user, :manager) }
108108
let(:contributor) { FactoryBot.create(:user, :contributor) }
109109

110110
it "will not allow a guest to delete a user_category" do
@@ -118,9 +118,20 @@
118118
end
119119

120120
it "will allow a manager to delete a user_category" do
121-
sign_in user
121+
sign_in manager
122122
expect(subject).to be_no_content
123123
end
124+
125+
context "when the user_category does not exist" do
126+
let(:user_category) do
127+
{id: -1}
128+
end
129+
130+
it "returns the same response as a successful deletion" do
131+
sign_in manager
132+
expect(subject).to be_no_content
133+
end
134+
end
124135
end
125136
end
126137
end

0 commit comments

Comments
 (0)