Add flipflop config

This commit is contained in:
Paul Chavard 2018-04-18 12:16:25 +02:00
parent 6b9003c3f4
commit 3f3358db16
4 changed files with 79 additions and 6 deletions

View file

@ -0,0 +1,44 @@
module Flipflop::Strategies
class UserPreferenceStrategy < AbstractStrategy
def self.default_description
"Allows configuration of features per user."
end
def switchable?
false
end
def enabled?(feature)
# Can only check features if we have the user's session.
if request?
legacy_enabled?(feature) || find_current_administrateur&.feature_enabled?(feature)
end
end
private
def legacy_enabled?(feature)
if self.class.legacy_features_map.present?
ids = self.class.legacy_features_map["#{feature}_allowed_for_admin_ids"]
ids.present? && find_current_administrateur&.id&.in?(ids)
end
end
LEGACY_CONFIG_FILE = Rails.root.join("config", "initializers", "features.yml")
def self.legacy_features_map
@@legacy_features_map = begin
if File.exist?(LEGACY_CONFIG_FILE)
YAML.load_file(LEGACY_CONFIG_FILE)
end
end
end
def find_current_administrateur
if request.session["warden.user.user.key"]
administrateur_id = request.session["warden.user.user.key"][0][0]
Administrateur.find_by(id: administrateur_id)
end
end
end
end

View file

@ -74,8 +74,20 @@ class Administrateur < ApplicationRecord
end end
def feature_enabled?(feature) def feature_enabled?(feature)
ids = Features.send(:"#{feature}_for_admin_ids") Flipflop.feature_set.feature(feature)
ids.present? ? id.in?(ids) : false features[feature.to_s]
end
def disable_feature(feature)
Flipflop.feature_set.feature(feature)
features.delete(feature.to_s)
save
end
def enable_feature(feature)
Flipflop.feature_set.feature(feature)
features[feature.to_s] = true
save
end end
private private

18
config/features.rb Normal file
View file

@ -0,0 +1,18 @@
Flipflop.configure do
strategy :cookie
strategy :active_record
strategy :user_preference
strategy :default
group :champs do
feature :champ_pj
feature :champ_siret
end
feature :web_hook
group :production do
feature :remote_storage,
default: Rails.env.production? || Rails.env.staging?
feature :weekly_overview,
default: Rails.env.production? || Rails.env.staging?
end
end

View file

@ -71,11 +71,10 @@ describe Administrateur, type: :model do
let(:administrateur) { create(:administrateur) } let(:administrateur) { create(:administrateur) }
before do before do
allow(Features).to receive(:champ_pj_allowed_for_admin_ids) administrateur.enable_feature(:champ_pj)
.and_return([administrateur.id])
end end
it { expect(administrateur.feature_enabled?(:yolo)).to be_falsey } it { expect(administrateur.feature_enabled?(:champ_siret)).to be_falsey }
it { expect(administrateur.feature_enabled?(:champ_pj_allowed)).to be_truthy } it { expect(administrateur.feature_enabled?(:champ_pj)).to be_truthy }
end end
end end