2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CustomEmoji < ApplicationRecord
|
2021-03-11 19:13:27 +05:30
|
|
|
NAME_REGEXP = /[a-z0-9_-]+/.freeze
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
belongs_to :namespace, inverse_of: :custom_emoji
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id'
|
|
|
|
|
|
|
|
# For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467
|
|
|
|
validates :external, inclusion: { in: [true] }
|
|
|
|
|
|
|
|
validates :file, public_url: true, if: :external
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
validate :valid_emoji_name
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
validates :group, presence: true
|
2020-07-28 23:09:34 +05:30
|
|
|
validates :name,
|
|
|
|
uniqueness: { scope: [:namespace_id, :name] },
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 36 },
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
format: { with: /\A#{NAME_REGEXP}\z/ }
|
|
|
|
|
|
|
|
scope :by_name, -> (names) { where(name: names) }
|
|
|
|
|
|
|
|
alias_attribute :url, :file # this might need a change in https://gitlab.com/gitlab-org/gitlab/-/issues/230467
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_emoji_name
|
|
|
|
if Gitlab::Emoji.emoji_exists?(name)
|
|
|
|
errors.add(:name, _('%{name} is already being used for another emoji') % { name: self.name })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|