Ability to delete geozones from admin dashboard

This commit is contained in:
Alberto Miedes Garcés
2016-12-08 13:16:42 +01:00
parent 4a58c786c8
commit 93e79228a7
5 changed files with 95 additions and 0 deletions

View File

@@ -29,6 +29,30 @@ class Admin::GeozonesController < Admin::BaseController
render :edit render :edit
end end
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 private
def geozone_params def geozone_params

View File

@@ -22,6 +22,9 @@
<td> <td>
<%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %> <%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %>
</td> </td>
<td>
<%= link_to t("admin.geozones.index.delete"), admin_geozone_path(geozone), method: :delete, class: 'button hollow alert' %>
</td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

@@ -272,6 +272,8 @@ en:
index: index:
title: Geozone title: Geozone
create: Create geozone create: Create geozone
edit: Edit
delete: Delete
geozone: geozone:
name: Name name: Name
external_code: External code external_code: External code
@@ -289,6 +291,9 @@ en:
new: new:
back: Go back back: Go back
creating: Create district creating: Create district
delete:
success: Geozone successfully deleted
error: This geozone can't be deleted since there are elements attached to it
stats: stats:
show: show:
stats_title: Stats stats_title: Stats

View File

@@ -270,6 +270,8 @@ es:
index: index:
title: Distritos title: Distritos
create: Crear un distrito create: Crear un distrito
edit: Editar
delete: Borrar
geozone: geozone:
name: Nombre name: Nombre
external_code: Código externo external_code: Código externo
@@ -287,6 +289,9 @@ es:
new: new:
back: Volver back: Volver
creating: Crear distrito creating: Crear distrito
delete:
success: Distrito borrado correctamente
error: No se puede borrar el distrito porque ya tiene elementos asociados
stats: stats:
show: show:
stats_title: Estadísticas stats_title: Estadísticas

View File

@@ -73,5 +73,63 @@ feature 'Admin geozones' do
click_button 'Save changes' click_button 'Save changes'
expect(proposal.geozone.name).to eq('New geozone name') 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
end end