From 81461684f353fd57189cdb219c21806e75f7add7 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 12 Oct 2022 17:16:22 +1100 Subject: [PATCH] Generalise feature toggle constraint Now we can re-use it for any feature. --- app/constraints/feature_toggle_constraint.rb | 15 +++++++++++++++ app/constraints/split_checkout_constraint.rb | 11 ----------- config/routes.rb | 2 +- spec/routing/stripe_spec.rb | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 app/constraints/feature_toggle_constraint.rb delete mode 100644 app/constraints/split_checkout_constraint.rb diff --git a/app/constraints/feature_toggle_constraint.rb b/app/constraints/feature_toggle_constraint.rb new file mode 100644 index 0000000000..cd7070eed8 --- /dev/null +++ b/app/constraints/feature_toggle_constraint.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class FeatureToggleConstraint + def initialize(feature_name) + @feature = feature_name + end + + def matches?(request) + OpenFoodNetwork::FeatureToggle.enabled?(@feature, current_user(request)) + end + + def current_user(request) + request.env['warden'].user + end +end diff --git a/app/constraints/split_checkout_constraint.rb b/app/constraints/split_checkout_constraint.rb deleted file mode 100644 index ee6c810d88..0000000000 --- a/app/constraints/split_checkout_constraint.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class SplitCheckoutConstraint - def matches?(request) - OpenFoodNetwork::FeatureToggle.enabled? :split_checkout, current_user(request) - end - - def current_user(request) - request.env['warden'].user - end -end diff --git a/config/routes.rb b/config/routes.rb index bf8ac6aa01..cf7e544f5a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,7 +84,7 @@ Openfoodnetwork::Application.routes.draw do get "/stripe/authorize/:order_number", to: "stripe#authorize", as: :authorize_stripe end - constraints SplitCheckoutConstraint.new do + constraints FeatureToggleConstraint.new(:split_checkout) do get '/checkout', to: 'split_checkout#edit' constraints step: /(details|payment|summary)/ do diff --git a/spec/routing/stripe_spec.rb b/spec/routing/stripe_spec.rb index 50121dd671..54dd76bde2 100644 --- a/spec/routing/stripe_spec.rb +++ b/spec/routing/stripe_spec.rb @@ -4,7 +4,7 @@ require "spec_helper" describe "routing for Stripe return URLS", type: :routing do before do - allow_any_instance_of(SplitCheckoutConstraint).to receive(:current_user) { build(:user) } + allow_any_instance_of(FeatureToggleConstraint).to receive(:current_user) { build(:user) } end context "checkout return URLs" do