summaryrefslogtreecommitdiff
path: root/app/finders/feature_flags_finder.rb
blob: 7b38841970d22f2249e7872494a79c96c99b2847 (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
# frozen_string_literal: true

class FeatureFlagsFinder
  attr_reader :project, :params, :current_user

  def initialize(project, current_user, params = {})
    @project = project
    @current_user = current_user
    @params = params
  end

  def execute(preload: true)
    unless Ability.allowed?(current_user, :read_feature_flag, project)
      return Operations::FeatureFlag.none
    end

    items = feature_flags
    items = by_scope(items)

    items = items.preload_relations if preload
    items.ordered
  end

  private

  def feature_flags
    project.operations_feature_flags
  end

  def by_scope(items)
    case params[:scope]
    when 'enabled'
      items.enabled
    when 'disabled'
      items.disabled
    else
      items
    end
  end
end