From 93e79228a7780967cb25f91a4083bca93c55aa62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Thu, 8 Dec 2016 13:16:42 +0100 Subject: [PATCH] Ability to delete geozones from admin dashboard --- app/controllers/admin/geozones_controller.rb | 24 ++++++++ app/views/admin/geozones/index.html.erb | 3 + config/locales/admin.en.yml | 5 ++ config/locales/admin.es.yml | 5 ++ spec/features/admin/geozones_spec.rb | 58 ++++++++++++++++++++ 5 files changed, 95 insertions(+) diff --git a/app/controllers/admin/geozones_controller.rb b/app/controllers/admin/geozones_controller.rb index eb8119a91..b87fec242 100644 --- a/app/controllers/admin/geozones_controller.rb +++ b/app/controllers/admin/geozones_controller.rb @@ -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 diff --git a/app/views/admin/geozones/index.html.erb b/app/views/admin/geozones/index.html.erb index 9cedca9c6..69d9334d5 100644 --- a/app/views/admin/geozones/index.html.erb +++ b/app/views/admin/geozones/index.html.erb @@ -22,6 +22,9 @@ <%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %> + + <%= link_to t("admin.geozones.index.delete"), admin_geozone_path(geozone), method: :delete, class: 'button hollow alert' %> + <% end %> diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index ecba0e851..2187c458c 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -272,6 +272,8 @@ en: index: title: Geozone create: Create geozone + edit: Edit + delete: Delete geozone: name: Name external_code: External code @@ -289,6 +291,9 @@ en: new: back: Go back creating: Create district + delete: + success: Geozone successfully deleted + error: This geozone can't be deleted since there are elements attached to it stats: show: stats_title: Stats diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 3d14a717f..7ba79aad3 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -270,6 +270,8 @@ es: index: title: Distritos create: Crear un distrito + edit: Editar + delete: Borrar geozone: name: Nombre external_code: Código externo @@ -287,6 +289,9 @@ es: new: back: Volver creating: Crear distrito + delete: + success: Distrito borrado correctamente + error: No se puede borrar el distrito porque ya tiene elementos asociados stats: show: stats_title: Estadísticas diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index d571f6616..c7cc605c8 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -73,5 +73,63 @@ feature 'Admin geozones' do click_button 'Save changes' expect(proposal.geozone.name).to eq('New geozone name') + + scenario 'Delete geozone with no associated elements' do + target_geozone = create(:geozone, name: 'Delete me!') + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).not_to have_content('Delete me!') + expect(Geozone.find_by_id(target_geozone.id)).to be_nil + end + + scenario 'Delete geozone with associated proposal' do + target_geozone = create(:geozone, name: 'Delete me!') + proposal = create(:proposal, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(proposal.reload.geozone).to eq(target_geozone) + end + + scenario 'Delete geozone with associated spending proposal' do + target_geozone = create(:geozone, name: 'Delete me!') + spending_proposal = create(:spending_proposal, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(spending_proposal.reload.geozone).to eq(target_geozone) + end + + scenario 'Delete geozone with associated debate' do + target_geozone = create(:geozone, name: 'Delete me!') + debate = create(:debate, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(debate.reload.geozone).to eq(target_geozone) + end + + scenario 'Delete geozone with associated user' do + target_geozone = create(:geozone, name: 'Delete me!') + user = create(:user, geozone: target_geozone) + + visit admin_geozones_path + + within("#geozone_#{target_geozone.id}") { click_link 'Delete' } + + expect(page).to have_content('Delete me!') + expect(user.reload.geozone).to eq(target_geozone) end end