Move poll shifts form partial to a component
Thanks to it, we can move a few helper methods to the component.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
(function() {
|
||||
"use strict";
|
||||
App.PollsAdmin = {
|
||||
App.AdminPollShiftsForm = {
|
||||
initialize: function() {
|
||||
$("select[class='js-poll-shifts']").on({
|
||||
change: function() {
|
||||
@@ -101,7 +101,6 @@
|
||||
//= require imageable
|
||||
//= require tree_navigator
|
||||
//= require tag_autocomplete
|
||||
//= require polls_admin
|
||||
//= require leaflet/dist/leaflet
|
||||
//= require leaflet.markercluster/dist/leaflet.markercluster
|
||||
//= require map
|
||||
@@ -157,7 +156,6 @@ var initialize_modules = function() {
|
||||
App.Documentable.initialize();
|
||||
App.Imageable.initialize();
|
||||
App.TagAutocomplete.initialize();
|
||||
App.PollsAdmin.initialize();
|
||||
App.Map.initialize();
|
||||
App.Polls.initialize();
|
||||
App.Sortable.initialize();
|
||||
@@ -171,6 +169,7 @@ var initialize_modules = function() {
|
||||
}
|
||||
App.AdminBudgetsWizardCreationStep.initialize();
|
||||
App.AdminMachineLearningScripts.initialize();
|
||||
App.AdminPollShiftsForm.initialize();
|
||||
App.AdminTenantsForm.initialize();
|
||||
App.AdminVotationTypesFields.initialize();
|
||||
App.AdminMenu.initialize();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<%= form_for @shift, as: :shift, url: admin_booth_shifts_path do |f| %>
|
||||
<%= render "shared/errors", resource: @shift %>
|
||||
<%= form_for shift, as: :shift, url: admin_booth_shifts_path do |f| %>
|
||||
<%= render "shared/errors", resource: shift %>
|
||||
|
||||
<fieldset class="fieldset">
|
||||
<legend>
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
<div class="small-12 medium-3 column highlight padding">
|
||||
<strong><%= t("admin.poll_shifts.new.officer") %></strong>
|
||||
<br><%= @officer.name %>
|
||||
<%= f.hidden_field :officer_id, value: @officer.id %>
|
||||
<br><%= officer.name %>
|
||||
<%= f.hidden_field :officer_id, value: officer.id %>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-3 column">
|
||||
@@ -22,17 +22,17 @@
|
||||
<div class="small-12 medium-3 column">
|
||||
<label><%= t("admin.poll_shifts.new.date") %></label>
|
||||
<%= select "shift[date]", "vote_collection_date",
|
||||
options_for_select(shift_vote_collection_dates(@booth, @voting_polls)),
|
||||
{ prompt: @voting_polls.present? ? t("admin.poll_shifts.new.select_date") : t("admin.poll_shifts.new.no_voting_days") },
|
||||
options_for_select(shift_vote_collection_dates(booth, voting_polls)),
|
||||
{ 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" %>
|
||||
<%= select "shift[date]", "recount_scrutiny_date",
|
||||
options_for_select(shift_recount_scrutiny_dates(@booth, @recount_polls)),
|
||||
options_for_select(shift_recount_scrutiny_dates(booth, recount_polls)),
|
||||
{ prompt: t("admin.poll_shifts.new.select_date") },
|
||||
class: "js-shift-recount-scrutiny-dates",
|
||||
hidden: "hidden" %>
|
||||
</div>
|
||||
|
||||
<%= f.hidden_field :booth_id, value: @booth.id %>
|
||||
<%= f.hidden_field :booth_id, value: booth.id %>
|
||||
|
||||
<div class="small-12 medium-3 column">
|
||||
<%= f.submit t("admin.poll_shifts.new.add_shift"),
|
||||
56
app/components/admin/poll/shifts/form_component.rb
Normal file
56
app/components/admin/poll/shifts/form_component.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
class Admin::Poll::Shifts::FormComponent < ApplicationComponent
|
||||
attr_reader :shift, :booth, :officer
|
||||
|
||||
def initialize(shift, booth:, officer:)
|
||||
@shift = shift
|
||||
@booth = booth
|
||||
@officer = officer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def voting_polls
|
||||
booth.polls.current
|
||||
end
|
||||
|
||||
def recount_polls
|
||||
booth.polls.current_or_recounting
|
||||
end
|
||||
|
||||
def shift_vote_collection_dates(booth, polls)
|
||||
return [] if polls.blank?
|
||||
|
||||
date_options((start_date(polls)..end_date(polls)), Poll::Shift.tasks[:vote_collection], booth)
|
||||
end
|
||||
|
||||
def shift_recount_scrutiny_dates(booth, polls)
|
||||
return [] if polls.blank?
|
||||
|
||||
dates = polls.map(&:ends_at).map(&:to_date).sort.reduce([]) do |total, date|
|
||||
initial_date = [date, Date.current].max
|
||||
total << (initial_date..date + Poll::RECOUNT_DURATION).to_a
|
||||
end
|
||||
date_options(dates.flatten.uniq, Poll::Shift.tasks[:recount_scrutiny], booth)
|
||||
end
|
||||
|
||||
def date_options(dates, task_id, booth)
|
||||
valid_dates(dates, task_id, booth).map { |date| [l(date, format: :long), l(date)] }
|
||||
end
|
||||
|
||||
def valid_dates(dates, task_id, booth)
|
||||
dates.reject { |date| officer_shifts(task_id, booth).include?(date) }
|
||||
end
|
||||
|
||||
def start_date(polls)
|
||||
start_date = polls.minimum(:starts_at).to_date
|
||||
[start_date, Date.current].max
|
||||
end
|
||||
|
||||
def end_date(polls)
|
||||
polls.maximum(:ends_at).to_date
|
||||
end
|
||||
|
||||
def officer_shifts(task_id, booth)
|
||||
officer.shifts.where(task: task_id, booth: booth).map(&:date)
|
||||
end
|
||||
end
|
||||
@@ -5,8 +5,6 @@ class Admin::Poll::ShiftsController < Admin::Poll::BaseController
|
||||
def new
|
||||
load_shifts
|
||||
@shift = ::Poll::Shift.new
|
||||
@voting_polls = @booth.polls.current
|
||||
@recount_polls = @booth.polls.current_or_recounting
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
module ShiftsHelper
|
||||
def shift_vote_collection_dates(booth, polls)
|
||||
return [] if polls.blank?
|
||||
|
||||
date_options((start_date(polls)..end_date(polls)), Poll::Shift.tasks[:vote_collection], booth)
|
||||
end
|
||||
|
||||
def shift_recount_scrutiny_dates(booth, polls)
|
||||
return [] if polls.blank?
|
||||
|
||||
dates = polls.map(&:ends_at).map(&:to_date).sort.reduce([]) do |total, date|
|
||||
initial_date = [date, Date.current].max
|
||||
total << (initial_date..date + Poll::RECOUNT_DURATION).to_a
|
||||
end
|
||||
date_options(dates.flatten.uniq, Poll::Shift.tasks[:recount_scrutiny], booth)
|
||||
end
|
||||
|
||||
def date_options(dates, task_id, booth)
|
||||
valid_dates(dates, task_id, booth).map { |date| [l(date, format: :long), l(date)] }
|
||||
end
|
||||
|
||||
def valid_dates(dates, task_id, booth)
|
||||
dates.reject { |date| officer_shifts(task_id, booth).include?(date) }
|
||||
end
|
||||
|
||||
def start_date(polls)
|
||||
start_date = polls.minimum(:starts_at).to_date
|
||||
[start_date, Date.current].max
|
||||
end
|
||||
|
||||
def end_date(polls)
|
||||
polls.maximum(:ends_at).to_date
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def officer_shifts(task_id, booth)
|
||||
@officer.shifts.where(task: task_id, booth: booth).map(&:date)
|
||||
end
|
||||
end
|
||||
@@ -8,7 +8,7 @@
|
||||
</p>
|
||||
<%= render "search_officers" %>
|
||||
<% else %>
|
||||
<%= render "form" %>
|
||||
<%= render Admin::Poll::Shifts::FormComponent.new(@shift, booth: @booth, officer: @officer) %>
|
||||
<% end %>
|
||||
|
||||
<div id="shifts">
|
||||
|
||||
Reference in New Issue
Block a user