debian-mirror-gitlab/lib/feature.rb

104 lines
3.1 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
require 'flipper/adapters/active_record'
require 'flipper/adapters/active_support_cache_store'
2017-09-10 17:25:29 +05:30
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'
2018-03-17 18:26:18 +05:30
def self.feature_names
pluck(:key)
end
2017-09-10 17:25:29 +05:30
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
2018-03-17 18:26:18 +05:30
def persisted_names
2018-12-05 23:21:45 +05:30
Gitlab::SafeRequestStore[:flipper_persisted_names] ||= FlipperFeature.feature_names
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
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.
2018-11-18 11:00:15 +05:30
persisted_names.include?(feature.name.to_s)
2017-09-10 17:25:29 +05:30
end
2018-11-20 20:47:30 +05:30
# use `default_enabled: true` to default the flag to being `enabled`
# unless set explicitly. The default is `disabled`
def enabled?(key, thing = nil, default_enabled: false)
feature = Feature.get(key)
# 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
2017-09-10 17:25:29 +05:30
end
2018-11-20 20:47:30 +05:30
def disabled?(key, thing = nil, default_enabled: false)
# we need to make different method calls to make it easy to mock / define expectations in test mode
thing.nil? ? !enabled?(key, default_enabled: default_enabled) : !enabled?(key, thing, default_enabled: default_enabled)
2018-11-18 11:00:15 +05:30
end
2017-09-10 17:25:29 +05:30
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
2018-12-05 23:21:45 +05:30
if Gitlab::SafeRequestStore.active?
Gitlab::SafeRequestStore[:flipper] ||= build_flipper_instance
2018-11-08 19:23:39 +05:30
else
@flipper ||= build_flipper_instance
end
end
def build_flipper_instance
Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
2017-09-10 17:25:29 +05:30
end
# 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
2018-11-08 19:23:39 +05:30
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
2017-09-10 17:25:29 +05:30
end
end