Ability to delete geozones from admin dashboard
This commit is contained in:
@@ -29,6 +29,30 @@ class Admin::GeozonesController < Admin::BaseController
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
# Check that in none of the other associated models talbes a record exists
|
||||
# referencing this geozone
|
||||
safe_to_destroy = true
|
||||
|
||||
# safe_to_destroy &= Proposal.where(geozone: @geozone).empty?
|
||||
# safe_to_destroy &= Debate.where(geozone: @geozone).empty?
|
||||
# safe_to_destroy &= SpendingProposal.where(geozone: @geozone).empty?
|
||||
# safe_to_destroy &= User.where(geozone: @geozone).empty?
|
||||
|
||||
Geozone.reflect_on_all_associations.each do |association|
|
||||
attached_model = association.klass
|
||||
safe_to_destroy &= attached_model.where(geozone: @geozone).empty?
|
||||
end
|
||||
|
||||
if safe_to_destroy
|
||||
@geozone.destroy
|
||||
redirect_to admin_geozones_url, notice: t('admin.geozones.delete.success')
|
||||
else
|
||||
redirect_to admin_geozones_path, flash: { error: t('admin.geozones.delete.error') }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def geozone_params
|
||||
|
||||
Reference in New Issue
Block a user