Skip to content

Commit da1f8b6

Browse files
authored
fix: destroy record with restriction on children (#632)
close #630
1 parent eec1873 commit da1f8b6

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

app/controllers/forest_liana/resources_controller.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,20 @@ def update
129129

130130
def destroy
131131
forest_authorize!('delete', forest_user, @resource)
132-
begin
132+
begin
133133
collection_name = ForestLiana.name_for(@resource)
134134
scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, forest_user, collection_name, params[:timezone])
135135

136136
unless scoped_records.exists?(params[:id])
137137
return render serializer: nil, json: { status: 404 }, status: :not_found
138138
end
139139

140-
scoped_records.destroy(params[:id])
141-
142-
head :no_content
140+
if scoped_records.destroy(params[:id])
141+
head :no_content
142+
else
143+
restrict_error = ActiveRecord::DeleteRestrictionError.new
144+
render json: { errors: [{ status: :bad_request, detail: restrict_error.message }] }, status: :bad_request
145+
end
143146
rescue => error
144147
FOREST_REPORTER.report error
145148
FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
@@ -151,9 +154,14 @@ def destroy_bulk
151154
forest_authorize!('delete', forest_user, @resource)
152155
begin
153156
ids = ForestLiana::ResourcesGetter.get_ids_from_request(params, forest_user)
154-
@resource.destroy(ids) if ids&.any?
155-
156-
head :no_content
157+
@resource.transaction do
158+
ids.each do |id|
159+
record = @resource.find(id)
160+
record.destroy!
161+
end
162+
end
163+
rescue ActiveRecord::RecordNotDestroyed => error
164+
render json: { errors: [{ status: :bad_request, detail: error.message }] }, status: :bad_request
157165
rescue => error
158166
FOREST_REPORTER.report error
159167
FOREST_LOGGER.error "Records Destroy error: #{error}\n#{format_stacktrace(error)}"

0 commit comments

Comments
 (0)