mirror of https://github.com/mastodon/mastodon
Add age verification on sign-up (#34150)
parent
4a6cf67c46
commit
d213c585ff
@ -0,0 +1,31 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
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,
|
||||
].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('.')
|
||||
|
||||
safe_join(Array.new(3) 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"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_id(index)
|
||||
"#{object_name}_#{attribute_name}_#{index + 1}i"
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DateOfBirthValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
record.errors.add(attribute, :below_limit) if value.present? && value.to_date > min_age.ago
|
||||
rescue Date::Error
|
||||
record.errors.add(attribute, :invalid)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def min_age
|
||||
Setting.min_age.to_i.years
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddAgeVerifiedAtToUsers < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :users, :age_verified_at, :datetime
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,51 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe DateOfBirthValidator do
|
||||
let(:record_class) do
|
||||
Class.new do
|
||||
include ActiveModel::Validations
|
||||
|
||||
attr_accessor :date_of_birth
|
||||
|
||||
validates :date_of_birth, date_of_birth: true
|
||||
end
|
||||
end
|
||||
|
||||
let(:record) { record_class.new }
|
||||
|
||||
before do
|
||||
Setting.min_age = 16
|
||||
end
|
||||
|
||||
describe '#validate_each' do
|
||||
context 'with an invalid date' do
|
||||
it 'adds errors' do
|
||||
record.date_of_birth = '76.830.10'
|
||||
|
||||
expect(record).to_not be_valid
|
||||
expect(record.errors.first.attribute).to eq(:date_of_birth)
|
||||
expect(record.errors.first.type).to eq(:invalid)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a date below age limit' do
|
||||
it 'adds errors' do
|
||||
record.date_of_birth = 13.years.ago.strftime('%d.%m.%Y')
|
||||
|
||||
expect(record).to_not be_valid
|
||||
expect(record.errors.first.attribute).to eq(:date_of_birth)
|
||||
expect(record.errors.first.type).to eq(:below_limit)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a date above age limit' do
|
||||
it 'does not add errors' do
|
||||
record.date_of_birth = 16.years.ago.strftime('%d.%m.%Y')
|
||||
|
||||
expect(record).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue