Deleting a booth shift with recounts or partial results.

Show a flash message that it's not possible to delete booth shifts
when they have associated recounts or partial results. Before an
execption was raised.
This commit is contained in:
Julian Herrero
2019-02-06 10:55:43 +01:00
parent e76f483850
commit 5f4a369606
6 changed files with 70 additions and 3 deletions

View File

@@ -26,9 +26,14 @@ class Admin::Poll::ShiftsController < Admin::Poll::BaseController
def destroy
@shift = Poll::Shift.find(params[:id])
@shift.destroy
notice = t("admin.poll_shifts.flash.destroy")
redirect_to new_admin_booth_shift_path(@booth), notice: notice
if @shift.unable_to_destroy?
alert = t("admin.poll_shifts.flash.unable_to_destroy")
redirect_to new_admin_booth_shift_path(@booth), alert: alert
else
@shift.destroy
notice = t("admin.poll_shifts.flash.destroy")
redirect_to new_admin_booth_shift_path(@booth), notice: notice
end
end
def search_officers

View File

@@ -15,6 +15,10 @@ class Poll
shifts.empty? ? false : true
end
def unable_to_destroy?
(partial_results.count + recounts.count).positive?
end
private
def shifts

View File

@@ -35,6 +35,10 @@ class Poll
end
end
def unable_to_destroy?
booth.booth_assignments.map(&:unable_to_destroy?).any?
end
def destroy_officer_assignments
Poll::OfficerAssignment.where(booth_assignment: booth.booth_assignments,
officer: officer,

View File

@@ -872,6 +872,7 @@ en:
flash:
create: "Shift added"
destroy: "Shift removed"
unable_to_destroy: "Shifts with associated results or recounts cannot be deleted"
date_missing: "A date must be selected"
vote_collection: Collect Votes
recount_scrutiny: Recount & Scrutiny

View File

@@ -872,6 +872,7 @@ es:
flash:
create: "Añadido turno de presidente de mesa"
destroy: "Eliminado turno de presidente de mesa"
unable_to_destroy: "No se pueden eliminar turnos que tienen resultados o recuentos asociados"
date_missing: "Debe seleccionarse una fecha"
vote_collection: Recoger Votos
recount_scrutiny: Recuento & Escrutinio

View File

@@ -165,6 +165,58 @@ feature 'Admin shifts' do
expect(page).to have_css(".shift", count: 0)
end
scenario "Try to destroy with associated recount" do
assignment = create(:poll_booth_assignment)
officer_assignment = create(:poll_officer_assignment, booth_assignment: assignment)
create(:poll_recount, booth_assignment: assignment, officer_assignment: officer_assignment)
officer = officer_assignment.officer
booth = assignment.booth
shift = create(:poll_shift, officer: officer, booth: booth)
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 1)
within("#shift_#{shift.id}") do
click_link "Remove"
end
expect(page).not_to have_content "Shift removed"
expect(page).to have_content "Shifts with associated results or recounts cannot be deleted"
expect(page).to have_css(".shift", count: 1)
end
scenario "try to destroy with associated partial results" do
assignment = create(:poll_booth_assignment)
officer_assignment = create(:poll_officer_assignment, booth_assignment: assignment)
create(:poll_partial_result,
booth_assignment: assignment,
officer_assignment: officer_assignment)
officer = officer_assignment.officer
booth = assignment.booth
shift = create(:poll_shift, officer: officer, booth: booth)
visit available_admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
expect(page).to have_css(".shift", count: 1)
within("#shift_#{shift.id}") do
click_link "Remove"
end
expect(page).not_to have_content "Shift removed"
expect(page).to have_content "Shifts with associated results or recounts cannot be deleted"
expect(page).to have_css(".shift", count: 1)
end
scenario "Destroy an officer" do
poll = create(:poll)
booth = create(:poll_booth)