-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfeature_service.rb
More file actions
43 lines (32 loc) · 1.15 KB
/
feature_service.rb
File metadata and controls
43 lines (32 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class FeatureService
class UserRequiredError < StandardError; end
class GroupRequiredError < StandardError; end
attr_reader :group
class << self
def enabled?(...)
FeatureService.new.enabled?(...)
end
end
def initialize(user: nil, group: nil)
@user = user
@group = group
end
def enabled?(feature_name)
return false if Settings.features.blank?
segments = feature_name.to_s.split(".")
feature = Settings.features.dig(*segments)
return feature unless feature.is_a? Config::Options
if feature.organisations.present?
raise UserRequiredError, "Feature #{feature_name} requires user to be provided" if @user.blank?
if @user.organisation.present?
organisation_key = @user.organisation.slug.underscore.to_sym
return feature.organisations[organisation_key] if feature.organisations.key?(organisation_key)
end
end
if feature.enabled_by_group.present? && feature.enabled_by_group
raise GroupRequiredError, "Feature #{feature_name} requires group to be provided" if group.blank?
return group.send(:"#{feature_name}_enabled?")
end
feature.enabled
end
end