Files
grecia/app/models/verification/management/document.rb
Javi Martín 3b7948a139 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.
2024-11-12 15:15:34 +01:00

48 lines
1.2 KiB
Ruby

class Verification::Management::Document
include ActiveModel::Model
include ActiveModel::Attributes
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 user
@user = User.active.by_document(document_type, document_number).first
end
def user?
user.present?
end
def in_census?
response = CensusCaller.new.call(document_type, document_number, date_of_birth, postal_code)
response.valid? && valid_age?(response)
end
def valid_age?(response)
if under_age?(response)
errors.add(:age, true)
false
else
true
end
end
def under_age?(response)
response.date_of_birth.blank? || Age.in_years(response.date_of_birth) < User.minimum_required_age
end
def verified?
user? && user.level_three_verified?
end
def verify
user.update(verified_at: Time.current) if user?
end
end