Simplify passing the booth in shift form component

Since we're using helpers instead of components, the booth is now a
method and we don't need to pass it around.
This commit is contained in:
Javi Martín
2024-10-14 21:24:58 +02:00
parent ee34ead4ee
commit 5915194fc4
2 changed files with 11 additions and 11 deletions

View File

@@ -22,11 +22,11 @@
<div class="small-12 medium-3 column"> <div class="small-12 medium-3 column">
<label><%= t("admin.poll_shifts.new.date") %></label> <label><%= t("admin.poll_shifts.new.date") %></label>
<%= select "shift[date]", "vote_collection_date", <%= select "shift[date]", "vote_collection_date",
options_for_select(shift_vote_collection_dates(booth, voting_polls)), options_for_select(shift_vote_collection_dates(voting_polls)),
{ prompt: voting_polls.present? ? t("admin.poll_shifts.new.select_date") : t("admin.poll_shifts.new.no_voting_days") }, { prompt: voting_polls.present? ? t("admin.poll_shifts.new.select_date") : t("admin.poll_shifts.new.no_voting_days") },
class: "js-shift-vote-collection-dates" %> class: "js-shift-vote-collection-dates" %>
<%= select "shift[date]", "recount_scrutiny_date", <%= select "shift[date]", "recount_scrutiny_date",
options_for_select(shift_recount_scrutiny_dates(booth, recount_polls)), options_for_select(shift_recount_scrutiny_dates(recount_polls)),
{ prompt: t("admin.poll_shifts.new.select_date") }, { prompt: t("admin.poll_shifts.new.select_date") },
class: "js-shift-recount-scrutiny-dates", class: "js-shift-recount-scrutiny-dates",
hidden: "hidden" %> hidden: "hidden" %>

View File

@@ -17,28 +17,28 @@ class Admin::Poll::Shifts::FormComponent < ApplicationComponent
booth.polls.current_or_recounting booth.polls.current_or_recounting
end end
def shift_vote_collection_dates(booth, polls) def shift_vote_collection_dates(polls)
return [] if polls.blank? return [] if polls.blank?
date_options((start_date(polls)..end_date(polls)), Poll::Shift.tasks[:vote_collection], booth) date_options((start_date(polls)..end_date(polls)), Poll::Shift.tasks[:vote_collection])
end end
def shift_recount_scrutiny_dates(booth, polls) def shift_recount_scrutiny_dates(polls)
return [] if polls.blank? return [] if polls.blank?
dates = polls.map(&:ends_at).map(&:to_date).sort.reduce([]) do |total, date| dates = polls.map(&:ends_at).map(&:to_date).sort.reduce([]) do |total, date|
initial_date = [date, Date.current].max initial_date = [date, Date.current].max
total << (initial_date..date + Poll::RECOUNT_DURATION).to_a total << (initial_date..date + Poll::RECOUNT_DURATION).to_a
end end
date_options(dates.flatten.uniq, Poll::Shift.tasks[:recount_scrutiny], booth) date_options(dates.flatten.uniq, Poll::Shift.tasks[:recount_scrutiny])
end end
def date_options(dates, task_id, booth) def date_options(dates, task_id)
valid_dates(dates, task_id, booth).map { |date| [l(date, format: :long), l(date)] } valid_dates(dates, task_id).map { |date| [l(date, format: :long), l(date)] }
end end
def valid_dates(dates, task_id, booth) def valid_dates(dates, task_id)
dates.reject { |date| officer_shifts(task_id, booth).include?(date) } dates.reject { |date| officer_shifts(task_id).include?(date) }
end end
def start_date(polls) def start_date(polls)
@@ -50,7 +50,7 @@ class Admin::Poll::Shifts::FormComponent < ApplicationComponent
polls.maximum(:ends_at).to_date polls.maximum(:ends_at).to_date
end end
def officer_shifts(task_id, booth) def officer_shifts(task_id)
officer.shifts.where(task: task_id, booth: booth).map(&:date) officer.shifts.where(task: task_id, booth: booth).map(&:date)
end end
end end