Move geozone 'safe to destroy' check from controller to model

This commit is contained in:
Alberto Miedes Garcés
2016-12-13 12:30:47 +01:00
parent 60da2a875b
commit c22a80b48f
2 changed files with 7 additions and 11 deletions

View File

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

View File

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