summaryrefslogtreecommitdiff
path: root/app/serializers/feature_flag_entity.rb
blob: 80cf869a389ef0f1ee22f2790f9609a58cfaf636 (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
42
43
44
45
46
47
48
# frozen_string_literal: true

class FeatureFlagEntity < Grape::Entity
  include RequestAwareEntity

  expose :id
  expose :iid
  expose :active
  expose :created_at
  expose :updated_at
  expose :name
  expose :description
  expose :version

  expose :edit_path, if: -> (feature_flag, _) { can_update?(feature_flag) } do |feature_flag|
    edit_project_feature_flag_path(feature_flag.project, feature_flag)
  end

  expose :update_path, if: -> (feature_flag, _) { can_update?(feature_flag) } do |feature_flag|
    project_feature_flag_path(feature_flag.project, feature_flag)
  end

  expose :destroy_path, if: -> (feature_flag, _) { can_destroy?(feature_flag) } do |feature_flag|
    project_feature_flag_path(feature_flag.project, feature_flag)
  end

  expose :scopes, with: FeatureFlagScopeEntity do |feature_flag|
    feature_flag.scopes.sort_by(&:id)
  end

  expose :strategies, with: FeatureFlags::StrategyEntity do |feature_flag|
    feature_flag.strategies.sort_by(&:id)
  end

  private

  def can_update?(feature_flag)
    can?(current_user, :update_feature_flag, feature_flag)
  end

  def can_destroy?(feature_flag)
    can?(current_user, :destroy_feature_flag, feature_flag)
  end

  def current_user
    request.current_user
  end
end