-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Flipflop gem for feature-toggling
Add the Flipflop gem to enable feature-toggling within the publisher app. The dashboard (available at `/flipflop`) provided is not secured in any way (but since we will be using the "Cookie" strategy, users will only be able to toggle features on/off for themselves). This could be added later, if desired. Also provides a `FeatureConstraint` class to enable dynamic routing to be set up based on the value of the feature toggles.
- Loading branch information
1 parent
0328221
commit 8c85c76
Showing
9 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class FeatureConstraint | ||
def initialize(feature_name) | ||
@feature_name = feature_name | ||
end | ||
|
||
def matches?(request) | ||
if request.cookies.key?(@feature_name) | ||
request.cookies[@feature_name] == "1" | ||
else | ||
Flipflop.enabled?(@feature_name.to_sym) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Flipflop.configure do | ||
# Strategies will be used in the order listed here. | ||
strategy :cookie | ||
strategy :default | ||
|
||
if Rails.env.test? | ||
feature :feature_for_tests, | ||
default: true, | ||
description: "A feature only used by tests; not to be used for any actual features." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "test_helper" | ||
|
||
class FeatureConstraintTest < ActiveSupport::TestCase | ||
context "Feature 'feature_for_tests' is enabled by default" do | ||
setup do | ||
test_strategy = Flipflop::FeatureSet.current.test! | ||
test_strategy.switch!(:feature_for_tests, true) | ||
end | ||
|
||
should "match when a request cookie explicitly enables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "1" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal true, feature_constraint.matches?(request) | ||
end | ||
|
||
should "not match when a request cookie explicitly disables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "0" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal false, feature_constraint.matches?(request) | ||
end | ||
|
||
should "match when a request cookie does not override default feature status" do | ||
request = stub(cookies: {}) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal true, feature_constraint.matches?(request) | ||
end | ||
end | ||
|
||
context "Feature 'feature_for_tests' is disabled by default" do | ||
setup do | ||
test_strategy = Flipflop::FeatureSet.current.test! | ||
test_strategy.switch!(:feature_for_tests, false) | ||
end | ||
|
||
should "match when a request cookie explicitly enables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "1" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal true, feature_constraint.matches?(request) | ||
end | ||
|
||
should "not match when a request cookie explicitly disables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "0" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal false, feature_constraint.matches?(request) | ||
end | ||
|
||
should "not match when a request cookie does not override default feature status" do | ||
request = stub(cookies: {}) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal false, feature_constraint.matches?(request) | ||
end | ||
end | ||
end |