Files
openfoodnetwork/app/constraints/feature_toggle_constraint.rb
Maikel Linke 36c44a5487 Allow FeatureToggleConstraint to run w/o warden
Routing specs and controller specs don't set warden on the request
environment. While we could try to re-implement Warden/Devise logic
here, it's much easier to not rely on the environment to be set.

While I'm usually opposed to changing production code to make testing
easier, it does avoid a lot of complication in this case. And maybe it
would be handy in other circumstances to have a more defensive approach
to checking the logged in user.
2022-10-27 15:07:04 +11:00

18 lines
349 B
Ruby

# frozen_string_literal: true
require "open_food_network/feature_toggle"
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