Commit 3aea946e authored by Brett Walker's avatar Brett Walker

add 'default_enabled' to feature flags

This allows you to default a feature flag to 'on' when
checking whether it's enabled/disabled.
parent 589776fc
...@@ -58,6 +58,13 @@ Features that are developed and are intended to be merged behind a feature flag ...@@ -58,6 +58,13 @@ Features that are developed and are intended to be merged behind a feature flag
should not include a changelog entry. The entry should be added in the merge should not include a changelog entry. The entry should be added in the merge
request removing the feature flags. request removing the feature flags.
In the rare case that you need the feature flag to be on automatically, use
`default_enabled: true` when checking:
```ruby
Feature.enabled?(:feature_flag, project, default_enabled: true)
```
### Specs ### Specs
In the test environment `Feature.enabled?` is stubbed to always respond to `true`, In the test environment `Feature.enabled?` is stubbed to always respond to `true`,
......
...@@ -42,13 +42,17 @@ class Feature ...@@ -42,13 +42,17 @@ class Feature
persisted_names.include?(feature.name.to_s) persisted_names.include?(feature.name.to_s)
end end
def enabled?(key, thing = nil) def enabled?(key, thing = nil, default_enabled: false)
get(key).enabled?(thing) feature = Feature.get(key)
return feature.enabled?(thing) unless default_enabled
# If the feature has been set, always evaluate
Feature.persisted?(feature) ? feature.enabled?(thing) : true
end end
def disabled?(key, thing = nil) def disabled?(key, thing = nil, default_enabled: false)
# we need to make different method calls to make it easy to mock / define expectations in test mode # we need to make different method calls to make it easy to mock / define expectations in test mode
thing.nil? ? !enabled?(key) : !enabled?(key, thing) thing.nil? ? !enabled?(key, default_enabled: default_enabled) : !enabled?(key, thing, default_enabled: default_enabled)
end end
def enable(key, thing = true) def enable(key, thing = true)
......
...@@ -119,6 +119,10 @@ describe Feature do ...@@ -119,6 +119,10 @@ describe Feature do
expect(described_class.enabled?(:some_random_feature_flag)).to be_falsey expect(described_class.enabled?(:some_random_feature_flag)).to be_falsey
end end
it 'returns true for undefined feature with default_enabled' do
expect(described_class.enabled?(:some_random_feature_flag, default_enabled: true)).to be_truthy
end
it 'returns false for existing disabled feature in the database' do it 'returns false for existing disabled feature in the database' do
described_class.disable(:disabled_feature_flag) described_class.disable(:disabled_feature_flag)
...@@ -160,6 +164,10 @@ describe Feature do ...@@ -160,6 +164,10 @@ describe Feature do
expect(described_class.disabled?(:some_random_feature_flag)).to be_truthy expect(described_class.disabled?(:some_random_feature_flag)).to be_truthy
end end
it 'returns false for undefined feature with default_enabled' do
expect(described_class.disabled?(:some_random_feature_flag, default_enabled: true)).to be_falsey
end
it 'returns true for existing disabled feature in the database' do it 'returns true for existing disabled feature in the database' do
described_class.disable(:disabled_feature_flag) described_class.disable(:disabled_feature_flag)
......
...@@ -4,8 +4,8 @@ module StubFeatureFlags ...@@ -4,8 +4,8 @@ module StubFeatureFlags
# @param [Hash] features where key is feature name and value is boolean whether enabled or not # @param [Hash] features where key is feature name and value is boolean whether enabled or not
def stub_feature_flags(features) def stub_feature_flags(features)
features.each do |feature_name, enabled| features.each do |feature_name, enabled|
allow(Feature).to receive(:enabled?).with(feature_name) { enabled } allow(Feature).to receive(:enabled?).with(feature_name, any_args) { enabled }
allow(Feature).to receive(:enabled?).with(feature_name.to_s) { enabled } allow(Feature).to receive(:enabled?).with(feature_name.to_s, any_args) { enabled }
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment