From b854d992484e196e724db33b3547616ea8682a69 Mon Sep 17 00:00:00 2001 From: iagirre Date: Fri, 27 Oct 2017 13:10:10 +0200 Subject: [PATCH 1/5] Assign/Unassign button for expired polls is disabled --- .../admin/poll/booth_assignments/_booth_assignment.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..8fb944e9c 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,7 @@ method: :delete, remote: true, title: t("admin.booth_assignments.manage.actions.unassign"), - class: "button hollow alert" %> + class: "button hollow alert #{@poll.expired? ? 'disabled' : ''}" %> <% else %> @@ -26,6 +26,6 @@ method: :post, remote: true, title: t("admin.booth_assignments.manage.actions.assign"), - class: "button" %> + class: "button #{@poll.expired? ? 'disabled' : ''}" %> <% end %> From 89425f50efdd3312afb76916b209832a25302ffd Mon Sep 17 00:00:00 2001 From: iagirre Date: Fri, 27 Oct 2017 14:57:32 +0200 Subject: [PATCH 2/5] First steps to destroy shifts when booth_assignments are destroyed --- app/models/poll.rb | 4 ++++ app/models/poll/booth_assignment.rb | 13 +++++++++++++ .../booth_assignments/_booth_assignment.html.erb | 3 ++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/models/poll.rb b/app/models/poll.rb index 27125d8a1..f08f9bdcd 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -90,4 +90,8 @@ class Poll < ActiveRecord::Base end end + def shifts_in_booth(booth_id) + officer_assignments.where(booth_assignment_id: booth_assignments.where(booth_id: booth_id).pluck(:id)).pluck(:officer_id).uniq + end + end diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index 8489c3cf0..a1d0c0d39 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -8,5 +8,18 @@ class Poll has_many :voters has_many :partial_results has_many :recounts + + before_destroy :destroy_poll_shifts + + def has_shifts? + + end + + private + + def destroy_poll_shifts +# officers = poll.officers_in_booth(booth.id) +# Poll::Shift.where(officer_id: officers, booth_id: booth.id) + 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 8fb944e9c..b1ab3885e 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 #{@poll.expired? ? 'disabled' : ''}" %> + class: "button hollow alert #{@poll.expired? ? 'disabled' : ''}", + data: (booth_assignment.has_shifts? ? {confirm: "Are you sure?"} : nil) %> <% else %> From 1077e25b2ba9fcff63cefe64537fcc5c711f6551 Mon Sep 17 00:00:00 2001 From: iagirre Date: Mon, 30 Oct 2017 12:36:07 +0100 Subject: [PATCH 3/5] Shifts are destroyed when a booths is unassigned. An alert appears if there are shifts, but it doesn't if there aren't. --- app/models/poll.rb | 4 ---- app/models/poll/booth_assignment.rb | 15 +++++++++------ .../booth_assignments/_booth_assignment.html.erb | 2 +- config/locales/en/admin.yml | 2 ++ config/locales/es/admin.yml | 2 ++ 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/models/poll.rb b/app/models/poll.rb index f08f9bdcd..27125d8a1 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -90,8 +90,4 @@ class Poll < ActiveRecord::Base end end - def shifts_in_booth(booth_id) - officer_assignments.where(booth_assignment_id: booth_assignments.where(booth_id: booth_id).pluck(:id)).pluck(:officer_id).uniq - end - end diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index a1d0c0d39..81759ee0f 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -3,23 +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 - before_destroy :destroy_poll_shifts - - def has_shifts? - + 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 -# officers = poll.officers_in_booth(booth.id) -# Poll::Shift.where(officer_id: officers, booth_id: booth.id) + 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 b1ab3885e..b3b711f9f 100644 --- a/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb +++ b/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb @@ -15,7 +15,7 @@ remote: true, title: t("admin.booth_assignments.manage.actions.unassign"), class: "button hollow alert #{@poll.expired? ? 'disabled' : ''}", - data: (booth_assignment.has_shifts? ? {confirm: "Are you sure?"} : nil) %> + data: (booth_assignment.shifts? ? {confirm: "#{t("admin.poll_booth_assignments.alert.shifts")}"} : nil) %> <% else %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index a2062f8ce..c36ba0b94 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -544,6 +544,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 1c7d0fa74..cc3683bf4 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -544,6 +544,8 @@ es: assign: Assign booth unassign: Unassign booth 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" From 4b8a471d3842e409250e678d599c42a7e3da8d24 Mon Sep 17 00:00:00 2001 From: iagirre Date: Mon, 30 Oct 2017 16:50:25 +0100 Subject: [PATCH 4/5] Specs added to test the functionality and some UI modified to make test pass --- .../_booth_assignment.html.erb | 6 +-- .../admin/poll/booth_assigments_spec.rb | 37 ++++++++++++++++++- spec/models/poll/booth_assignment_spec.rb | 29 +++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 spec/models/poll/booth_assignment_spec.rb 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 b3b711f9f..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,8 +14,8 @@ method: :delete, remote: true, title: t("admin.booth_assignments.manage.actions.unassign"), - class: "button hollow alert #{@poll.expired? ? 'disabled' : ''}", - data: (booth_assignment.shifts? ? {confirm: "#{t("admin.poll_booth_assignments.alert.shifts")}"} : nil) %> + class: "button hollow alert", + data: (booth_assignment.shifts? ? {confirm: "#{t("admin.poll_booth_assignments.alert.shifts")}"} : nil) if !@poll.expired? %> <% else %> @@ -27,6 +27,6 @@ method: :post, remote: true, title: t("admin.booth_assignments.manage.actions.assign"), - class: "button #{@poll.expired? ? 'disabled' : ''}" %> + class: "button" if !@poll.expired? %> <% end %> diff --git a/spec/features/admin/poll/booth_assigments_spec.rb b/spec/features/admin/poll/booth_assigments_spec.rb index cceec1a3e..52f5d89fa 100644 --- a/spec/features/admin/poll/booth_assigments_spec.rb +++ b/spec/features/admin/poll/booth_assigments_spec.rb @@ -106,9 +106,44 @@ 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 + xfeature 'Show' do scenario 'Lists all assigned poll officers' do poll = create(:poll) booth = create(:poll_booth) 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 From 4819d1b74e770ad367c5c79060a64d808b09609e Mon Sep 17 00:00:00 2001 From: iagirre Date: Mon, 30 Oct 2017 16:55:13 +0100 Subject: [PATCH 5/5] Enabled one test disabled to run in local --- spec/features/admin/poll/booth_assigments_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/admin/poll/booth_assigments_spec.rb b/spec/features/admin/poll/booth_assigments_spec.rb index 52f5d89fa..29e1eccde 100644 --- a/spec/features/admin/poll/booth_assigments_spec.rb +++ b/spec/features/admin/poll/booth_assigments_spec.rb @@ -143,7 +143,7 @@ feature 'Admin booths assignments' do end end - xfeature 'Show' do + feature 'Show' do scenario 'Lists all assigned poll officers' do poll = create(:poll) booth = create(:poll_booth)