cartodb/spec/helpers/feature_flag_helper.rb

28 lines
778 B
Ruby
Raw Normal View History

2020-06-15 10:58:47 +08:00
module FeatureFlagHelper
def set_feature_flag(user, feature, state)
user.reload
previous_state = user.has_feature_flag?(feature)
if state != previous_state
ff = FeatureFlag[name: feature]
ffu = FeatureFlagsUser[feature_flag_id: ff.id, user_id: user.id]
if state
unless ffu
FeatureFlagsUser.new(feature_flag_id: ff.id, user_id: user.id).save
end
else
ff.update restricted: false unless ff.restricted
ffu.delete if ffu
end
user.reload
end
previous_state
end
def with_feature_flag(user, feature, state)
user.reload
previous_state = set_feature_flag user, feature, state
yield
set_feature_flag user, feature, previous_state if state != previous_state
end
end