FeatureFlag.enabled_for_user(_id) (#19929)
* FeatureFlag can work with user_id * Use the new methods * No thank you, rubocop * Some tests would be nice * Try to appease codecov --------- Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
This commit is contained in:
parent
945587b75d
commit
c5731ac91a
4 changed files with 83 additions and 3 deletions
|
|
@ -288,7 +288,7 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def feature_flag_enabled?(flag_name, acting_as: current_user)
|
||||
FeatureFlag.enabled?(flag_name, FeatureFlag::Actor[acting_as])
|
||||
FeatureFlag.enabled_for_user?(flag_name, acting_as)
|
||||
end
|
||||
|
||||
helper_method :feature_flag_enabled?
|
||||
|
|
|
|||
|
|
@ -8,13 +8,21 @@ module FeatureFlag
|
|||
end
|
||||
|
||||
def flipper_id
|
||||
respond_to?(:id) ? id : nil
|
||||
respond_to?(:id) ? id : self
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
delegate :add, :disable, :enable, :enabled?, :exist?, :remove, to: Flipper
|
||||
|
||||
def enabled_for_user?(flag_name, user)
|
||||
enabled?(flag_name, FeatureFlag::Actor[user])
|
||||
end
|
||||
|
||||
def enabled_for_user_id?(flag_name, user_id)
|
||||
enabled?(flag_name, FeatureFlag::Actor[user_id])
|
||||
end
|
||||
|
||||
# @!method FeatureFlag.enabled?(feature_flag_name, *args)
|
||||
#
|
||||
# Answers if the :feature_flag_name has been _explicitly_ **enabled**.
|
||||
|
|
|
|||
19
spec/controllers/application_controller_spec.rb
Normal file
19
spec/controllers/application_controller_spec.rb
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "ApplicationController", type: :request do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:controller) { ApplicationController.new }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:current_user).and_return(user)
|
||||
end
|
||||
|
||||
describe "#feature_flag_enabled?" do
|
||||
it "calls FeatureFlag.enabled_for_user with current_user" do
|
||||
allow(FeatureFlag).to receive(:enabled_for_user?)
|
||||
controller.feature_flag_enabled?("flag_name")
|
||||
expect(FeatureFlag).to have_received(:enabled_for_user?)
|
||||
.with("flag_name", user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -49,6 +49,36 @@ describe FeatureFlag, type: :service do
|
|||
end
|
||||
end
|
||||
|
||||
describe ".enabled_for_user?" do
|
||||
subject(:method_call) { described_class.enabled?(flag) }
|
||||
|
||||
let(:flag) { "foo" }
|
||||
let(:user) { build(:user) }
|
||||
|
||||
it "calls Flipper's enabled? method" do
|
||||
allow(Flipper).to receive(:enabled?)
|
||||
|
||||
described_class.enabled_for_user?(flag, user)
|
||||
|
||||
expect(Flipper).to have_received(:enabled?).with(flag, instance_of(FeatureFlag::Actor))
|
||||
end
|
||||
end
|
||||
|
||||
describe ".enabled_for_user_id?" do
|
||||
subject(:method_call) { described_class.enabled?(flag) }
|
||||
|
||||
let(:flag) { "foo" }
|
||||
let(:user) { build(:user) }
|
||||
|
||||
it "calls Flipper's enabled? method" do
|
||||
allow(Flipper).to receive(:enabled?)
|
||||
|
||||
described_class.enabled_for_user_id?(flag, user.id)
|
||||
|
||||
expect(Flipper).to have_received(:enabled?).with(flag, instance_of(FeatureFlag::Actor))
|
||||
end
|
||||
end
|
||||
|
||||
describe ".exist?" do
|
||||
it "calls Flipper's exist? method" do
|
||||
allow(Flipper).to receive(:exist?).with("foo")
|
||||
|
|
@ -60,7 +90,7 @@ describe FeatureFlag, type: :service do
|
|||
end
|
||||
|
||||
describe ".accessible?" do
|
||||
let(:user) { UserStruct.new(flipper_id: 1) }
|
||||
let(:user) { UserStruct.new({ flipper_id: 1 }) }
|
||||
|
||||
it "returns false when flag doesn't exist" do
|
||||
expect(described_class.accessible?("missing_flag")).to be(true)
|
||||
|
|
@ -109,4 +139,27 @@ describe FeatureFlag, type: :service do
|
|||
expect(described_class.all).to eq({ flag1: :on, flag2: :off })
|
||||
end
|
||||
end
|
||||
|
||||
describe FeatureFlag::Actor, type: :service do
|
||||
context "when user object" do
|
||||
subject(:actor) { described_class[user] }
|
||||
|
||||
let(:user_id) { 123 }
|
||||
let(:user) { User.new id: user_id }
|
||||
|
||||
it "represents flipper_id as user.id" do
|
||||
expect(actor.flipper_id).to eq(user_id)
|
||||
end
|
||||
end
|
||||
|
||||
context "when user_id" do
|
||||
subject(:actor) { described_class[user_id] }
|
||||
|
||||
let(:user_id) { 123 }
|
||||
|
||||
it "represents flipper_id as user.id" do
|
||||
expect(actor.flipper_id).to eq(user_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue