From 9a1b1498bf343a52282ed6d4d885a9406fd00b91 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 22 Mar 2023 14:35:17 +1100 Subject: [PATCH 1/3] Add all feature toggles with descriptions at boot So you can easily inspect and activate new features without trying to use them first. It brings more visibility and will enable us to easily remove retired features as well. --- config/initializers/flipper.rb | 15 +++++++ lib/open_food_network/feature_toggle.rb | 52 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/config/initializers/flipper.rb b/config/initializers/flipper.rb index dd2f97ba2c..69a013f25f 100644 --- a/config/initializers/flipper.rb +++ b/config/initializers/flipper.rb @@ -1,5 +1,6 @@ require "flipper" require "flipper/adapters/active_record" +require "open_food_network/feature_toggle" if Rails.env.production? Flipper::UI.configure do |config| @@ -9,3 +10,17 @@ if Rails.env.production? end Flipper.register(:admins) { |actor| actor.respond_to?(:admin?) && actor.admin? } + +Flipper::UI.configure do |config| + config.descriptions_source = ->(_keys) do + # return has to be hash of {String key => String description} + OpenFoodNetwork::FeatureToggle::CURRENT_FEATURES + end + + # Defaults to false. Set to true to show feature descriptions on the list + # page as well as the view page. + # config.show_feature_description_in_list = true +end + +# Add known feature toggles. This may fail if the database isn't setup yet. +OpenFoodNetwork::FeatureToggle.setup! rescue ActiveRecord::StatementInvalid diff --git a/lib/open_food_network/feature_toggle.rb b/lib/open_food_network/feature_toggle.rb index 888342fab3..981155e5d6 100644 --- a/lib/open_food_network/feature_toggle.rb +++ b/lib/open_food_network/feature_toggle.rb @@ -7,6 +7,58 @@ module OpenFoodNetwork # - http://localhost:3000/admin/feature-toggle/features # module FeatureToggle + # Please add your new feature here to appear in the Flipper UI. + # We way move this to a YAML file when it becomes too awkward. + CURRENT_FEATURES = { + "admin_style_v2" => <<~DESC, + Change some colour and layout in the backend to a newer version. + DESC + "api_reports" => <<~DESC, + An API endpoint for reports at + /api/v0/reports/:report_type(/:report_subtype) + DESC + "api_v1" => <<~DESC, + Enable the new API at /api/v1 + DESC + "background_reports" => <<~DESC, + Generate reports in a background process to limit memory consumption. + DESC + "dfc_provider" => <<~DESC, + Enable the DFC compatible endpoint at /api/dfc-*. + DESC + "match_shipping_categories" => <<~DESC, + During checkout, show only shipping methods that support all + shipping categories. Activating this feature for an enterprise owner + will activate it for all shops of this enterprise. + DESC + "new_products_page" => <<~DESC, + Show the new (experimental) version of the admin products page. + DESC + "split_checkout" => <<~DESC, + Replace the one-page checkout with a multi-step checkout. + DESC + }.freeze + + # Move your feature entry from CURRENT_FEATURES to RETIRED_FEATURES when + # you remove it from the code. It will then be deleted from the database. + # + # We may delete this field one day and regard all features not listed in + # CURRENT_FEATURES as unsupported and remove them. But until this approach + # is accepted we delete only the features listed here. + RETIRED_FEATURES = {}.freeze + + def self.setup! + CURRENT_FEATURES.each_key do |name| + feature = Flipper.feature(name) + feature.add unless feature.exist? + end + + RETIRED_FEATURES.each_key do |name| + feature = Flipper.feature(name) + feature.remove if feature.exist? + end + end + def self.enabled?(feature_name, user = nil) feature = Flipper.feature(feature_name) feature.add unless feature.exist? From fd68cbf56dab10dcc380c8e8e642bd1ad7484b0e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 22 Mar 2023 14:40:20 +1100 Subject: [PATCH 2/3] Remove on-the-fly adding of feature toggle entries We now have a new source of truth. You shouldn't use a feature toggle without adding it to OpenFoodNetwork::FeatureToggle first. All toggles are added at boot time already. --- lib/open_food_network/feature_toggle.rb | 4 +--- spec/lib/open_food_network/feature_toggle_spec.rb | 7 ------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/open_food_network/feature_toggle.rb b/lib/open_food_network/feature_toggle.rb index 981155e5d6..e0b8e49451 100644 --- a/lib/open_food_network/feature_toggle.rb +++ b/lib/open_food_network/feature_toggle.rb @@ -60,9 +60,7 @@ module OpenFoodNetwork end def self.enabled?(feature_name, user = nil) - feature = Flipper.feature(feature_name) - feature.add unless feature.exist? - feature.enabled?(user) + Flipper.enabled?(feature_name, user) end def self.disabled?(feature_name, user = nil) diff --git a/spec/lib/open_food_network/feature_toggle_spec.rb b/spec/lib/open_food_network/feature_toggle_spec.rb index ce18fb5bba..97e41108a6 100644 --- a/spec/lib/open_food_network/feature_toggle_spec.rb +++ b/spec/lib/open_food_network/feature_toggle_spec.rb @@ -13,13 +13,6 @@ module OpenFoodNetwork Flipper.enable(:foo) expect(FeatureToggle.enabled?(:foo)).to be true end - - it "adds features to the database for easy admin in the UI" do - feature = Flipper.feature(:sparkling_new) - - expect { FeatureToggle.enabled?(:sparkling_new) }. - to change { feature.exist? }.from(false).to(true) - end end end end From e0fd180edd1797e0ab1f33ae3c0daa196b50f7b0 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 22 Mar 2023 15:45:26 +1100 Subject: [PATCH 3/3] DRY Flipper UI config --- config/initializers/flipper.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/config/initializers/flipper.rb b/config/initializers/flipper.rb index 69a013f25f..42cee7e0d6 100644 --- a/config/initializers/flipper.rb +++ b/config/initializers/flipper.rb @@ -2,13 +2,6 @@ require "flipper" require "flipper/adapters/active_record" require "open_food_network/feature_toggle" -if Rails.env.production? - Flipper::UI.configure do |config| - config.banner_text = '⚠️ Production environment: be aware that the changes have an impact on the application. Please, read the how-to before: https://github.com/openfoodfoundation/openfoodnetwork/wiki/Feature-toggle-with-Flipper' - config.banner_class = 'danger' - end -end - Flipper.register(:admins) { |actor| actor.respond_to?(:admin?) && actor.admin? } Flipper::UI.configure do |config| @@ -20,6 +13,15 @@ Flipper::UI.configure do |config| # Defaults to false. Set to true to show feature descriptions on the list # page as well as the view page. # config.show_feature_description_in_list = true + + if Rails.env.production? + config.banner_text = <<~TEXT + ⚠️ Production environment: be aware that the changes have an impact on the + application. Please read the how-to before: + https://github.com/openfoodfoundation/openfoodnetwork/wiki/Feature-toggle-with-Flipper + TEXT + config.banner_class = 'danger' + end end # Add known feature toggles. This may fail if the database isn't setup yet.