Remove unused custom FeatureToggle implementation

Flipper is now our only source of truth regarding feature toggles.
This commit is contained in:
Maikel Linke
2021-06-18 10:44:46 +10:00
parent bba85e5e79
commit a8eecf963d
2 changed files with 4 additions and 81 deletions

View File

@@ -1,63 +1,15 @@
module OpenFoodNetwork
# This feature toggles implementation provides two mechanisms to conditionally enable features.
# Feature toggles are configured via Flipper.
#
# You can configure features via the Flipper config and web interface. See:
# We define features in the initializer and then it can be customised via the
# web interface on each server.
#
# - config/initializers/flipper.rb
# - http://localhost:3000/admin/feature-toggle/features
#
# Alternatively, you can choose which users have the feature toggled on. To do that you need to
# register the feature and its users from an initializer like:
#
# require 'open_food_network/feature_toggle'
# OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ['ofn@example.com'])
#
# Note, however, that it'd be better to read the user emails from an ENV var provisioned with
# ofn-install:
#
# require 'open_food_network/feature_toggle'
# OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ENV['PRODUCT_TEAM'])
#
# You can then check it from a view like:
#
# - if feature? :new_shiny_feature, spree_current_user
# = render "new_shiny_feature"
#
module FeatureToggle
def self.enabled?(feature_name, user = nil)
features = Thread.current[:features] || {}
if Flipper[feature_name].exist?
Flipper.enabled?(feature_name, user)
else
feature = features.fetch(feature_name, DefaultFeature.new)
feature.enabled?(user)
end
end
def self.enable(feature_name, &block)
Thread.current[:features] ||= {}
Thread.current[:features][feature_name] = Feature.new(block)
end
end
class Feature
def initialize(block)
@block = block
end
def enabled?(user)
block.call(user)
end
private
attr_reader :block
end
class DefaultFeature
def enabled?(_user)
false
Flipper.enabled?(feature_name, user)
end
end
end

View File

@@ -14,34 +14,5 @@ module OpenFoodNetwork
expect(FeatureToggle.enabled?(:foo)).to be true
end
end
context 'when specifying users' do
let(:insider) { build(:user) }
let(:outsider) { build(:user, email: "different") }
context 'and the block does not specify arguments' do
before do
FeatureToggle.enable(:foo) { 'return value' }
end
it "returns the block's return value" do
expect(FeatureToggle.enabled?(:foo, insider)).to eq('return value')
end
end
context 'and the block specifies arguments' do
let(:users) { [insider.email] }
before do
FeatureToggle.enable(:foo) { |user| users.include?(user&.email) }
end
it "returns the block's return value" do
expect(FeatureToggle.enabled?(:foo, insider)).to eq(true)
expect(FeatureToggle.enabled?(:foo, outsider)).to eq(false)
expect(FeatureToggle.enabled?(:foo, nil)).to eq(false)
end
end
end
end
end