summaryrefslogtreecommitdiff
path: root/app/models/operations/feature_flags/user_list.rb
blob: 3e492eaa892fb7ea0903c68e91bc110f87b53cb9 (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
37
38
39
40
41
# frozen_string_literal: true

module Operations
  module FeatureFlags
    class UserList < ApplicationRecord
      include AtomicInternalId
      include IidRoutes
      include ::Gitlab::SQL::Pattern

      self.table_name = 'operations_user_lists'

      belongs_to :project
      has_many :strategy_user_lists
      has_many :strategies, through: :strategy_user_lists

      has_internal_id :iid, scope: :project, presence: true

      validates :project, presence: true
      validates :name,
        presence: true,
        uniqueness: { scope: :project_id },
        length: 1..255
      validates :user_xids, feature_flag_user_xids: true

      before_destroy :ensure_no_associated_strategies

      scope :for_name_like, -> (query) do
        fuzzy_search(query, [:name], use_minimum_char_limit: false)
      end

      private

      def ensure_no_associated_strategies
        if strategies.present?
          errors.add(:base, 'User list is associated with a strategy')
          throw :abort # rubocop: disable Cop/BanCatchThrow
        end
      end
    end
  end
end