Use a date field to select the date of birth

The default `date_select` used in fields presents an accessibility
issue, because in generates three select controls but only one label.
That means that there are two controls without a label.

So we're using a date field instead. This type is field is supported by
about 99% of the browsers, and we've already got JavaScript code
converting this field to a jQuery UI datepicker in case the browser
doesn't support date fields.

Note that, since we no longer need to parse the three date fields into
one, we can simplify the code in both the models and the tests.

Another slight improvement is that, previously, we couldn't restrict the
month and day controls in order to set the minimum date, so the maximum
selectable date was always the 31st of December of the year set by the
minimum age setting. As seen in the component test, now that we use only
one field, we can set a specific date as the maximum one.
This commit is contained in:
Javi Martín
2024-10-12 16:50:40 +02:00
parent 6af8ddd324
commit 3b7948a139
16 changed files with 35 additions and 90 deletions

View File

@@ -1 +1 @@
<%= form.date_select :date_of_birth, **field_options %> <%= form.date_field :date_of_birth, **field_options %>

View File

@@ -10,9 +10,8 @@ class Shared::DateOfBirthFieldComponent < ApplicationComponent
def default_options def default_options
{ {
prompt: true, min: "1900-01-01",
start_year: 1900, max: minimum_required_age.years.ago
end_year: minimum_required_age.years.ago.year
} }
end end

View File

@@ -1,13 +0,0 @@
module ActiveModel::Dates
def parse_date(field, attrs)
year, month, day = attrs["#{field}(1i)"], attrs["#{field}(2i)"], attrs["#{field}(3i)"]
return nil if day.blank? || month.blank? || year.blank?
Date.new(year.to_i, month.to_i, day.to_i)
end
def remove_date(field, attrs)
attrs.except("#{field}(1i)", "#{field}(2i)", "#{field}(3i)")
end
end

View File

@@ -1,10 +1,10 @@
class Officing::Residence class Officing::Residence
include ActiveModel::Model include ActiveModel::Model
include ActiveModel::Dates include ActiveModel::Attributes
include ActiveModel::Validations::Callbacks include ActiveModel::Validations::Callbacks
attr_accessor :user, :officer, :document_number, :document_type, :year_of_birth, attribute :date_of_birth, :date
:date_of_birth, :postal_code attr_accessor :user, :officer, :document_number, :document_type, :year_of_birth, :postal_code
before_validation :retrieve_census_data before_validation :retrieve_census_data
@@ -18,8 +18,6 @@ class Officing::Residence
validate :local_residence validate :local_residence
def initialize(attrs = {}) def initialize(attrs = {})
self.date_of_birth = parse_date("date_of_birth", attrs)
attrs = remove_date("date_of_birth", attrs)
super super
clean_document_number clean_document_number
end end

View File

@@ -1,8 +1,9 @@
class Verification::Management::Document class Verification::Management::Document
include ActiveModel::Model 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 :document_type, :document_number, presence: true
validates :date_of_birth, presence: true, if: -> { Setting.force_presence_date_of_birth? } validates :date_of_birth, presence: true, if: -> { Setting.force_presence_date_of_birth? }
@@ -10,12 +11,6 @@ class Verification::Management::Document
delegate :username, :email, to: :user, allow_nil: true 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 def user
@user = User.active.by_document(document_type, document_number).first @user = User.active.by_document(document_type, document_number).first
end end

View File

@@ -1,9 +1,10 @@
class Verification::Residence class Verification::Residence
include ActiveModel::Model include ActiveModel::Model
include ActiveModel::Dates include ActiveModel::Attributes
include ActiveModel::Validations::Callbacks 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_number, presence: true
validates :document_type, presence: true validates :document_type, presence: true
@@ -18,8 +19,6 @@ class Verification::Residence
validate :local_residence validate :local_residence
def initialize(attrs = {}) def initialize(attrs = {})
self.date_of_birth = parse_date("date_of_birth", attrs)
attrs = remove_date("date_of_birth", attrs)
super super
clean_document_number clean_document_number
end end

View File

@@ -19,8 +19,8 @@ describe Shared::DateOfBirthFieldComponent do
travel_to "2015-07-15" do travel_to "2015-07-15" do
render_inline component render_inline component
expect(page).to have_select with_options: [2002, 2001, 2000, 1999] expect(page).to have_field "Date of birth"
expect(page).not_to have_select with_options: [2003] expect(page).to have_css "input[type=date][min='1900-01-01'][max='2002-07-15']"
end end
end end
end end

View File

