Ability to delete geozones from admin dashboard
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -22,6 +22,9 @@
|
||||
<td>
|
||||
<%= link_to t("admin.geozones.index.edit"), edit_admin_geozone_path(geozone), class: 'edit-banner button hollow' %>
|
||||
</td>
|
||||
<td>
|
||||
<%= link_to t("admin.geozones.index.delete"), admin_geozone_path(geozone), method: :delete, class: 'button hollow alert' %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user