Class: Rollout

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ar_rollout/rollout.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.matching(user) ⇒ Object


6
7
8
9
10
11
# File 'lib/ar_rollout/rollout.rb', line 6

def self.matching(user)
  where('"rollouts".user_id = ? OR "rollouts".user_id IS NULL', user.id).
    where('"rollouts".name NOT IN (SELECT feature FROM "opt_outs" WHERE "opt_outs".user_id = ?)', user.id).select { |rollout|
      rollout.match? user
    }.uniq_by(&:name)
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/ar_rollout/rollout.rb', line 18

def enabled?
  failure_count.to_i < 1
end

#match?(user) ⇒ Boolean

Returns:

  • (Boolean)

13
14
15
16
# File 'lib/ar_rollout/rollout.rb', line 13

def match?(user)
  return false unless user
  enabled? && (match_user?(user) || match_group?(user) || match_percentage?(user))
end

#match_group?(user) ⇒ Boolean

Returns:

  • (Boolean)

22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ar_rollout/rollout.rb', line 22

def match_group?(user)
  if Rollout.method_defined? "match_#{group}?"
    send "match_#{group}?", user
  else
    data_group = Group.find_by_name(group)
    if data_group && data_group.memberships.where('user_id = ?', user.id).any?
      true
    else
      false
    end
  end
end

#match_percentage?(user) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/ar_rollout/rollout.rb', line 39

def match_percentage?(user)
  percentage ? ((user.id.to_i % 100) < percentage.to_i) : false
end

#match_user?(user) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/ar_rollout/rollout.rb', line 35

def match_user?(user)
  user_id ? user_id.to_s == user.id.to_s : false
end