diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index 8489c3cf0..81759ee0f 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -3,10 +3,26 @@ class Poll belongs_to :booth belongs_to :poll + before_destroy :destroy_poll_shifts, only: :destroy + has_many :officer_assignments, class_name: "Poll::OfficerAssignment", dependent: :destroy has_many :officers, through: :officer_assignments has_many :voters has_many :partial_results has_many :recounts + + def shifts? + shifts.empty? ? false : true + end + + private + + def shifts + Poll::Shift.where(booth_id: booth_id, officer_id: officer_assignments.pluck(:officer_id), date: officer_assignments.pluck(:date)) + end + + def destroy_poll_shifts + shifts.destroy_all + end end end diff --git a/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb b/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb index d78f7be8b..d7896446f 100644 --- a/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb +++ b/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb @@ -14,7 +14,8 @@ method: :delete, remote: true, title: t("admin.booth_assignments.manage.actions.unassign"), - class: "button hollow alert" %> + class: "button hollow alert", + data: (booth_assignment.shifts? ? {confirm: "#{t("admin.poll_booth_assignments.alert.shifts")}"} : nil) if !@poll.expired? %> <% else %> @@ -26,6 +27,6 @@ method: :post, remote: true, title: t("admin.booth_assignments.manage.actions.assign"), - class: "button" %> + class: "button" if !@poll.expired? %> <% end %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 1f68ad66d..4edad349f 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -558,6 +558,8 @@ en: assign: Assign booth unassign: Unassign booth poll_booth_assignments: + alert: + shifts: "There are shifts associated to this booth. If you remove the booth assignment, the shifts will be also deleted. Continue?" flash: destroy: "Booth not assigned anymore" create: "Booth assigned" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 7f90192bc..44cfa7600 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -558,6 +558,8 @@ es: assign: Asignar urna unassign: Asignar urna poll_booth_assignments: + alert: + shifts: "Hay turnos asignados para esta urna. Si la desasignas, esos turnos se eliminarán. ¿Deseas continuar?" flash: destroy: "Urna desasignada" create: "Urna asignada" diff --git a/spec/features/admin/poll/booth_assigments_spec.rb b/spec/features/admin/poll/booth_assigments_spec.rb index cceec1a3e..29e1eccde 100644 --- a/spec/features/admin/poll/booth_assigments_spec.rb +++ b/spec/features/admin/poll/booth_assigments_spec.rb @@ -106,6 +106,41 @@ feature 'Admin booths assignments' do expect(page).to have_content 'There are no booths assigned to this poll.' expect(page).not_to have_content booth.name end + + scenario 'Unassing booth whith associated shifts', :js do + assignment = create(:poll_booth_assignment, poll: poll, booth: booth) + officer = create(:poll_officer) + create(:poll_officer_assignment, officer: officer, booth_assignment: assignment) + create(:poll_shift, booth: booth, officer: officer) + + visit manage_admin_poll_booth_assignments_path(poll) + + within("#poll_booth_#{booth.id}") do + expect(page).to have_content(booth.name) + expect(page).to have_content "Assigned" + + click_link 'Unassign booth' + + expect(page).to have_content "Unassigned" + expect(page).not_to have_content "Assigned" + expect(page).to have_link "Assign booth" + end + end + + scenario "Cannot unassing booth if poll is expired" do + poll_expired = create(:poll, :expired) + create(:poll_booth_assignment, poll: poll_expired, booth: booth) + + visit manage_admin_poll_booth_assignments_path(poll_expired) + + within("#poll_booth_#{booth.id}") do + expect(page).to have_content(booth.name) + expect(page).to have_content "Assigned" + + expect(page).not_to have_link 'Unassign booth' + end + + end end feature 'Show' do diff --git a/spec/models/poll/booth_assignment_spec.rb b/spec/models/poll/booth_assignment_spec.rb new file mode 100644 index 000000000..8663e3872 --- /dev/null +++ b/spec/models/poll/booth_assignment_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +describe :booth_assignment do + let(:poll){create(:poll)} + let(:booth){create(:poll_booth)} + let(:booth1){create(:poll_booth)} + + it "should check if there are shifts" do + assignment_with_shifts = create(:poll_booth_assignment, poll: poll, booth: booth) + assignment_without_shifts = create(:poll_booth_assignment, poll: poll, booth: booth1) + officer = create(:poll_officer) + create(:poll_officer_assignment, officer: officer, booth_assignment: assignment_with_shifts) + create(:poll_shift, booth: booth, officer: officer) + + expect(assignment_with_shifts.shifts?).to eq(true) + expect(assignment_without_shifts.shifts?).to eq(false) + end + + it "should delete shifts associated to booth assignments" do + assignment = create(:poll_booth_assignment, poll: poll, booth: booth) + officer = create(:poll_officer) + create(:poll_officer_assignment, officer: officer, booth_assignment: assignment) + create(:poll_shift, booth: booth, officer: officer) + + assignment.destroy + + expect(Poll::Shift.all.count).to eq(0) + end +end \ No newline at end of file