BigW Consortium Gitlab

feature.rb 1.68 KB
Newer Older
1 2 3 4 5
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'
6 7 8 9

    def self.feature_names
      pluck(:key)
    end
10 11 12 13 14 15 16
  end

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

  class << self
17 18
    delegate :group, to: :flipper

19 20 21 22 23 24 25 26
    def all
      flipper.features.to_a
    end

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

27 28 29 30 31 32 33 34
    def persisted_names
      if RequestStore.active?
        RequestStore[:flipper_persisted_names] ||= FlipperFeature.feature_names
      else
        FlipperFeature.feature_names
      end
    end

35 36 37 38
    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.
39
      persisted_names.include?(feature.name)
40 41
    end

42 43 44 45 46 47 48 49 50 51
    def enabled?(key, thing = nil)
      get(key).enabled?(thing)
    end

    def enable(key, thing = true)
      get(key).enable(thing)
    end

    def disable(key, thing = false)
      get(key).disable(thing)
52 53
    end

54 55
    def enable_group(key, group)
      get(key).enable_group(group)
56 57
    end

58 59
    def disable_group(key, group)
      get(key).disable_group(group)
60 61
    end

62
    def flipper
63
      @flipper ||= Flipper.instance
64
    end
65 66 67 68 69 70

    # 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
71 72
  end
end