@@ -84,18 +84,14 @@ describe Officing::Residence do
describe "dates" do describe "dates" do
it "is not valid but not because date of birth" do it "is not valid but not because date of birth" do
custom_residence = Officing::Residence.new("date_of_birth(3i)" => "1", custom_residence = Officing::Residence.new(date_of_birth: "1980-01-01")
"date_of_birth(2i)" => "1",
"date_of_birth(1i)" => "1980")
expect(custom_residence).not_to be_valid expect(custom_residence).not_to be_valid
expect(custom_residence.errors[:date_of_birth]).to be_empty expect(custom_residence.errors[:date_of_birth]).to be_empty
end end
it "is not valid without a date of birth" do it "is not valid without a date of birth" do
custom_residence = Officing::Residence.new("date_of_birth(3i)" => "", custom_residence = Officing::Residence.new(date_of_birth: "")
"date_of_birth(2i)" => "",
"date_of_birth(1i)" => "")
expect(custom_residence).not_to be_valid expect(custom_residence).not_to be_valid
expect(custom_residence.errors[:date_of_birth]).to include("can't be blank") expect(custom_residence.errors[:date_of_birth]).to include("can't be blank")
end end

View File

@@ -57,17 +57,13 @@ describe Verification::Management::Document do
describe "dates" do describe "dates" do
it "is valid with a valid date of birth" do it "is valid with a valid date of birth" do
verification_document = Verification::Management::Document.new("date_of_birth(3i)" => "1", verification_document = Verification::Management::Document.new(date_of_birth: "1980-01-01")
"date_of_birth(2i)" => "1",
"date_of_birth(1i)" => "1980")
expect(verification_document.errors[:date_of_birth]).to be_empty expect(verification_document.errors[:date_of_birth]).to be_empty
end end
it "is not valid without a date of birth" do it "is not valid without a date of birth" do
verification_document = Verification::Management::Document.new("date_of_birth(3i)" => "", verification_document = Verification::Management::Document.new(date_of_birth: "")
"date_of_birth(2i)" => "",
"date_of_birth(1i)" => "")
expect(verification_document).not_to be_valid expect(verification_document).not_to be_valid
expect(verification_document.errors[:date_of_birth]).to include("can't be blank") expect(verification_document.errors[:date_of_birth]).to include("can't be blank")
end end

View File

@@ -11,26 +11,20 @@ describe Verification::Residence do
describe "dates" do describe "dates" do
it "is valid with a valid date of birth" do it "is valid with a valid date of birth" do
residence = Verification::Residence.new("date_of_birth(3i)" => "1", residence = Verification::Residence.new(date_of_birth: "1980-01-01")
"date_of_birth(2i)" => "1",
"date_of_birth(1i)" => "1980")
expect(residence.errors[:date_of_birth]).to be_empty expect(residence.errors[:date_of_birth]).to be_empty
end end
it "is not valid without a date of birth" do it "is not valid without a date of birth" do
residence = Verification::Residence.new("date_of_birth(3i)" => "", residence = Verification::Residence.new(date_of_birth: "")
"date_of_birth(2i)" => "",
"date_of_birth(1i)" => "")
expect(residence).not_to be_valid expect(residence).not_to be_valid
expect(residence.errors[:date_of_birth]).to include("can't be blank") expect(residence.errors[:date_of_birth]).to include("can't be blank")
end end
end end
it "validates user has allowed age" do it "validates user has allowed age" do
residence = Verification::Residence.new("date_of_birth(3i)" => "1", residence = Verification::Residence.new(date_of_birth: 5.years.ago)
"date_of_birth(2i)" => "1",
"date_of_birth(1i)" => 5.years.ago.year.to_s)
expect(residence).not_to be_valid expect(residence).not_to be_valid
expect(residence.errors[:date_of_birth]).to include("You don't have the required age to participate") expect(residence.errors[:date_of_birth]).to include("You don't have the required age to participate")
end end

View File

@@ -1,17 +1,8 @@
module Verifications module Verifications
def select_date(values, selector)
selector = selector[:from]
day, month, year = values.split("-")
select day, from: "#{selector}_3i"
select month, from: "#{selector}_2i"
select year, from: "#{selector}_1i"
end
def verify_residence def verify_residence
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
fill_in "residence_document_number", with: "12345678Z" fill_in "residence_document_number", with: "12345678Z"
select_date "31-#{I18n.l(Date.current.at_end_of_year, format: "%B")}-1980", fill_in "residence_date_of_birth", with: Date.new(1980, 12, 31)
from: "residence_date_of_birth"
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
check "residence_terms_of_service" check "residence_terms_of_service"

View File

@@ -84,9 +84,7 @@ describe "Admin local census records", :admin do
select "DNI", from: :local_census_record_document_type select "DNI", from: :local_census_record_document_type
fill_in :local_census_record_document_number, with: "#DOCUMENT" fill_in :local_census_record_document_number, with: "#DOCUMENT"
select "1982", from: :local_census_record_date_of_birth_1i fill_in "Date of birth", with: Date.new(1982, 7, 7)
select "July", from: :local_census_record_date_of_birth_2i
select "7", from: :local_census_record_date_of_birth_3i
fill_in :local_census_record_postal_code, with: "07003" fill_in :local_census_record_postal_code, with: "07003"
click_button "Save" click_button "Save"
@@ -116,9 +114,7 @@ describe "Admin local census records", :admin do
select "Passport", from: :local_census_record_document_type select "Passport", from: :local_census_record_document_type
fill_in :local_census_record_document_number, with: "#NIE_NUMBER" fill_in :local_census_record_document_number, with: "#NIE_NUMBER"
select "1982", from: :local_census_record_date_of_birth_1i fill_in "Date of birth", with: Date.new(1982, 8, 8)
select "August", from: :local_census_record_date_of_birth_2i
select "8", from: :local_census_record_date_of_birth_3i
fill_in :local_census_record_postal_code, with: "07007" fill_in :local_census_record_postal_code, with: "07007"
click_button "Save" click_button "Save"

