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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
# Check that in none of the other associated models tables a record exists
|
if @geozone.safe_to_destroy?
|
||||||
# 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
|
|
||||||
@geozone.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
|
else
|
||||||
redirect_to admin_geozones_path, flash: { error: t('admin.geozones.delete.error') }
|
redirect_to admin_geozones_path, flash: { error: t('admin.geozones.delete.error') }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,4 +9,9 @@ class Geozone < ActiveRecord::Base
|
|||||||
Geozone.pluck(:name)
|
Geozone.pluck(:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def safe_to_destroy?
|
||||||
|
Geozone.reflect_on_all_associations(:has_many).all? do |association|
|
||||||
|
association.klass.where(geozone: self).empty?
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user