Files
openfoodnetwork/config/initializers/flipper.rb
Gaetan Craig-Riou 172647f1cd Rework group to enable variant tag and inventory
To make it easier to manage, enterprises with no inventory will have
variant tag enabled, and enterprises with inventory will have inventory
enabled.
Add migration to disable old group and enabled new group
2025-11-26 13:29:40 +11:00

100 lines
3.5 KiB
Ruby
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# frozen_string_literal: true
require "flipper"
require "flipper/adapters/active_record"
require "open_food_network/feature_toggle"
Rails.application.configure do
# Disable Flipper's built-in test helper.
# It fails in CI and feature don't get activated.
config.flipper.test_help = false
end
Flipper.configure do |flipper|
# Still use recommended test setup with faster memory adapter:
if Rails.env.test?
# Use a shared Memory adapter for all tests. The adapter is instantiated
# outside of the block so the same instance is returned in new threads.
adapter = Flipper::Adapters::Memory.new
flipper.adapter { adapter }
end
end
# Groups
Flipper.register(:admins) do |actor|
actor.respond_to?(:admin?) && actor.admin?
end
Flipper.register(:new_2024_07_03) do |actor|
actor.respond_to?(:created_at?) && actor.created_at >= Time.zone.parse("2024-07-03")
end
Flipper.register(:enterprise_created_before_2025_08_11) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise
actor.respond_to?(:created_at?) && actor.created_at < Time.zone.parse("2025-08-11")
end
Flipper.register(:enterprise_created_after_2025_08_11) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise
actor.respond_to?(:created_at?) && actor.created_at >= Time.zone.parse("2025-08-11")
end
Flipper.register(:enterprise_with_no_inventory) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise
# Uses 2025-08-11 as filter because variant tag did not exist before that, enterprise created
# after never had access to the inventory
enterprise_with_variant_override = Enterprise
.where(id: VariantOverride.joins(:hub).select(:hub_id))
.where(created_at: ..."2025-08-11")
.distinct
enterprise_with_no_variant_override = Enterprise
.where.not(id: enterprise_with_variant_override)
enterprise_with_no_variant_override.exists?(actor.id)
end
Flipper.register(:enterprise_with_inventory) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise
# Uses 2025-08-11 as filter because variant tag did not exist before that, enterprise created
# after never had access to the inventory
enterprise_with_variant_override = Enterprise
.where(id: VariantOverride.joins(:hub).select(:hub_id))
.where(created_at: ..."2025-08-11")
.distinct
enterprise_with_variant_override.exists?(actor.id)
end
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
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-toggles
TEXT
config.banner_class = 'danger'
end
end
# Add known feature toggles. This may fail if the database isn't setup yet.
begin
OpenFoodNetwork::FeatureToggle.setup!
rescue ActiveRecord::StatementInvalid
nil
end