View File

@@ -63,7 +63,7 @@ describe "DocumentVerifications" do
login_as_manager login_as_manager
visit management_document_verifications_path visit management_document_verifications_path
fill_in "document_verification_document_number", with: "12345678Z" fill_in "document_verification_document_number", with: "12345678Z"
select_date "31-December-1980", from: "document_verification_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
fill_in "document_verification_postal_code", with: "inexisting" fill_in "document_verification_postal_code", with: "inexisting"
click_button "Check document" click_button "Check document"
@@ -77,7 +77,7 @@ describe "DocumentVerifications" do
login_as_manager login_as_manager
visit management_document_verifications_path visit management_document_verifications_path
fill_in "document_verification_document_number", with: "12345678Z" fill_in "document_verification_document_number", with: "12345678Z"
select_date "31-December-1980", from: "document_verification_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
fill_in "document_verification_postal_code", with: "28013" fill_in "document_verification_postal_code", with: "28013"
click_button "Check document" click_button "Check document"

View File

@@ -13,7 +13,7 @@ describe "Users" do
fill_in "user_username", with: "pepe" fill_in "user_username", with: "pepe"
fill_in "user_email", with: "pepe@gmail.com" fill_in "user_email", with: "pepe@gmail.com"
select_date "31-December-1980", from: "user_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
click_button "Create user" click_button "Create user"
@@ -54,7 +54,7 @@ describe "Users" do
fill_in "user_username", with: "Kelly Sue" fill_in "user_username", with: "Kelly Sue"
fill_in "user_email", with: "" fill_in "user_email", with: ""
select_date "31-December-1980", from: "user_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
click_button "Create user" click_button "Create user"

View File

@@ -162,7 +162,7 @@ describe "Residence", :with_frozen_time do
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
fill_in "residence_document_number", with: "12345678Z" fill_in "residence_document_number", with: "12345678Z"
select_date "31-December-1980", from: "residence_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
click_button "Validate document" click_button "Validate document"

View File

@@ -12,7 +12,7 @@ describe "Residence" do
fill_in "Document number", with: "12345678Z" fill_in "Document number", with: "12345678Z"
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
select_date "31-December-1980", from: "residence_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
check "residence_terms_of_service" check "residence_terms_of_service"
click_button "Verify residence" click_button "Verify residence"
@@ -30,7 +30,7 @@ describe "Residence" do
fill_in "Document number", with: "12345678Z" fill_in "Document number", with: "12345678Z"
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
select_date "31-December-1980", from: "residence_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
check "residence_terms_of_service" check "residence_terms_of_service"
click_button "Verify residence" click_button "Verify residence"
@@ -50,7 +50,7 @@ describe "Residence" do
fill_in "Document number", with: "12345678Z" fill_in "Document number", with: "12345678Z"
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
select_date "31-December-1980", from: "residence_date_of_birth" fill_in "Date of birth", with: Date.new(1980, 12, 31)
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
check "residence_terms_of_service" check "residence_terms_of_service"
@@ -85,9 +85,7 @@ describe "Residence" do
fill_in "Document number", with: "12345678Z" fill_in "Document number", with: "12345678Z"
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
select "1997", from: "residence_date_of_birth_1i" fill_in "Date of birth", with: Date.new(1997, 1, 1)
select "January", from: "residence_date_of_birth_2i"
select "1", from: "residence_date_of_birth_3i"
fill_in "residence_postal_code", with: "00000" fill_in "residence_postal_code", with: "00000"
check "residence_terms_of_service" check "residence_terms_of_service"
@@ -105,9 +103,7 @@ describe "Residence" do
fill_in "Document number", with: "12345678Z" fill_in "Document number", with: "12345678Z"
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
select "1997", from: "residence_date_of_birth_1i" fill_in "Date of birth", with: Date.new(1997, 1, 1)
select "January", from: "residence_date_of_birth_2i"
select "1", from: "residence_date_of_birth_3i"
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
check "residence_terms_of_service" check "residence_terms_of_service"
@@ -126,9 +122,7 @@ describe "Residence" do
5.times do 5.times do
fill_in "Document number", with: "12345678Z" fill_in "Document number", with: "12345678Z"
select "DNI", from: "residence_document_type" select "DNI", from: "residence_document_type"
select "1997", from: "residence_date_of_birth_1i" fill_in "Date of birth", with: Date.new(1997, 1, 1)
select "January", from: "residence_date_of_birth_2i"
select "1", from: "residence_date_of_birth_3i"
fill_in "residence_postal_code", with: "28013" fill_in "residence_postal_code", with: "28013"
check "residence_terms_of_service" check "residence_terms_of_service"