2020-03-03 04:07:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserDetail < ApplicationRecord
|
2020-07-09 20:09:13 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2021-09-28 11:11:30 -04:00
|
|
|
REGISTRATION_OBJECTIVE_PAIRS = { basics: 0, move_repository: 1, code_storage: 2, exploring: 3, ci: 4, other: 5, joining_team: 6 }.freeze
|
|
|
|
|
2020-03-03 04:07:54 -05:00
|
|
|
belongs_to :user
|
|
|
|
|
2021-06-07 17:10:00 -04:00
|
|
|
validates :pronouns, length: { maximum: 50 }
|
2021-08-04 02:09:49 -04:00
|
|
|
validates :pronunciation, length: { maximum: 255 }
|
2020-03-04 16:07:54 -05:00
|
|
|
validates :job_title, length: { maximum: 200 }
|
2020-07-09 20:09:13 -04:00
|
|
|
validates :bio, length: { maximum: 255 }, allow_blank: true
|
|
|
|
|
2022-10-18 05:11:01 -04:00
|
|
|
DEFAULT_FIELD_LENGTH = 500
|
|
|
|
|
|
|
|
validates :linkedin, length: { maximum: DEFAULT_FIELD_LENGTH }, allow_blank: true
|
|
|
|
validates :twitter, length: { maximum: DEFAULT_FIELD_LENGTH }, allow_blank: true
|
|
|
|
validates :skype, length: { maximum: DEFAULT_FIELD_LENGTH }, allow_blank: true
|
|
|
|
validates :location, length: { maximum: DEFAULT_FIELD_LENGTH }, allow_blank: true
|
|
|
|
validates :organization, length: { maximum: DEFAULT_FIELD_LENGTH }, allow_blank: true
|
|
|
|
validates :website_url, length: { maximum: DEFAULT_FIELD_LENGTH }, url: true, allow_blank: true
|
|
|
|
|
|
|
|
before_validation :sanitize_attrs
|
2020-07-13 08:09:18 -04:00
|
|
|
before_save :prevent_nil_bio
|
|
|
|
|
2021-09-28 11:11:30 -04:00
|
|
|
enum registration_objective: REGISTRATION_OBJECTIVE_PAIRS, _suffix: true
|
|
|
|
|
2022-10-18 05:11:01 -04:00
|
|
|
def self.user_fields_changed?(user)
|
|
|
|
(%w[linkedin skype twitter website_url location organization] & user.changed).any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def sanitize_attrs
|
|
|
|
%i[linkedin skype twitter website_url].each do |attr|
|
|
|
|
value = self[attr]
|
|
|
|
self[attr] = Sanitize.clean(value) if value.present?
|
|
|
|
end
|
|
|
|
%i[location organization].each do |attr|
|
|
|
|
value = self[attr]
|
|
|
|
self[attr] = Sanitize.clean(value).gsub('&', '&') if value.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_changed_fields_from_user
|
|
|
|
self.linkedin = trim_field(user.linkedin) if user.linkedin_changed?
|
|
|
|
self.twitter = trim_field(user.twitter) if user.twitter_changed?
|
|
|
|
self.skype = trim_field(user.skype) if user.skype_changed?
|
|
|
|
self.website_url = trim_field(user.website_url) if user.website_url_changed?
|
|
|
|
self.location = trim_field(user.location) if user.location_changed?
|
|
|
|
self.organization = trim_field(user.organization) if user.organization_changed?
|
|
|
|
end
|
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def prevent_nil_bio
|
|
|
|
self.bio = '' if bio_changed? && bio.nil?
|
|
|
|
end
|
2022-10-18 05:11:01 -04:00
|
|
|
|
|
|
|
def trim_field(value)
|
|
|
|
return '' unless value
|
|
|
|
|
|
|
|
value.first(DEFAULT_FIELD_LENGTH)
|
|
|
|
end
|
2020-03-03 04:07:54 -05:00
|
|
|
end
|
2020-12-03 13:10:10 -05:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
UserDetail.prepend_mod_with('UserDetail')
|