mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-29 18:52:22 +01:00
server-side protection against last category deletion
This commit is contained in:
parent
bb6295c487
commit
812d7a3c05
@ -31,8 +31,11 @@ class API::CategoriesController < API::ApiController
|
||||
|
||||
def destroy
|
||||
authorize Category
|
||||
@category.destroy
|
||||
head :no_content
|
||||
if @category.safe_destroy
|
||||
head :no_content
|
||||
else
|
||||
render json: @category.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -1,3 +1,11 @@
|
||||
class Category < ActiveRecord::Base
|
||||
has_and_belongs_to_many :events, join_table: :events_categories, dependent: :destroy
|
||||
|
||||
def safe_destroy
|
||||
if count > 1
|
||||
destroy
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user