Module: HasHelpers::Feature

Defined in:
app/lib/has_helpers/feature.rb

Class Method Summary collapse

Class Method Details

.activate!(feature, user_or_org = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'app/lib/has_helpers/feature.rb', line 49

def activate!(feature, user_or_org = nil)
  if user_or_org.nil?
    flipper[feature].enable
  else
    flipper[feature].enable(user_or_org)
  end
end

.active?(feature, user_or_org = nil) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
# File 'app/lib/has_helpers/feature.rb', line 22

def active?(feature, user_or_org = nil)
  case
  when user_or_org.nil?
    flipper[feature].enabled?
  when user_or_org.respond_to?(:organization)
    flipper[feature].enabled?(user_or_org && user_or_org.organization) || flipper[feature].enabled?(user_or_org)
  else
    flipper[feature].enabled?(user_or_org)
  end
end

.active_any?(feature) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'app/lib/has_helpers/feature.rb', line 38

def active_any?(feature)
  # true if the feature is fully enabled or if any actor is enabled
  !flipper[feature].off?
end

.active_orgs(feature) ⇒ Object



43
44
45
46
47
# File 'app/lib/has_helpers/feature.rb', line 43

def active_orgs(feature)
  # get all organizations that are enabled for the feature (this method won't work if the feature is fully enabled)
  # since the actors' format equals "Organization:id", we can just split the string to obtain the organization_id
  flipper[feature].actors_value.map { |org| org.split(":").last.to_i }
end

.deactivate!(feature, user_or_org = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'app/lib/has_helpers/feature.rb', line 61

def deactivate!(feature, user_or_org = nil)
  if user_or_org.nil?
    flipper[feature].disable
  else
    flipper[feature].disable(user_or_org)
  end
end

.flipperObject



73
74
75
# File 'app/lib/has_helpers/feature.rb', line 73

def flipper
  @flipper ||= Flipper.new(adapter)
end

.off?(feature) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'app/lib/has_helpers/feature.rb', line 33

def off?(feature)
  # will only be true of the feature is fully disabled (partially enabled for some actors does not count)
  flipper[feature].off?
end

.register(*args, &block) ⇒ Object



69
70
71
# File 'app/lib/has_helpers/feature.rb', line 69

def register(*args, &block)
  Flipper.register(*args, &block)
end

.setup(feature) ⇒ Object



57
58
59
# File 'app/lib/has_helpers/feature.rb', line 57

def setup(feature)
  activate!(feature) unless flipper.exist?(feature)
end