From 45f73aa1baff8e995ade7f58c7988f95f1e6b00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Mon, 16 Oct 2017 12:22:54 +0200 Subject: [PATCH 1/3] Excluded officer shift dates from each task date range --- app/helpers/shifts_helper.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/helpers/shifts_helper.rb b/app/helpers/shifts_helper.rb index dc9f97a91..ec1b342c4 100644 --- a/app/helpers/shifts_helper.rb +++ b/app/helpers/shifts_helper.rb @@ -1,15 +1,15 @@ module ShiftsHelper def shift_vote_collection_dates(polls) - date_options((start_date(polls)..end_date(polls))) + date_options((start_date(polls)..end_date(polls)), 0) end def shift_recount_scrutiny_dates(polls) - date_options(polls.map(&:ends_at).map(&:to_date).sort.inject([]) { |total, date| total << (date..date + 1.week).to_a }.flatten.uniq) + date_options(polls.map(&:ends_at).map(&:to_date).sort.inject([]) { |total, date| total << (date..date + 1.week).to_a }.flatten.uniq, 1) end - def date_options(dates) - dates.map { |date| [l(date, format: :long), l(date)] } + def date_options(dates, task_id) + dates.reject { |date| officer_shifts(task_id).include?(date) }.map { |date| [l(date, format: :long), l(date)] } end def start_date(polls) @@ -24,4 +24,9 @@ module ShiftsHelper officers.collect { |officer| [officer.name, officer.id] } end + private + + def officer_shifts(task_id) + @officer.shifts.where(task: task_id).map(&:date) + end end From a6d533a76750c86aa2a7ab623bcf01a5a9b8891c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Mon, 16 Oct 2017 13:47:10 +0200 Subject: [PATCH 2/3] Helper improvements --- app/helpers/shifts_helper.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/helpers/shifts_helper.rb b/app/helpers/shifts_helper.rb index ec1b342c4..f8cfe8cd4 100644 --- a/app/helpers/shifts_helper.rb +++ b/app/helpers/shifts_helper.rb @@ -1,15 +1,19 @@ module ShiftsHelper def shift_vote_collection_dates(polls) - date_options((start_date(polls)..end_date(polls)), 0) + date_options((start_date(polls)..end_date(polls)), Poll::Shift.tasks[:vote_collection]) end def shift_recount_scrutiny_dates(polls) - date_options(polls.map(&:ends_at).map(&:to_date).sort.inject([]) { |total, date| total << (date..date + 1.week).to_a }.flatten.uniq, 1) + date_options(polls.map(&:ends_at).map(&:to_date).sort.inject([]) { |total, date| total << (date..date + 1.week).to_a }.flatten.uniq, Poll::Shift.tasks[:recount_scrutiny]) end def date_options(dates, task_id) - dates.reject { |date| officer_shifts(task_id).include?(date) }.map { |date| [l(date, format: :long), l(date)] } + valid_dates(dates, task_id).map { |date| [l(date, format: :long), l(date)] } + end + + def valid_dates(dates, task_id) + dates.reject { |date| officer_shifts(task_id).include?(date) } end def start_date(polls) From 7d22b386b947baaca82982fe45526671df69c8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Mon, 16 Oct 2017 16:08:15 +0200 Subject: [PATCH 3/3] Added tests --- spec/factories.rb | 8 +++++++ spec/features/admin/poll/shifts_spec.rb | 31 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/spec/factories.rb b/spec/factories.rb index 69aa92c62..ba7c910ba 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -531,6 +531,14 @@ FactoryGirl.define do association :booth, factory: :poll_booth association :officer, factory: :poll_officer date Date.current + + trait :vote_collection_task do + task 0 + end + + trait :recount_scrutiny_task do + task 1 + end end factory :poll_voter, class: 'Poll::Voter' do diff --git a/spec/features/admin/poll/shifts_spec.rb b/spec/features/admin/poll/shifts_spec.rb index 2ac290216..4864d63d8 100644 --- a/spec/features/admin/poll/shifts_spec.rb +++ b/spec/features/admin/poll/shifts_spec.rb @@ -91,6 +91,37 @@ feature 'Admin shifts' do end end + scenario "Vote Collection Shift and Recount & Scrutiny Shift don't include already assigned dates to officer", :js do + poll = create(:poll, :current) + booth = create(:poll_booth) + assignment = create(:poll_booth_assignment, poll: poll, booth: booth) + officer = create(:poll_officer) + + shift1 = create(:poll_shift, :vote_collection_task, officer: officer, booth: booth, date: Time.zone.today) + shift2 = create(:poll_shift, :recount_scrutiny_task, officer: officer, booth: booth, date: Time.zone.tomorrow) + + vote_collection_dates = (poll.starts_at.to_date..poll.ends_at.to_date).to_a + .reject { |date| date == Time.zone.today } + .map { |date| I18n.l(date, format: :long) } + recount_scrutiny_dates = (poll.ends_at.to_date..poll.ends_at.to_date + 1.week).to_a + .reject { |date| date == Time.zone.tomorrow } + .map { |date| I18n.l(date, format: :long) } + + visit available_admin_booths_path + + within("#booth_#{booth.id}") do + click_link "Manage shifts" + end + + fill_in "search", with: officer.email + click_button "Search" + click_link "Edit shifts" + + expect(page).to have_select('shift_date_vote_collection_date', options: ["Select day", *vote_collection_dates]) + select "Recount & Scrutiny", from: 'shift_task' + expect(page).to have_select('shift_date_recount_scrutiny_date', options: ["Select day", *recount_scrutiny_dates]) + end + scenario "Error on create", :js do poll = create(:poll, :current) booth = create(:poll_booth)