-
-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GA toggle #6657
GA toggle #6657
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module OpenFoodNetwork | ||
class Feature | ||
def initialize(users = []) | ||
@users = users | ||
end | ||
|
||
def enabled?(user) | ||
users.include?(user.email) | ||
end | ||
|
||
private | ||
|
||
attr_reader :users | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
require 'open_food_network/feature' | ||
require 'open_food_network/null_feature' | ||
require 'open_food_network/ga_feature' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to move all the feature toggles code to lib/feature_toggle/ but doing it now will likely mess up with the work I'm doing in #6643. This is good enough to keep on going IMO, and I'll finish later on. |
||
|
||
module OpenFoodNetwork | ||
# This feature toggles implementation provides two mechanisms to conditionally enable features. | ||
# | ||
|
@@ -9,16 +13,17 @@ module OpenFoodNetwork | |
# = render "new_shiny_feature" | ||
# | ||
# 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: | ||
# register the feature and its users using `.enable` like: | ||
# | ||
# require 'open_food_network/feature_toggle' | ||
# OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ['[email protected]']) | ||
# | ||
# Note, however, that it'd be better to read the user emails from an ENV var provisioned with | ||
# ofn-install: | ||
# This is handled in config/initializers/feature_toggles.rb. | ||
# | ||
# There's also the option to toggle something on for everyone using "all" instead of an email: | ||
# | ||
# require 'open_food_network/feature_toggle' | ||
# OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ENV['PRODUCT_TEAM']) | ||
# OpenFoodNetwork::FeatureToggle.enable(:new_shiny_feature, ['all']) | ||
# | ||
# This doesn't require a deployment but to change the ENV var and restart Unicorn and DJ. | ||
# | ||
# You can then check it from a view like: | ||
# | ||
|
@@ -34,7 +39,9 @@ def self.enable(feature_name, user_emails) | |
return unless user_emails.present? | ||
|
||
Thread.current[:features] ||= {} | ||
Thread.current[:features][feature_name] = Feature.new(user_emails) | ||
|
||
klass = user_emails == ["all"] ? GAFeature : Feature | ||
Thread.current[:features][feature_name] = klass.new(user_emails) | ||
end | ||
|
||
def initialize | ||
|
@@ -66,24 +73,4 @@ def true?(value) | |
value.to_s.casecmp("true").zero? | ||
end | ||
end | ||
|
||
class Feature | ||
def initialize(users = []) | ||
@users = users | ||
end | ||
|
||
def enabled?(user) | ||
users.include?(user.email) | ||
end | ||
|
||
private | ||
|
||
attr_reader :users | ||
end | ||
|
||
class NullFeature | ||
def enabled?(_user) | ||
false | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module OpenFoodNetwork | ||
class GAFeature | ||
def initialize(_users); end | ||
|
||
def enabled?(_user) | ||
true | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module OpenFoodNetwork | ||
class NullFeature | ||
def enabled?(_user) | ||
false | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,19 @@ def stub_foo(value) | |
end | ||
end | ||
|
||
context 'and the feature is enabled for "all"' do | ||
before { FeatureToggle.enable(:foo, ["all"]) } | ||
|
||
it 'returns true' do | ||
expect(FeatureToggle.enabled?(:foo, user)).to eq(true) | ||
end | ||
end | ||
|
||
context 'and the feature is disabled for them' do | ||
before { FeatureToggle.enable(:foo, []) } | ||
|
||
it 'returns false' do | ||
expect(FeatureToggle.enabled?(:foo, user)).to eq(false) | ||
expect(FeatureToggle.enabled?(:foo, user)).to eq(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this WIP? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it wasn't until I found this 😓 |
||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.