feature.rb 3.99 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
require 'flipper/adapters/active_record'
require 'flipper/adapters/active_support_cache_store'

6 7 8 9 10
class Feature
  # Classes to override flipper table names
  class FlipperFeature < Flipper::Adapters::ActiveRecord::Feature
    # Using `self.table_name` won't work. ActiveRecord bug?
    superclass.table_name = 'features'
11 12 13 14

    def self.feature_names
      pluck(:key)
    end
15 16 17 18 19 20 21
  end

  class FlipperGate < Flipper::Adapters::ActiveRecord::Gate
    superclass.table_name = 'feature_gates'
  end

  class << self
22 23
    delegate :group, to: :flipper

24 25 26 27 28 29 30 31
    def all
      flipper.features.to_a
    end

    def get(key)
      flipper.feature(key)
    end

32
    def persisted_names
33
      Gitlab::SafeRequestStore[:flipper_persisted_names] ||= FlipperFeature.feature_names
34 35
    end

36 37 38 39
    def persisted?(feature)
      # Flipper creates on-memory features when asked for a not-yet-created one.
      # If we want to check if a feature has been actually set, we look for it
      # on the persisted features list.
40
      persisted_names.include?(feature.name.to_s)
41 42
    end

43 44
    # use `default_enabled: true` to default the flag to being `enabled`
    # unless set explicitly.  The default is `disabled`
45 46 47
    def enabled?(key, thing = nil, default_enabled: false)
      feature = Feature.get(key)

48 49 50 51 52
      # If we're not default enabling the flag or the feature has been set, always evaluate.
      # `persisted?` can potentially generate DB queries and also checks for inclusion
      # in an array of feature names (177 at last count), possibly reducing performance by half.
      # So we only perform the `persisted` check if `default_enabled: true`
      !default_enabled || Feature.persisted?(feature) ? feature.enabled?(thing) : true
53 54
    end

55
    def disabled?(key, thing = nil, default_enabled: false)
56
      # we need to make different method calls to make it easy to mock / define expectations in test mode
57
      thing.nil? ? !enabled?(key, default_enabled: default_enabled) : !enabled?(key, thing, default_enabled: default_enabled)
58 59
    end

60 61 62 63 64 65
    def enable(key, thing = true)
      get(key).enable(thing)
    end

    def disable(key, thing = false)
      get(key).disable(thing)
66 67
    end

68 69
    def enable_group(key, group)
      get(key).enable_group(group)
70 71
    end

72 73
    def disable_group(key, group)
      get(key).disable_group(group)
74 75
    end

76
    def flipper
77 78 79 80 81
      if Gitlab::SafeRequestStore.active?
        Gitlab::SafeRequestStore[:flipper] ||= build_flipper_instance
      else
        @flipper ||= build_flipper_instance
      end
82 83 84 85
    end

    def build_flipper_instance
      Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
86
    end
87 88 89 90 91 92

    # This method is called from config/initializers/flipper.rb and can be used
    # to register Flipper groups.
    # See https://docs.gitlab.com/ee/development/feature_flags.html#feature-groups
    def register_feature_groups
    end
93 94 95 96 97 98 99 100 101 102 103

    def flipper_adapter
      active_record_adapter = Flipper::Adapters::ActiveRecord.new(
        feature_class: FlipperFeature,
        gate_class: FlipperGate)

      Flipper::Adapters::ActiveSupportCacheStore.new(
        active_record_adapter,
        Rails.cache,
        expires_in: 1.hour)
    end
104
  end
105 106 107 108 109 110 111 112 113

  class Target
    attr_reader :params

    def initialize(params)
      @params = params
    end

    def gate_specified?
114
      %i(user project group feature_group).any? { |key| params.key?(key) }
115 116 117
    end

    def targets
118
      [feature_group, user, project, group].compact
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def feature_group
      return unless params.key?(:feature_group)

      Feature.group(params[:feature_group])
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def user
      return unless params.key?(:user)

      UserFinder.new(params[:user]).find_by_username!
    end

    def project
      return unless params.key?(:project)

      Project.find_by_full_path(params[:project])
    end
142 143 144 145 146 147

    def group
      return unless params.key?(:group)

      Group.find_by_full_path(params[:group])
    end
148
  end
149
end