From f7f9fc15a58a9e26a6a700903398aaa5bd4a1d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 31 Aug 2018 05:47:11 +0200 Subject: [PATCH] Move `todays_booths_for_officer` to the model This way we can easily add a test which will fail if by accident we change the method to use `Date.today`. Until now using `Date.today` would only fail if we ran specs in a time zone with a different date. --- app/controllers/officing/base_controller.rb | 6 +----- app/controllers/officing/booth_controller.rb | 2 +- app/models/poll/officer.rb | 4 ++++ spec/models/poll/officer_spec.rb | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/controllers/officing/base_controller.rb b/app/controllers/officing/base_controller.rb index 729490ba4..58ba3c8b2 100644 --- a/app/controllers/officing/base_controller.rb +++ b/app/controllers/officing/base_controller.rb @@ -28,7 +28,7 @@ class Officing::BaseController < ApplicationController def verify_booth return unless current_booth.blank? - booths = todays_booths_for_officer(current_user.poll_officer) + booths = current_user.poll_officer.todays_booths case booths.count when 0 redirect_to officing_root_path @@ -43,8 +43,4 @@ class Officing::BaseController < ApplicationController Poll::Booth.where(id: session[:booth_id]).first end - def todays_booths_for_officer(officer) - officer.officer_assignments.by_date(Date.today).map(&:booth).uniq - end - end diff --git a/app/controllers/officing/booth_controller.rb b/app/controllers/officing/booth_controller.rb index 68e33dd61..584c92de0 100644 --- a/app/controllers/officing/booth_controller.rb +++ b/app/controllers/officing/booth_controller.rb @@ -3,7 +3,7 @@ class Officing::BoothController < Officing::BaseController before_action :verify_officer_assignment def new - @booths = todays_booths_for_officer(current_user.poll_officer) + @booths = current_user.poll_officer.todays_booths end def create diff --git a/app/models/poll/officer.rb b/app/models/poll/officer.rb index 384202455..ef96cfa91 100644 --- a/app/models/poll/officer.rb +++ b/app/models/poll/officer.rb @@ -23,5 +23,9 @@ class Poll sort {|x, y| y.ends_at <=> x.ends_at} end + def todays_booths + officer_assignments.by_date(Date.current).map(&:booth).uniq + end + end end diff --git a/spec/models/poll/officer_spec.rb b/spec/models/poll/officer_spec.rb index 1cbad6f5d..4cdc581b1 100644 --- a/spec/models/poll/officer_spec.rb +++ b/spec/models/poll/officer_spec.rb @@ -121,4 +121,21 @@ describe Poll::Officer do expect(assigned_polls.last).to eq(poll_3) end end + + describe "todays_booths" do + let(:officer) { create(:poll_officer) } + + it "returns booths for the application's time zone date", :with_different_time_zone do + assignment_with_local_time_zone = create(:poll_officer_assignment, + date: Date.today, + officer: officer) + + assignment_with_application_time_zone = create(:poll_officer_assignment, + date: Date.current, + officer: officer) + + expect(officer.todays_booths).to include(assignment_with_application_time_zone.booth) + expect(officer.todays_booths).not_to include(assignment_with_local_time_zone.booth) + end + end end