Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/open_food_network/feature.rb
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
41 changes: 14 additions & 27 deletions lib/open_food_network/feature_toggle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'open_food_network/feature'
Copy link

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.

require 'open_food_network/null_feature'
require 'open_food_network/ga_feature'
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
#
Expand All @@ -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:
#
Expand All @@ -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
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions lib/open_food_network/ga_feature.rb
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
9 changes: 9 additions & 0 deletions lib/open_food_network/null_feature.rb
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
10 changes: 9 additions & 1 deletion spec/lib/open_food_network/feature_toggle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this WIP?

Copy link
Contributor Author

@sauloperez sauloperez Jan 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wasn't until I found this 😓

end
end
end
Expand Down