Welcome to mirror list, hosted at ThFree Co, Russian Federation.

emoji_name_validator.rb « gitlab « validators « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c034a79214b256c36be8e03e7401b15e03525acb (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
# frozen_string_literal: true

# Gitlab::EmojiNameValidator
#
# Validates that the provided value matches an indexed emoji alpha code
#
# @example Usage
#    class AwardEmoji < ApplicationRecord
#      validate :name, 'gitlab/emoji_name':  true
#    end
module Gitlab
  class EmojiNameValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      return if valid_tanuki_emoji?(value)
      return if valid_custom_emoji?(record, value)

      record.errors.add(attribute, (options[:message] || 'is not a valid emoji name'))
    end

    private

    def valid_tanuki_emoji?(value)
      TanukiEmoji.find_by_alpha_code(value.to_s)
    end

    def valid_custom_emoji?(record, value)
      resource = record.try(:resource_parent)

      CustomEmoji.for_resource(resource).by_name(value.to_s).any?
    end
  end
end