Move geozone 'safe to destroy' check from controller to model
This commit is contained in:
@@ -33,18 +33,9 @@ class Admin::GeozonesController < Admin::BaseController
|
||||
end
|
||||
|
||||
def destroy
|
||||
# Check that in none of the other associated models tables a record exists
|
||||
# referencing this geozone
|
||||
safe_to_destroy = true
|
||||
|
||||
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
|
||||
if @geozone.safe_to_destroy?
|
||||
@geozone.destroy
|
||||
redirect_to admin_geozones_url, notice: t('admin.geozones.delete.success')
|
||||
redirect_to admin_geozones_path, notice: t('admin.geozones.delete.success')
|
||||
else
|
||||
redirect_to admin_geozones_path, flash: { error: t('admin.geozones.delete.error') }
|
||||
end
|
||||
|
||||
@@ -9,4 +9,9 @@ class Geozone < ActiveRecord::Base
|
||||
Geozone.pluck(:name)
|
||||
end
|
||||
|
||||
def safe_to_destroy?
|
||||
Geozone.reflect_on_all_associations(:has_many).all? do |association|
|
||||
association.klass.where(geozone: self).empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user