From aa91f1225c87d0ab6b7e1bac97ba0288bd9d82fd Mon Sep 17 00:00:00 2001 From: Steve Jackson Date: Thu, 30 Jan 2025 15:46:17 -0600 Subject: [PATCH] Extract method for feature flag check While working on a controller we wanted a fallback behavior where we could check another related association for a test track assignment if the visitor did not have direct access. With this in place a controller can override the check when needed without completely replacing the before_action behavior ``` def feature_flagged?(feature_flag) super || current_user.organization.test_track_ab(feature_flag, context: self.class.name.underscore) end ``` --- app/controllers/concerns/test_track/controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/concerns/test_track/controller.rb b/app/controllers/concerns/test_track/controller.rb index 8fd337ee..3a249518 100644 --- a/app/controllers/concerns/test_track/controller.rb +++ b/app/controllers/concerns/test_track/controller.rb @@ -12,13 +12,17 @@ module TestTrack::Controller class_methods do def require_feature_flag(feature_flag, *args) before_action(*args) do - raise ActionController::RoutingError, 'Not Found' unless test_track_visitor.ab(feature_flag, context: self.class.name.underscore) + raise ActionController::RoutingError, 'Not Found' unless feature_flagged?(feature_flag) end end end private + def feature_flagged?(feature_flag) + test_track_visitor.ab(feature_flag, context: self.class.name.underscore) + end + def test_track_session @test_track_session ||= TestTrack::WebSession.new(self) end