diff --git a/app/helpers/shifts_helper.rb b/app/helpers/shifts_helper.rb index f8cfe8cd4..3773014ac 100644 --- a/app/helpers/shifts_helper.rb +++ b/app/helpers/shifts_helper.rb @@ -5,7 +5,11 @@ module ShiftsHelper 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, Poll::Shift.tasks[:recount_scrutiny]) + dates = polls.map(&:ends_at).map(&:to_date).sort.inject([]) do |total, date| + initial_date = date < Date.current ? Date.current : date + total << (initial_date..date + Poll::RECOUNT_DURATION).to_a + end + date_options(dates.flatten.uniq, Poll::Shift.tasks[:recount_scrutiny]) end def date_options(dates, task_id) @@ -17,7 +21,8 @@ module ShiftsHelper end def start_date(polls) - polls.map(&:starts_at).min.to_date + start_date = polls.map(&:starts_at).min.to_date + start_date < Date.current ? Date.current : start_date end def end_date(polls) diff --git a/app/models/poll.rb b/app/models/poll.rb index 8675ce49a..5c65b688f 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -2,6 +2,9 @@ class Poll < ActiveRecord::Base include Imageable acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases + + RECOUNT_DURATION = 1.week + has_many :booth_assignments, class_name: "Poll::BoothAssignment" has_many :booths, through: :booth_assignments has_many :partial_results, through: :booth_assignments @@ -22,6 +25,7 @@ class Poll < ActiveRecord::Base scope :current, -> { where('starts_at <= ? and ? <= ends_at', Date.current.beginning_of_day, Date.current.beginning_of_day) } scope :incoming, -> { where('? < starts_at', Date.current.beginning_of_day) } scope :expired, -> { where('ends_at < ?', Date.current.beginning_of_day) } + scope :recounting, -> { Poll.where(ends_at: (Date.current.beginning_of_day - RECOUNT_DURATION)..Date.current.beginning_of_day) } scope :published, -> { where('published = ?', true) } scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) } @@ -47,6 +51,10 @@ class Poll < ActiveRecord::Base current + incoming end + def self.current_or_recounting_or_incoming + current + recounting + incoming + end + def answerable_by?(user) user.present? && user.level_two_or_three_verified? && diff --git a/app/views/admin/poll/shifts/_form.html.erb b/app/views/admin/poll/shifts/_form.html.erb index 1a13edf45..4004a949b 100644 --- a/app/views/admin/poll/shifts/_form.html.erb +++ b/app/views/admin/poll/shifts/_form.html.erb @@ -24,12 +24,12 @@
<%= select 'shift[date]', 'vote_collection_date', - options_for_select(shift_vote_collection_dates(@booth.polls)), + options_for_select(shift_vote_collection_dates(@booth.polls.current_or_incoming)), { prompt: t("admin.poll_shifts.new.select_date"), label: false }, class: 'js-shift-vote-collection-dates' %> <%= select 'shift[date]', 'recount_scrutiny_date', - options_for_select(shift_recount_scrutiny_dates(@booth.polls)), + options_for_select(shift_recount_scrutiny_dates(@booth.polls.current_or_recounting_or_incoming)), { prompt: t("admin.poll_shifts.new.select_date"), label: false }, class: 'js-shift-recount-scrutiny-dates', diff --git a/spec/factories.rb b/spec/factories.rb index ba7c910ba..ed1d24fd6 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -489,6 +489,11 @@ FactoryGirl.define do ends_at { 15.days.ago } end + trait :recounting do + starts_at { 1.month.ago } + ends_at { Date.current } + end + trait :published do published true end diff --git a/spec/features/admin/poll/shifts_spec.rb b/spec/features/admin/poll/shifts_spec.rb index 4864d63d8..6f027fad3 100644 --- a/spec/features/admin/poll/shifts_spec.rb +++ b/spec/features/admin/poll/shifts_spec.rb @@ -35,9 +35,10 @@ feature 'Admin shifts' do create(:poll, :incoming) poll = create(:poll, :current) booth = create(:poll_booth) - assignment = create(:poll_booth_assignment, poll: poll, booth: booth) + create(:poll_booth_assignment, poll: poll, booth: booth) + create(:poll_booth_assignment, poll: create(:poll, :expired), booth: booth) officer = create(:poll_officer) - vote_collection_dates = (poll.starts_at.to_date..poll.ends_at.to_date).to_a.map { |date| I18n.l(date, format: :long) } + vote_collection_dates = (Date.current..poll.ends_at.to_date).to_a.map { |date| I18n.l(date, format: :long) } recount_scrutiny_dates = (poll.ends_at.to_date..poll.ends_at.to_date + 1.week).to_a.map { |date| I18n.l(date, format: :long) } visit available_admin_booths_path @@ -52,14 +53,14 @@ feature 'Admin shifts' do expect(page).to have_select('shift_date_vote_collection_date', options: ["Select day", *vote_collection_dates]) expect(page).not_to have_select('shift_date_recount_scrutiny_date') - select I18n.l(poll.starts_at.to_date, format: :long), from: 'shift_date_vote_collection_date' + select I18n.l(Date.current, format: :long), from: 'shift_date_vote_collection_date' click_button "Add shift" expect(page).to have_content "Shift added" within("#shifts") do expect(page).to have_css(".shift", count: 1) - expect(page).to have_content(I18n.l(poll.starts_at.to_date, format: :long)) + expect(page).to have_content(I18n.l(Date.current, format: :long)) expect(page).to have_content("Collect Votes") expect(page).to have_content(officer.name) end diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index 5083d7439..bfb5881f2 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -76,6 +76,38 @@ describe :poll do end end + describe "#recounting" do + it "returns polls in recount & scrutiny phase" do + current = create(:poll, :current) + incoming = create(:poll, :incoming) + expired = create(:poll, :expired) + recounting = create(:poll, :recounting) + + recounting_polls = Poll.recounting + + expect(recounting_polls).to_not include(current) + expect(recounting_polls).to_not include(incoming) + expect(recounting_polls).to_not include(expired) + expect(recounting_polls).to include(recounting) + end + end + + describe "#current_or_recounting_or_incoming" do + it "returns current or recounting or incoming polls" do + current = create(:poll, :current) + incoming = create(:poll, :incoming) + expired = create(:poll, :expired) + recounting = create(:poll, :recounting) + + current_or_recounting_or_incoming = Poll.current_or_recounting_or_incoming + + expect(current_or_recounting_or_incoming).to include(current) + expect(current_or_recounting_or_incoming).to include(recounting) + expect(current_or_recounting_or_incoming).to include(incoming) + expect(current_or_recounting_or_incoming).to_not include(expired) + end + end + describe "answerable_by" do let(:geozone) {create(:geozone) }