diff --git a/app/components/shared/date_of_birth_field_component.html.erb b/app/components/shared/date_of_birth_field_component.html.erb new file mode 100644 index 000000000..91fedf367 --- /dev/null +++ b/app/components/shared/date_of_birth_field_component.html.erb @@ -0,0 +1 @@ +<%= form.date_select :date_of_birth, **field_options %> diff --git a/app/components/shared/date_of_birth_field_component.rb b/app/components/shared/date_of_birth_field_component.rb new file mode 100644 index 000000000..9289ff05f --- /dev/null +++ b/app/components/shared/date_of_birth_field_component.rb @@ -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 diff --git a/app/helpers/verification_helper.rb b/app/helpers/verification_helper.rb index 3dd9123c9..17f57b774 100644 --- a/app/helpers/verification_helper.rb +++ b/app/helpers/verification_helper.rb @@ -5,10 +5,6 @@ module VerificationHelper [t("verification.residence.new.document_type.residence_card"), 3]] end - def minimum_required_age - (Setting["min_age_to_participate"] || 16).to_i - end - def mask_phone(number) match = number.match(/\d{3}$/) "******#{match}" diff --git a/app/views/admin/local_census_records/_form.html.erb b/app/views/admin/local_census_records/_form.html.erb index c84bb997c..11ef1fb01 100644 --- a/app/views/admin/local_census_records/_form.html.erb +++ b/app/views/admin/local_census_records/_form.html.erb @@ -15,9 +15,7 @@
- <%= f.date_select :date_of_birth, - prompt: true, - start_year: 1900, end_year: minimum_required_age.years.ago.year %> + <%= render Shared::DateOfBirthFieldComponent.new(f) %>
diff --git a/app/views/management/document_verifications/index.html.erb b/app/views/management/document_verifications/index.html.erb index 4c81f65ef..59010bf0b 100644 --- a/app/views/management/document_verifications/index.html.erb +++ b/app/views/management/document_verifications/index.html.erb @@ -17,9 +17,7 @@ <% if Setting.force_presence_date_of_birth? %>
- <%= f.date_select :date_of_birth, - prompt: true, - start_year: 1900, end_year: minimum_required_age.years.ago.year %> + <%= render Shared::DateOfBirthFieldComponent.new(f) %>
<% end %> diff --git a/app/views/management/users/new.html.erb b/app/views/management/users/new.html.erb index e7431d0f3..a5f01c432 100644 --- a/app/views/management/users/new.html.erb +++ b/app/views/management/users/new.html.erb @@ -11,10 +11,7 @@ <%= f.text_field :email, label: t("management.users.email_optional_label") %>
- <%= f.date_select :date_of_birth, - prompt: true, - start_year: 1900, end_year: 16.years.ago.year, - label: t("management.date_of_birth") %> + <%= render Shared::DateOfBirthFieldComponent.new(f, label: t("management.date_of_birth")) %>
<%= f.submit t("management.users.create_user_submit"), class: "button success" %> <% end %> diff --git a/app/views/officing/residence/new.html.erb b/app/views/officing/residence/new.html.erb index 93c510b14..49d6ce437 100644 --- a/app/views/officing/residence/new.html.erb +++ b/app/views/officing/residence/new.html.erb @@ -14,9 +14,7 @@ <% if Setting.force_presence_date_of_birth? %>
- <%= f.date_select :date_of_birth, - prompt: true, - start_year: 1900, end_year: minimum_required_age.years.ago.year %> + <%= render Shared::DateOfBirthFieldComponent.new(f) %>
<% else %>
diff --git a/app/views/verification/residence/new.html.erb b/app/views/verification/residence/new.html.erb index c361d7d4c..9909d41af 100644 --- a/app/views/verification/residence/new.html.erb +++ b/app/views/verification/residence/new.html.erb @@ -57,9 +57,7 @@
- <%= f.date_select :date_of_birth, - prompt: true, - start_year: 1900, end_year: minimum_required_age.years.ago.year %> + <%= render Shared::DateOfBirthFieldComponent.new(f) %>
diff --git a/spec/components/shared/date_of_birth_field_component_spec.rb b/spec/components/shared/date_of_birth_field_component_spec.rb new file mode 100644 index 000000000..8687b84cf --- /dev/null +++ b/spec/components/shared/date_of_birth_field_component_spec.rb @@ -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 diff --git a/spec/system/verification/residence_spec.rb b/spec/system/verification/residence_spec.rb index a44d6e3ae..a806cc378 100644 --- a/spec/system/verification/residence_spec.rb +++ b/spec/system/verification/residence_spec.rb @@ -38,21 +38,6 @@ describe "Residence" do expect(page).to have_content "Residence verified" 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 erased_user = create(:user, document_number: "12345678Z", document_type: "1", erased_at: Time.current) vote = create(:vote, voter: erased_user)