Use date attribute for date of birth (#36039)

This commit is contained in:
Matt Jankowski
2025-11-14 10:17:53 -05:00
committed by GitHub
parent 7dbb2ac79a
commit f8ffb85566
3 changed files with 9 additions and 19 deletions

View File

@@ -2,25 +2,25 @@
class DateOfBirthInput < SimpleForm::Inputs::Base
OPTIONS = [
{ autocomplete: 'bday-day', maxlength: 2, pattern: '[0-9]+', placeholder: 'DD' }.freeze,
{ autocomplete: 'bday-month', maxlength: 2, pattern: '[0-9]+', placeholder: 'MM' }.freeze,
{ autocomplete: 'bday-year', maxlength: 4, pattern: '[0-9]+', placeholder: 'YYYY' }.freeze,
{ autocomplete: 'bday-month', maxlength: 2, pattern: '[0-9]+', placeholder: 'MM' }.freeze,
{ autocomplete: 'bday-day', maxlength: 2, pattern: '[0-9]+', placeholder: 'DD' }.freeze,
].freeze
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
merged_input_options[:inputmode] = 'numeric'
values = (object.public_send(attribute_name) || '').split('.')
values = (object.public_send(attribute_name) || '').to_s.split('-')
safe_join(Array.new(3) do |index|
safe_join(2.downto(0).map do |index|
options = merged_input_options.merge(OPTIONS[index]).merge id: generate_id(index), 'aria-label': I18n.t("simple_form.labels.user.date_of_birth_#{index + 1}i"), value: values[index]
@builder.text_field("#{attribute_name}(#{index + 1}i)", options)
end)
end
def label_target
"#{attribute_name}_1i"
"#{attribute_name}_3i"
end
private

View File

@@ -131,11 +131,12 @@ class User < ApplicationRecord
delegate :can?, to: :role
attr_reader :invite_code, :date_of_birth
attr_reader :invite_code
attr_writer :current_account
attribute :external, :boolean, default: false
attribute :bypass_registration_checks, :boolean, default: false
attribute :date_of_birth, :date
def self.those_who_can(*any_of_privileges)
matching_role_ids = UserRole.that_can(*any_of_privileges).map(&:id)
@@ -151,17 +152,6 @@ class User < ApplicationRecord
Rails.env.local?
end
def date_of_birth=(hash_or_string)
@date_of_birth = begin
if hash_or_string.is_a?(Hash)
day, month, year = hash_or_string.values_at(1, 2, 3)
"#{day}.#{month}.#{year}"
else
hash_or_string
end
end
end
def role
if role_id.nil?
UserRole.everyone

View File

@@ -372,9 +372,9 @@ en:
jurisdiction: Legal jurisdiction
min_age: Minimum age
user:
date_of_birth_1i: Day
date_of_birth_1i: Year
date_of_birth_2i: Month
date_of_birth_3i: Year
date_of_birth_3i: Day
role: Role
time_zone: Time zone
user_role: