gitlab-org--gitlab-foss/lib/feature.rb
Rémy Coutable b4d325c80c Allow the feature flags to be enabled/disabled with more granularity
This allows to enable/disable a feature flag for a given user, or a
given Flipper group (must be declared statically in the `flipper.rb`
initializer beforehand).

Signed-off-by: Rémy Coutable <remy@rymai.me>
2017-06-27 18:59:51 +02:00

61 lines
1.4 KiB
Ruby

require 'flipper/adapters/active_record'
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'
end
class FlipperGate < Flipper::Adapters::ActiveRecord::Gate
superclass.table_name = 'feature_gates'
end
class << self
delegate :group, to: :flipper
def all
flipper.features.to_a
end
def get(key)
flipper.feature(key)
end
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.
all.map(&:name).include?(feature.name)
end
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)
end
def enable_group(key, group)
get(key).enable_group(group)
end
def disable_group(key, group)
get(key).disable_group(group)
end
def flipper
@flipper ||= begin
adapter = Flipper::Adapters::ActiveRecord.new(
feature_class: FlipperFeature, gate_class: FlipperGate)
Flipper.new(adapter)
end
end
end
end