diff --git a/app/assets/javascripts/polls_admin.js b/app/assets/javascripts/admin/poll/shifts/form.js similarity index 95% rename from app/assets/javascripts/polls_admin.js rename to app/assets/javascripts/admin/poll/shifts/form.js index 87e55756b..ee37951f9 100644 --- a/app/assets/javascripts/polls_admin.js +++ b/app/assets/javascripts/admin/poll/shifts/form.js @@ -1,6 +1,6 @@ (function() { "use strict"; - App.PollsAdmin = { + App.AdminPollShiftsForm = { initialize: function() { $("select[class='js-poll-shifts']").on({ change: function() { diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 9351e8ad2..cce7d7dd9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -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(); diff --git a/app/views/admin/poll/shifts/_form.html.erb b/app/components/admin/poll/shifts/form_component.html.erb similarity index 74% rename from app/views/admin/poll/shifts/_form.html.erb rename to app/components/admin/poll/shifts/form_component.html.erb index 2c0fc6249..bdb00af68 100644 --- a/app/views/admin/poll/shifts/_form.html.erb +++ b/app/components/admin/poll/shifts/form_component.html.erb @@ -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 %>
@@ -8,8 +8,8 @@
<%= t("admin.poll_shifts.new.officer") %> -
<%= @officer.name %> - <%= f.hidden_field :officer_id, value: @officer.id %> +
<%= officer.name %> + <%= f.hidden_field :officer_id, value: officer.id %>
@@ -22,17 +22,17 @@
<%= 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" %>
- <%= f.hidden_field :booth_id, value: @booth.id %> + <%= f.hidden_field :booth_id, value: booth.id %>
<%= f.submit t("admin.poll_shifts.new.add_shift"), diff --git a/app/components/admin/poll/shifts/form_component.rb b/app/components/admin/poll/shifts/form_component.rb new file mode 100644 index 000000000..a2ec1d967 --- /dev/null +++ b/app/components/admin/poll/shifts/form_component.rb @@ -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 diff --git a/app/controllers/admin/poll/shifts_controller.rb b/app/controllers/admin/poll/shifts_controller.rb index 2aa60ecbb..b8ab83852 100644 --- a/app/controllers/admin/poll/shifts_controller.rb +++ b/app/controllers/admin/poll/shifts_controller.rb @@ -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 diff --git a/app/helpers/shifts_helper.rb b/app/helpers/shifts_helper.rb deleted file mode 100644 index 9dbc3d4a5..000000000 --- a/app/helpers/shifts_helper.rb +++ /dev/null @@ -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 diff --git a/app/views/admin/poll/shifts/new.html.erb b/app/views/admin/poll/shifts/new.html.erb index 4e0665ea2..b6b6245d8 100644 --- a/app/views/admin/poll/shifts/new.html.erb +++ b/app/views/admin/poll/shifts/new.html.erb @@ -8,7 +8,7 @@

<%= render "search_officers" %> <% else %> - <%= render "form" %> + <%= render Admin::Poll::Shifts::FormComponent.new(@shift, booth: @booth, officer: @officer) %> <% end %>