Skip to content

Commit

Permalink
Merge pull request consuldemocracy#5744 from consuldemocracy/select_name
Browse files Browse the repository at this point in the history
Associate all select fields with labels
  • Loading branch information
javierm authored Nov 12, 2024
2 parents ab67355 + 8130e4f commit 96e4ed4
Show file tree
Hide file tree
Showing 37 changed files with 188 additions and 208 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(function() {
"use strict";
App.PollsAdmin = {
App.AdminPollShiftsForm = {
initialize: function() {
$("select[class='js-poll-shifts']").on({
change: function() {
switch ($(this).val()) {
case "vote_collection":
$("select[class='js-shift-vote-collection-dates']").show();
$("select[class='js-shift-recount-scrutiny-dates']").hide();
$(".js-shift-vote-collection-dates").show();
$(".js-shift-recount-scrutiny-dates").hide();
break;
case "recount_scrutiny":
$("select[class='js-shift-recount-scrutiny-dates']").show();
$("select[class='js-shift-vote-collection-dates']").hide();
$(".js-shift-recount-scrutiny-dates").show();
$(".js-shift-vote-collection-dates").hide();
}
}
});
Expand Down
3 changes: 1 addition & 2 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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">
Expand All @@ -20,19 +20,26 @@
</div>

<div class="small-12 medium-3 column">
<label><%= t("admin.poll_shifts.new.date") %></label>
<%= label_tag :shift_date_vote_collection_date,
t("admin.poll_shifts.new.date"),
class: "js-shift-vote-collection-dates" %>
<%= 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),
{ 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" %>

<%= label_tag :shift_date_recount_scrutiny_date,
t("admin.poll_shifts.new.date"),
class: "js-shift-recount-scrutiny-dates",
hidden: "hidden" %>
<%= select "shift[date]", "recount_scrutiny_date",
options_for_select(shift_recount_scrutiny_dates(@booth, @recount_polls)),
options_for_select(shift_recount_scrutiny_dates),
{ 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"),
Expand Down
56 changes: 56 additions & 0 deletions app/components/admin/poll/shifts/form_component.rb
Original file line number Diff line number Diff line change
@@ -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
return [] if voting_polls.blank?

date_options((voting_start_date..voting_end_date), Poll::Shift.tasks[:vote_collection])
end

def shift_recount_scrutiny_dates
return [] if recount_polls.blank?

dates = recount_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])
end

def date_options(dates, task_id)
valid_dates(dates, task_id).map { |date| [l(date, format: :long), l(date)] }
end

def valid_dates(dates, task_id)
dates.reject { |date| officer_shifts(task_id).include?(date) }
end

def voting_start_date
start_date = voting_polls.minimum(:starts_at).to_date
[start_date, Date.current].max
end

def voting_end_date
voting_polls.maximum(:ends_at).to_date
end

def officer_shifts(task_id)
officer.shifts.where(task: task_id, booth: booth).map(&:date)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= form.date_field :date_of_birth, **field_options %>
25 changes: 25 additions & 0 deletions app/components/shared/date_of_birth_field_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Shared::DateOfBirthFieldComponent < ApplicationComponent
attr_reader :form, :options

def initialize(form, **options)
@form = form
@options = options
end

private

def default_options
{
min: "1900-01-01",
max: minimum_required_age.years.ago
}
end

def field_options
default_options.merge(options)
end

def minimum_required_age
User.minimum_required_age
end
end
2 changes: 0 additions & 2 deletions app/controllers/admin/poll/shifts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 0 additions & 44 deletions app/helpers/shifts_helper.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/helpers/verification_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ def document_types
[t("verification.residence.new.document_type.residence_card"), 3]]
end

def minimum_required_age
(Setting["min_age_to_participate"] || 16).to_i
end

def mask_phone(number)
match = number.match(/\d{3}$/)
"******#{match}"
Expand Down
13 changes: 0 additions & 13 deletions app/lib/active_model/dates.rb

This file was deleted.

8 changes: 3 additions & 5 deletions app/models/officing/residence.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Officing::Residence
include ActiveModel::Model
include ActiveModel::Dates
include ActiveModel::Attributes
include ActiveModel::Validations::Callbacks

attr_accessor :user, :officer, :document_number, :document_type, :year_of_birth,
:date_of_birth, :postal_code
attribute :date_of_birth, :date
attr_accessor :user, :officer, :document_number, :document_type, :year_of_birth, :postal_code

before_validation :retrieve_census_data

Expand All @@ -18,8 +18,6 @@ class Officing::Residence
validate :local_residence

def initialize(attrs = {})
self.date_of_birth = parse_date("date_of_birth", attrs)
attrs = remove_date("date_of_birth", attrs)
super
clean_document_number
end
Expand Down
11 changes: 3 additions & 8 deletions app/models/verification/management/document.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
class Verification::Management::Document
include ActiveModel::Model
include ActiveModel::Dates
include ActiveModel::Attributes

attr_accessor :document_type, :document_number, :date_of_birth, :postal_code
attribute :date_of_birth, :date
attr_accessor :document_type, :document_number, :postal_code

validates :document_type, :document_number, presence: true
validates :date_of_birth, presence: true, if: -> { Setting.force_presence_date_of_birth? }
validates :postal_code, presence: true, if: -> { Setting.force_presence_postal_code? }

delegate :username, :email, to: :user, allow_nil: true

def initialize(attrs = {})
self.date_of_birth = parse_date("date_of_birth", attrs)
attrs = remove_date("date_of_birth", attrs)
super
end

def user
@user = User.active.by_document(document_type, document_number).first
end
Expand Down
7 changes: 3 additions & 4 deletions app/models/verification/residence.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Verification::Residence
include ActiveModel::Model
include ActiveModel::Dates
include ActiveModel::Attributes
include ActiveModel::Validations::Callbacks

attr_accessor :user, :document_number, :document_type, :date_of_birth, :postal_code, :terms_of_service
attribute :date_of_birth, :date
attr_accessor :user, :document_number, :document_type, :postal_code, :terms_of_service

validates :document_number, presence: true
validates :document_type, presence: true
Expand All @@ -18,8 +19,6 @@ class Verification::Residence
validate :local_residence

def initialize(attrs = {})
self.date_of_birth = parse_date("date_of_birth", attrs)
attrs = remove_date("date_of_birth", attrs)
super
clean_document_number
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/emails_download/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
method: :get,
id: "admin_download_emails" do %>

<label><%= t("admin.emails_download.index.download_segment") %></label>
<%= label_tag :users_segment, t("admin.emails_download.index.download_segment") %>
<p class="help-text" id="emails-help-text">
<%= t("admin.emails_download.index.download_segment_help_text") %>
</p>
Expand Down
4 changes: 1 addition & 3 deletions app/views/admin/local_census_records/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

<div class="row">
<div class="date-of-birth small-12">
<%= f.date_select :date_of_birth,
prompt: true,
start_year: 1900, end_year: minimum_required_age.years.ago.year %>
<%= render Shared::DateOfBirthFieldComponent.new(f) %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/poll/shifts/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
4 changes: 1 addition & 3 deletions app/views/management/document_verifications/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

<% if Setting.force_presence_date_of_birth? %>
<div class="date-of-birth small-12 medium-5">
<%= f.date_select :date_of_birth,
prompt: true,
start_year: 1900, end_year: minimum_required_age.years.ago.year %>
<%= render Shared::DateOfBirthFieldComponent.new(f) %>
</div>
<% end %>

Expand Down
5 changes: 1 addition & 4 deletions app/views/management/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
<%= f.text_field :email,
label: t("management.users.email_optional_label") %>
<div class="date-of-birth">
<%= f.date_select :date_of_birth,
prompt: true,
start_year: 1900, end_year: 16.years.ago.year,
label: t("management.date_of_birth") %>
<%= render Shared::DateOfBirthFieldComponent.new(f, label: t("management.date_of_birth")) %>
</div>
<%= f.submit t("management.users.create_user_submit"), class: "button success" %>
<% end %>
Expand Down
5 changes: 2 additions & 3 deletions app/views/officing/booth/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
<div class="small-12 column">
<%= f.select :id,
@booths.map { |booth| [booth.location, booth.id] },
selected: @booths.first,
label: false,
tabindex: "1" %>
{ selected: @booths.first, label: false },
{ "aria-label": t("officing.booth.new.title") } %>

<%= f.submit(t("devise_views.sessions.new.submit"), class: "button expanded") %>
</div>
Expand Down
Loading

0 comments on commit 96e4ed4

Please sign in to comment.