Skip to content

Commit

Permalink
Merge pull request #27 from ResultadosDigitais/release-a-feature-for-all
Browse files Browse the repository at this point in the history
Release a feature for all
  • Loading branch information
geisonbiazus committed Oct 24, 2016
2 parents 2d03992 + ec44b5a commit ab88f83
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,15 @@ FeatureFlagger::KeyNotFoundError: ["account", "email_marketing", "new_email_flo"

# Get an array with all released Account ids
Account.all_released_ids_for(:email_marketing, :new_email_flow)

# Releasing a feature to all accounts
Account.release_to_all(:email_marketing, :new_email_flow)

# Unreleasing a feature to all accounts
Account.unrelease_to_all(:email_marketing, :new_email_flow)

# Return an array with all features released for all
Account.released_features_to_all
```

## Contributing
Expand Down
16 changes: 15 additions & 1 deletion lib/feature_flagger/control.rb
Expand Up @@ -2,18 +2,24 @@ module FeatureFlagger
class Control
attr_reader :storage

RELEASED_FEATURES = 'released_features'

def initialize(storage)
@storage = storage
end

def rollout?(feature_key, resource_id)
@storage.has_value?(feature_key, resource_id)
@storage.has_value?(RELEASED_FEATURES, feature_key) || @storage.has_value?(feature_key, resource_id)
end

def release(feature_key, resource_id)
@storage.add(feature_key, resource_id)
end

def release_to_all(feature_key)
@storage.add(RELEASED_FEATURES, feature_key)
end

# <b>DEPRECATED:</b> Please use <tt>release</tt> instead.
def release!(feature_key, resource_id)
warn "[DEPRECATION] `release!` is deprecated. Please use `release` instead."
Expand All @@ -24,6 +30,10 @@ def unrelease(feature_key, resource_id)
@storage.remove(feature_key, resource_id)
end

def unrelease_to_all(feature_key)
@storage.remove(RELEASED_FEATURES, feature_key)
end

# <b>DEPRECATED:</b> Please use <tt>unrelease</tt> instead.
def unrelease!(feature_key, resource_id)
warn "[DEPRECATION] `unrelease!` is deprecated. Please use `unrelease` instead."
Expand All @@ -33,5 +43,9 @@ def unrelease!(feature_key, resource_id)
def resource_ids(feature_key)
@storage.all_values(feature_key)
end

def released_features_to_all
@storage.all_values(RELEASED_FEATURES)
end
end
end
14 changes: 14 additions & 0 deletions lib/feature_flagger/model.rb
Expand Up @@ -49,6 +49,20 @@ def all_released_ids_for(*feature_key)
FeatureFlagger.control.resource_ids(feature.key)
end

def release_to_all(*feature_key)
feature = Feature.new(feature_key, rollout_resource_name)
FeatureFlagger.control.release_to_all(feature.key)
end

def unrelease_to_all(*feature_key)
feature = Feature.new(feature_key, rollout_resource_name)
FeatureFlagger.control.unrelease_to_all(feature.key)
end

def released_features_to_all
FeatureFlagger.control.released_features_to_all
end

def rollout_resource_name
klass_name = self.to_s
klass_name.gsub!(/::/, '_')
Expand Down
36 changes: 36 additions & 0 deletions spec/feature_flagger/control_spec.rb
Expand Up @@ -17,11 +17,21 @@ module FeatureFlagger

context 'when resource entity id has no access to release_key' do
it { expect(result).to be_falsey }

context 'and a feature is release to all' do
before { storage.add(FeatureFlagger::Control::RELEASED_FEATURES, key) }
it { expect(result).to be_truthy }
end
end

context 'when resource entity id has access to release_key' do
before { storage.add(key, resource_id) }
it { expect(result).to be_truthy }

context 'and a feature is release to all' do
before { storage.add(FeatureFlagger::Control::RELEASED_FEATURES, key) }
it { expect(result).to be_truthy }
end
end
end

Expand All @@ -32,6 +42,13 @@ module FeatureFlagger
end
end

describe '#release_to_all' do
it 'adds feature_key to storage' do
control.release_to_all(key)
expect(storage).to have_value(FeatureFlagger::Control::RELEASED_FEATURES, key)
end
end

describe '#unrelease' do
it 'removes resource_id from storage' do
storage.add(key, resource_id)
Expand All @@ -40,6 +57,14 @@ module FeatureFlagger
end
end

describe '#unrelease_to_all' do
it 'removes feature_key to storage' do
storage.add(FeatureFlagger::Control::RELEASED_FEATURES, key)
control.unrelease_to_all(key)
expect(storage).not_to have_value(FeatureFlagger::Control::RELEASED_FEATURES, key)
end
end

describe '#resource_ids' do
subject { control.resource_ids(key) }

Expand All @@ -50,5 +75,16 @@ module FeatureFlagger
is_expected.to match_array %w(1 2 15)
end
end

describe '#released_features_to_all' do
subject { control.released_features_to_all }

it 'returns all the values to given features' do
control.release(FeatureFlagger::Control::RELEASED_FEATURES, 'feature::name1')
control.release(FeatureFlagger::Control::RELEASED_FEATURES, 'feature::name2')
control.release(FeatureFlagger::Control::RELEASED_FEATURES, 'feature::name15')
is_expected.to match_array %w(feature::name1 feature::name2 feature::name15)
end
end
end
end
21 changes: 21 additions & 0 deletions spec/feature_flagger/model_spec.rb
Expand Up @@ -45,5 +45,26 @@ def id; 14 end
DummyClass.all_released_ids_for(key)
end
end

describe '.release_to_all' do
it 'calls Control#release_to_all with appropriated methods' do
expect(control).to receive(:release_to_all).with(resolved_key)
DummyClass.release_to_all(key)
end
end

describe '.unrelease_to_all' do
it 'calls Control#unrelease_to_all with appropriated methods' do
expect(control).to receive(:unrelease_to_all).with(resolved_key)
DummyClass.unrelease_to_all(key)
end
end

describe '.released_features_to_all' do
it 'calls Control#resource_ids with appropriated methods' do
expect(control).to receive(:released_features_to_all)
DummyClass.released_features_to_all
end
end
end
end

0 comments on commit ab88f83

Please sign in to comment.