diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d4918484c..7235ce41d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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? diff --git a/app/services/feature_flag.rb b/app/services/feature_flag.rb index c75b4ad88..e9efe718b 100644 --- a/app/services/feature_flag.rb +++ b/app/services/feature_flag.rb @@ -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**. diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 000000000..5a3257d95 --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -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 diff --git a/spec/services/feature_flag_spec.rb b/spec/services/feature_flag_spec.rb index 2e207a2f8..e1330954b 100644 --- a/spec/services/feature_flag_spec.rb +++ b/spec/services/feature_flag_spec.rb @@ -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