2018-04-19 12:53:07 -04:00
|
|
|
require 'flipper/adapters/active_record'
|
|
|
|
require 'flipper/adapters/active_support_cache_store'
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
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'
|
2017-10-30 11:10:31 -04:00
|
|
|
|
|
|
|
def self.feature_names
|
|
|
|
pluck(:key)
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class FlipperGate < Flipper::Adapters::ActiveRecord::Gate
|
|
|
|
superclass.table_name = 'feature_gates'
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2017-06-21 10:49:51 -04:00
|
|
|
delegate :group, to: :flipper
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
def all
|
|
|
|
flipper.features.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key)
|
|
|
|
flipper.feature(key)
|
|
|
|
end
|
|
|
|
|
2017-10-30 11:10:31 -04:00
|
|
|
def persisted_names
|
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore[:flipper_persisted_names] ||= FlipperFeature.feature_names
|
|
|
|
else
|
|
|
|
FlipperFeature.feature_names
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
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.
|
2017-10-30 11:10:31 -04:00
|
|
|
persisted_names.include?(feature.name)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
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)
|
2017-05-12 12:44:03 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
def enable_group(key, group)
|
|
|
|
get(key).enable_group(group)
|
2017-05-12 12:44:03 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
def disable_group(key, group)
|
|
|
|
get(key).disable_group(group)
|
2017-05-12 12:44:03 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
def flipper
|
2018-04-19 12:53:07 -04:00
|
|
|
Thread.current[:flipper] ||=
|
|
|
|
Flipper.new(flipper_adapter).tap { |flip| flip.memoize = true }
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
2017-07-10 09:54:49 -04:00
|
|
|
|
|
|
|
# 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-04-19 12:53:07 -04:00
|
|
|
|
|
|
|
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-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
end
|