summaryrefslogtreecommitdiff
path: root/app/models/user_detail.rb
blob: 47537e5885f5d4b50dbaeaabb17d0f50795ecf8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

class UserDetail < ApplicationRecord
  extend ::Gitlab::Utils::Override
  include CacheMarkdownField

  belongs_to :user

  validates :pronouns, length: { maximum: 50 }
  validates :job_title, length: { maximum: 200 }
  validates :bio, length: { maximum: 255 }, allow_blank: true

  before_save :prevent_nil_bio

  cache_markdown_field :bio

  def bio_html
    read_attribute(:bio_html) || bio
  end

  # For backward compatibility.
  # Older migrations (and their tests) reference the `User.migration_bot` where the `bio` attribute is set.
  # Here we disable writing the markdown cache when the `bio_html` column does not exist.
  override :invalidated_markdown_cache?
  def invalidated_markdown_cache?
    self.class.column_names.include?('bio_html') && super
  end

  private

  def prevent_nil_bio
    self.bio = '' if bio_changed? && bio.nil?
  end
end

UserDetail.prepend_mod_with('UserDetail')