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

View File

@@ -5,10 +5,6 @@ module VerificationHelper
[t("verification.residence.new.document_type.residence_card"), 3]] [t("verification.residence.new.document_type.residence_card"), 3]]
end end
def minimum_required_age
(Setting["min_age_to_participate"] || 16).to_i
end
def mask_phone(number) def mask_phone(number)
match = number.match(/\d{3}$/) match = number.match(/\d{3}$/)
"******#{match}" "******#{match}"

View File

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

View File

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

View File

@@ -11,10 +11,7 @@
<%= f.text_field :email, <%= f.text_field :email,
label: t("management.users.email_optional_label") %> label: t("management.users.email_optional_label") %>
<div class="date-of-birth"> <div class="date-of-birth">
<%= f.date_select :date_of_birth, <%= render Shared::DateOfBirthFieldComponent.new(f, label: t("management.date_of_birth")) %>
prompt: true,
start_year: 1900, end_year: 16.years.ago.year,
label: t("management.date_of_birth") %>
</div> </div>
<%= f.submit t("management.users.create_user_submit"), class: "button success" %> <%= f.submit t("management.users.create_user_submit"), class: "button success" %>
<% end %> <% end %>

View File

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

View File

@@ -57,9 +57,7 @@
</div> </div>
<div class="date-of-birth small-12 medium-6 clear"> <div class="date-of-birth small-12 medium-6 clear">
<%= f.date_select :date_of_birth, <%= render Shared::DateOfBirthFieldComponent.new(f) %>
prompt: true,
start_year: 1900, end_year: minimum_required_age.years.ago.year %>
</div> </div>
<div class="small-12 medium-5 clear"> <div class="small-12 medium-5 clear">

View File

@@ -0,0 +1,26 @@
require "rails_helper"
describe Shared::DateOfBirthFieldComponent do
before do
dummy_model = Class.new do
include ActiveModel::Model
attr_accessor :date_of_birth
end
stub_const("DummyModel", dummy_model)
end
let(:form) { ConsulFormBuilder.new(:dummy, DummyModel.new, ApplicationController.new.view_context, {}) }
let(:component) { Shared::DateOfBirthFieldComponent.new(form) }
it "uses the minimum required age as the latest date by default" do
Setting["min_age_to_participate"] = 13
travel_to "2015-07-15" do
render_inline component
expect(page).to have_select with_options: [2002, 2001, 2000, 1999]
expect(page).not_to have_select with_options: [2003]
end
end
end

View File

@@ -38,21 +38,6 @@ describe "Residence" do
expect(page).to have_content "Residence verified" expect(page).to have_content "Residence verified"
end end
scenario "Residence form use min age to participate" do
min_age = (Setting["min_age_to_participate"] = 16).to_i
underage = min_age - 1
user = create(:user)
login_as(user)
visit account_path
click_link "Verify my account"
expect(page).to have_select("residence_date_of_birth_1i",
with_options: [min_age.years.ago.year])
expect(page).not_to have_select("residence_date_of_birth_1i",
with_options: [underage.years.ago.year])
end
scenario "When trying to verify a deregistered account old votes are reassigned" do scenario "When trying to verify a deregistered account old votes are reassigned" do
erased_user = create(:user, document_number: "12345678Z", document_type: "1", erased_at: Time.current) erased_user = create(:user, document_number: "12345678Z", document_type: "1", erased_at: Time.current)
vote = create(:vote, voter: erased_user) vote = create(:vote, voter: erased_user)