Extract component to render the date of birth

We reduce code duplication thanks to that, and make it easier
to change this field.

Note that there was one place where the "16.years" value was
hardcoded. We're moving the test for this case to the
component and changing it so the test doesn't use the default
age.

We're also removing the redundant helper method that had the
same code as a method in the User class which is called
everywhere else.
This commit is contained in:
Javi Martín
2024-10-12 15:56:47 +02:00
parent 6f81215425
commit 6af8ddd324
10 changed files with 58 additions and 35 deletions

View File

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

View File

@@ -0,0 +1,26 @@
class Shared::DateOfBirthFieldComponent < ApplicationComponent
attr_reader :form, :options
def initialize(form, **options)
@form = form
@options = options
end
private
def default_options
{
prompt: true,
start_year: 1900,
end_year: minimum_required_age.years.ago.year
}
end
def field_options
default_options.merge(options)
end
def minimum_required_age
User.minimum_required_age
end
end