From 72f351e06e9be1714035a18836cead62df840bfb Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Wed, 30 Aug 2023 15:28:39 -0400 Subject: [PATCH 1/2] Add #enabled? to ActiveRecord models --- lib/flipper/model/active_record.rb | 19 +++++++++++++++ spec/flipper/model/active_record_spec.rb | 30 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/flipper/model/active_record.rb b/lib/flipper/model/active_record.rb index b334ec11f..4cae1e00b 100644 --- a/lib/flipper/model/active_record.rb +++ b/lib/flipper/model/active_record.rb @@ -7,6 +7,25 @@ module ActiveRecord def flipper_properties {"type" => self.class.name}.merge(attributes) end + + # Check if a feature is enabled for this record or any associated actors + # returned by `#flipper_actors` + def enabled?(feature_name) + Flipper.enabled?(feature_name, *flipper_actors) + end + + # Returns the set of actors associated with this record. Override to + # return any other records that should be considered actors. + # + # class User < ApplicationRecord + # # … + # def flipper_actors + # [self, company] + # end + # end + def flipper_actors + [self] + end end end end diff --git a/spec/flipper/model/active_record_spec.rb b/spec/flipper/model/active_record_spec.rb index 00433acad..d0abf94bc 100644 --- a/spec/flipper/model/active_record_spec.rb +++ b/spec/flipper/model/active_record_spec.rb @@ -45,4 +45,34 @@ class User < ActiveRecord::Base }) end end + + describe "enabled?" do + subject { User.create!(name: "Test") } + + module Friendable + attr_accessor :friends + + def flipper_actors + [self] + Array(friends) + end + end + + it "returns false if feature is disabled" do + expect(subject.enabled?(:stats)).to be(false) + end + + it "returns true if feature is enabled for actor" do + Flipper.enable :stats, subject + expect(subject.enabled?(:stats)).to be(true) + end + + it "returns true if feature is enabled for associated actor" do + friend = User.create!(name: "Friend") + subject.extend Friendable + subject.friends = [friend] + + Flipper.enable :stats, friend + expect(subject.enabled?(:stats)).to be(true) + end + end end From 2dfd29b6074e1a009f47c609148832ec08ae7d73 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Wed, 30 Aug 2023 16:14:01 -0400 Subject: [PATCH 2/2] Use #flipper_enabled? to avoid conflicts --- lib/flipper/model/active_record.rb | 2 +- spec/flipper/model/active_record_spec.rb | 33 ++++++++++++------------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/flipper/model/active_record.rb b/lib/flipper/model/active_record.rb index 4cae1e00b..7de2b3052 100644 --- a/lib/flipper/model/active_record.rb +++ b/lib/flipper/model/active_record.rb @@ -10,7 +10,7 @@ def flipper_properties # Check if a feature is enabled for this record or any associated actors # returned by `#flipper_actors` - def enabled?(feature_name) + def flipper_enabled?(feature_name) Flipper.enabled?(feature_name, *flipper_actors) end diff --git a/spec/flipper/model/active_record_spec.rb b/spec/flipper/model/active_record_spec.rb index d0abf94bc..2c6ab6300 100644 --- a/spec/flipper/model/active_record_spec.rb +++ b/spec/flipper/model/active_record_spec.rb @@ -46,33 +46,32 @@ class User < ActiveRecord::Base end end - describe "enabled?" do + describe "flipper_enabled?" do subject { User.create!(name: "Test") } - module Friendable - attr_accessor :friends - - def flipper_actors - [self] + Array(friends) - end - end - - it "returns false if feature is disabled" do - expect(subject.enabled?(:stats)).to be(false) - end - - it "returns true if feature is enabled for actor" do + it "delegates to Flipper.enabled?" do + expect(subject.flipper_enabled?(:stats)).to be(false) Flipper.enable :stats, subject - expect(subject.enabled?(:stats)).to be(true) + expect(subject.flipper_enabled?(:stats)).to be(true) end it "returns true if feature is enabled for associated actor" do friend = User.create!(name: "Friend") - subject.extend Friendable + + # Add a flipper_actors method to this instanc + subject.extend Module.new { + attr_accessor :friends + + def flipper_actors + [self] + Array(friends) + end + } + subject.friends = [friend] + expect(subject.flipper_enabled?(:stats)).to be(false) Flipper.enable :stats, friend - expect(subject.enabled?(:stats)).to be(true) + expect(subject.flipper_enabled?(:stats)).to be(true) end end end