Skip to content
Snippets Groups Projects
Verified Commit 6e839478 authored by Brian Williams's avatar Brian Williams :two:
Browse files

Address reviewer feedback

parent 849fc47d
No related branches found
No related tags found
1 merge request!145060Add `.actor_from_id` to check feature flags with model id
......@@ -12,6 +12,6 @@ def actor_from_id(model_id)
def flipper_id
return if new_record?
"#{self.class.name}:#{id}"
self.class.actor_from_id(id).flipper_id
end
end
......@@ -555,7 +555,10 @@ Feature.enabled?(:feature_flag_group, group)
Feature.enabled?(:feature_flag_user, user)
```
If you have a model's ID and do not need the model for anything other than checking the feature flag state, you can use `.actor_from_id` in order check the feature flag state without making a database query to retrieve the model.
Models which `include FeatureGate` have an `.actor_from_id` class method.
If you have the model's ID and do not need the model for anything other than checking the feature
flag state, you can use `.actor_from_id` in order check the feature flag state without making a
database query to retrieve the model.
```ruby
# Bad -- Unnecessary query is executed
......
......@@ -9,9 +9,13 @@
subject(:actor_from_id) { model_class.actor_from_id(model_id) }
where(:model_class, :model_id, :expected) do
Project | 1 | 'Project:1'
Group | 2 | 'Group:2'
User | 3 | 'User:3'
Project | 1 | 'Project:1'
Group | 2 | 'Group:2'
User | 3 | 'User:3'
Ci::Runner | 4 | 'Ci::Runner:4'
Namespace | 5 | 'Namespace:5'
Namespaces::ProjectNamespace | 6 | 'Namespaces::ProjectNamespace:6'
Namespaces::UserNamespace | 7 | 'Namespaces::UserNamespace:7'
end
with_them do
......@@ -21,18 +25,22 @@
end
end
describe 'User' do
describe '#flipper_id' do
context 'when user is not persisted' do
let(:user) { build(:user) }
describe '#flipper_id' do
where(:factory) { %i[project group user ci_runner namespace] }
it { expect(user.flipper_id).to be_nil }
with_them do
it 'returns nil when object is not persisted' do
actor = build(factory)
expect(actor.flipper_id).to be_nil
end
context 'when user is persisted' do
let(:user) { create(:user) }
it 'returns flipper_id when object is persisted' do
# rubocop:disable Rails/SaveBang -- This is FactoryBot#create, not ActiveModel#create.
actor = create(factory)
# rubocop:enable Rails/SaveBang
it { expect(user.flipper_id).to eq "User:#{user.id}" }
expect(actor.flipper_id).to eq("#{actor.class.name}:#{actor.id}")
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment