diff --git a/app/controllers/officing/base_controller.rb b/app/controllers/officing/base_controller.rb index 48054ce6a..0e0d02ae6 100644 --- a/app/controllers/officing/base_controller.rb +++ b/app/controllers/officing/base_controller.rb @@ -6,7 +6,29 @@ class Officing::BaseController < ApplicationController skip_authorization_check - def verify_officer - raise CanCan::AccessDenied unless current_user.try(:poll_officer?) - end + private + + def verify_officer + raise CanCan::AccessDenied unless current_user.try(:poll_officer?) + end + + def load_officer_assignment + @officer_assignments ||= current_user.poll_officer. + officer_assignments. + voting_days. + where(date: Time.current.to_date) + end + + def verify_officer_assignment + if @officer_assignments.blank? + redirect_to officing_root_path, notice: t("officing.residence.flash.not_allowed") + end + end + + def verify_booth + if session[:booth_id].blank? + redirect_to new_officing_booth_path + end + end + end diff --git a/app/controllers/officing/booth_controller.rb b/app/controllers/officing/booth_controller.rb new file mode 100644 index 000000000..7dfee5400 --- /dev/null +++ b/app/controllers/officing/booth_controller.rb @@ -0,0 +1,43 @@ +class Officing::BoothController < Officing::BaseController + before_action :load_officer_assignment + before_action :verify_officer_assignment + + def new + load_booths + + if only_one_booth? + set_booth(@booths.first) + redirect_to officing_root_path, notice: t("officing.booth.new.success", booth: @booths.first.name) + end + end + + def create + find_booth + set_booth(@booth) + redirect_to officing_root_path, notice: t("officing.booth.new.success", booth: @booth.name) + end + + private + + def booth_params + params.require(:booth).permit(:id) + end + + def load_booths + officer = current_user.poll_officer + @booths = officer.officer_assignments.by_date(Date.today).map(&:booth) + 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 + +end diff --git a/app/controllers/officing/residence_controller.rb b/app/controllers/officing/residence_controller.rb index da39f8428..cb7b96a64 100644 --- a/app/controllers/officing/residence_controller.rb +++ b/app/controllers/officing/residence_controller.rb @@ -1,6 +1,8 @@ class Officing::ResidenceController < Officing::BaseController - before_action :validate_officer_assignment + before_action :load_officer_assignment + before_action :verify_officer_assignment + before_action :verify_booth def new @residence = Officing::Residence.new @@ -21,17 +23,4 @@ class Officing::ResidenceController < Officing::BaseController params.require(:residence).permit(:document_number, :document_type, :year_of_birth) end - def load_officer_assignment - @officer_assignments ||= current_user.poll_officer. - officer_assignments. - voting_days. - where(date: Date.current) - end - - def validate_officer_assignment - load_officer_assignment - if @officer_assignments.blank? - redirect_to officing_root_path, notice: t("officing.residence.flash.not_allowed") - end - end end diff --git a/app/controllers/officing/voters_controller.rb b/app/controllers/officing/voters_controller.rb index 564d71ba7..3354795b6 100644 --- a/app/controllers/officing/voters_controller.rb +++ b/app/controllers/officing/voters_controller.rb @@ -1,6 +1,10 @@ class Officing::VotersController < Officing::BaseController respond_to :html, :js + before_action :load_officer_assignment + before_action :verify_officer_assignment + before_action :verify_booth + def new @user = User.find(params[:id]) booths = current_user.poll_officer.shifts.current.vote_collection.pluck(:booth_id).uniq @@ -29,7 +33,13 @@ class Officing::VotersController < Officing::BaseController def officer_assignment(poll) Poll::OfficerAssignment.by_officer(current_user.poll_officer) .by_poll(poll) + .by_booth(current_booth) .by_date(Date.current) .first end + + def current_booth + Poll::Booth.find(session[:booth_id]) + end + end diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index d16f9c87e..0409a9d5e 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -3,7 +3,9 @@ class Users::SessionsController < Devise::SessionsController private def after_sign_in_path_for(resource) - if !verifying_via_email? && resource.show_welcome_screen? + if current_user.poll_officer? + new_officing_booth_path + elsif !verifying_via_email? && resource.show_welcome_screen? welcome_path else super diff --git a/app/models/poll/officer_assignment.rb b/app/models/poll/officer_assignment.rb index 7d0e31ded..7de4f98b3 100644 --- a/app/models/poll/officer_assignment.rb +++ b/app/models/poll/officer_assignment.rb @@ -18,7 +18,8 @@ class Poll where("officer_id = ? AND poll_booth_assignments.poll_id = ?", officer_id, poll_id) end scope :by_officer, ->(officer){ where(officer_id: officer.id) } - scope :by_poll, ->(poll){ joins(:booth_assignment).where("poll_booth_assignments.poll_id" => poll.id) } + scope :by_poll, ->(poll){ joins(:booth_assignment).where("poll_booth_assignments.poll_id" => poll.id) } + scope :by_booth, ->(booth){ joins(:booth_assignment).where("poll_booth_assignments.booth_id" => booth.id) } scope :by_date, ->(date){ where(date: date) } before_create :log_user_data @@ -26,5 +27,10 @@ class Poll def log_user_data self.user_data_log = "#{officer.user_id} - #{officer.user.name_and_email}" end + + def booth + booth_assignment.booth + end + end end diff --git a/app/views/officing/booth/new.html.erb b/app/views/officing/booth/new.html.erb new file mode 100644 index 000000000..5fbcd429e --- /dev/null +++ b/app/views/officing/booth/new.html.erb @@ -0,0 +1,25 @@ +