1
0
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:
Sylvain 2016-06-30 09:57:40 +02:00
parent bb6295c487
commit 812d7a3c05
2 changed files with 13 additions and 2 deletions

View File

@ -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

View File

@ -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