diff --git a/app/controllers/officing/base_controller.rb b/app/controllers/officing/base_controller.rb index 7d9bf225d..729490ba4 100644 --- a/app/controllers/officing/base_controller.rb +++ b/app/controllers/officing/base_controller.rb @@ -27,7 +27,14 @@ class Officing::BaseController < ApplicationController end def verify_booth - if current_booth.blank? + return unless current_booth.blank? + booths = todays_booths_for_officer(current_user.poll_officer) + case booths.count + when 0 + redirect_to officing_root_path + when 1 + session[:booth_id] = booths.first.id + else redirect_to new_officing_booth_path end end @@ -36,4 +43,8 @@ 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 383154f97..68e33dd61 100644 --- a/app/controllers/officing/booth_controller.rb +++ b/app/controllers/officing/booth_controller.rb @@ -3,17 +3,11 @@ class Officing::BoothController < Officing::BaseController before_action :verify_officer_assignment def new - load_booths - - if only_one_booth? - set_booth(@booths.first) - redirect_to officing_root_path - end + @booths = todays_booths_for_officer(current_user.poll_officer) end def create - find_booth - set_booth(@booth) + set_booth(Poll::Booth.find(booth_params[:id])) redirect_to officing_root_path end @@ -23,19 +17,6 @@ class Officing::BoothController < Officing::BaseController params.require(:booth).permit(:id) end - def load_booths - officer = current_user.poll_officer - @booths = officer.officer_assignments.by_date(Date.today).map(&:booth).uniq - end - - def only_one_booth? - @booths.count == 1 - end - - def find_booth - @booth = Poll::Booth.find(booth_params[:id]) - end - def set_booth(booth) session[:booth_id] = booth.id end diff --git a/app/controllers/officing/polls_controller.rb b/app/controllers/officing/polls_controller.rb index 69b65ad23..dc806d35f 100644 --- a/app/controllers/officing/polls_controller.rb +++ b/app/controllers/officing/polls_controller.rb @@ -1,4 +1,5 @@ class Officing::PollsController < Officing::BaseController + before_action :verify_booth def index @polls = current_user.poll_officer? ? current_user.poll_officer.voting_days_assigned_polls : [] diff --git a/app/controllers/officing/results_controller.rb b/app/controllers/officing/results_controller.rb index 6d373046b..ff9bf3925 100644 --- a/app/controllers/officing/results_controller.rb +++ b/app/controllers/officing/results_controller.rb @@ -7,7 +7,6 @@ class Officing::ResultsController < Officing::BaseController before_action :load_officer_assignment, only: :create before_action :check_officer_assignment, only: :create before_action :build_results, only: :create - before_action :verify_booth def new diff --git a/spec/features/officing/residence_spec.rb b/spec/features/officing/residence_spec.rb index c4c7784bc..aeae7fb2c 100644 --- a/spec/features/officing/residence_spec.rb +++ b/spec/features/officing/residence_spec.rb @@ -97,20 +97,17 @@ feature "Residence", :with_frozen_time do booth = create(:poll_booth) poll = create(:poll) - ba = create(:poll_booth_assignment, poll: poll, booth: booth ) + ba = create(:poll_booth_assignment, poll: poll, booth: booth) create(:poll_officer_assignment, officer: officer, booth_assignment: ba) + create(:poll_shift, officer: officer, booth: booth, date: Time.zone.today) login_as(officer.user) - # User somehow skips setting session[:booth_id] - # set_officing_booth(booth) - visit new_officing_residence_path within("#officing-booth") do expect(page).to have_content "You are officing the booth located at #{booth.location}." end - visit new_officing_residence_path officing_verify_residence expect(page).to have_content poll.name diff --git a/spec/features/officing/voters_spec.rb b/spec/features/officing/voters_spec.rb index 61b9ea221..1e39ec2d7 100644 --- a/spec/features/officing/voters_spec.rb +++ b/spec/features/officing/voters_spec.rb @@ -17,6 +17,8 @@ feature "Voters" do scenario "Can vote", :js do create(:poll_officer_assignment, officer: officer) + officer_assignment = create(:poll_officer_assignment, officer: officer) + set_officing_booth(officer_assignment.booth) visit new_officing_residence_path officing_verify_residence @@ -38,10 +40,11 @@ feature "Voters" do scenario "Already voted", :js do poll2 = create(:poll, :current) booth_assignment = create(:poll_booth_assignment, poll: poll2, booth: booth) - create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment) + officer_assignment = create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment) user = create(:user, :level_two) voter = create(:poll_voter, poll: poll, user: user) + set_officing_booth(officer_assignment.booth) visit new_officing_voter_path(id: voter.user.id) @@ -74,7 +77,7 @@ feature "Voters" do poll_current = create(:poll, :current) second_booth = create(:poll_booth) booth_assignment = create(:poll_booth_assignment, poll: poll_current, booth: second_booth) - create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment) + officer_assignment = create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment) create(:poll_shift, officer: officer, booth: second_booth, date: Date.current, task: :recount_scrutiny) create(:poll_shift, officer: officer, booth: second_booth, date: Date.tomorrow, task: :vote_collection) @@ -89,6 +92,7 @@ feature "Voters" do booth_assignment = create(:poll_booth_assignment, poll: poll_geozone_restricted_out, booth: booth) create(:poll_officer_assignment, officer: officer, booth_assignment: booth_assignment) + set_officing_booth(officer_assignment.booth) visit new_officing_residence_path officing_verify_residence