From 322f93afbeeb300097ca5c279b0d9c67fd871f24 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 27 Mar 2018 15:43:59 +0200 Subject: [PATCH] Add administrateur#feature_enabled? method --- app/helpers/type_de_champ_helper.rb | 2 +- app/models/administrateur.rb | 5 +++++ spec/models/administrateur_spec.rb | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/helpers/type_de_champ_helper.rb b/app/helpers/type_de_champ_helper.rb index bacc16e7a..f91158bf6 100644 --- a/app/helpers/type_de_champ_helper.rb +++ b/app/helpers/type_de_champ_helper.rb @@ -2,7 +2,7 @@ module TypeDeChampHelper def tdc_options(current_administrateur) tdcs = TypeDeChamp.type_de_champs_list_fr - if !current_administrateur.id.in?(Features.champ_pj_allowed_for_admin_ids) + if !current_administrateur.feature_enabled?(:champ_pj_allowed) tdcs.reject! { |tdc| tdc.last == "piece_justificative" } end diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 790f1062e..2b1d2f220 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -73,6 +73,11 @@ class Administrateur < ApplicationRecord administrateur end + def feature_enabled?(feature) + ids = Features.send(:"#{feature}_for_admin_ids") + ids.present? ? id.in?(ids) : false + end + private def generate_api_token diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index 5b5b1160a..2cd5cfcb9 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -66,4 +66,16 @@ describe Administrateur, type: :model do it { expect(Administrateur.reset_password('123', '12345678').errors).not_to be_empty } it { expect(Administrateur.reset_password(reset_password_token, '').errors).not_to be_empty } end + + describe '#feature_enabled?' do + let(:administrateur) { create(:administrateur) } + + before do + allow(Features).to receive(:champ_pj_allowed_for_admin_ids) + .and_return([administrateur.id]) + end + + it { expect(administrateur.feature_enabled?(:yolo)).to be_falsey } + it { expect(administrateur.feature_enabled?(:champ_pj_allowed)).to be_truthy } + end end