Module: ArRollout

Defined in:
lib/ar_rollout.rb,
lib/ar_rollout/helper.rb,
lib/ar_rollout/version.rb

Defined Under Namespace

Modules: Controller

Constant Summary collapse

VERSION =
"0.0.23"
@@defined_groups =
[]
@@scanned_features =
nil

Class Method Summary collapse

Class Method Details

.activate_group(feature, group) ⇒ Object


32
33
34
35
36
37
38
# File 'lib/ar_rollout.rb', line 32

def self.activate_group(feature, group)
  unless defined_groups.include?(group) || Group.find_by_name(group)
    Group.create!(name: group)
  end

  Rollout.find_or_create_by_name_and_group!(feature, group)
end

.activate_percentage(feature, percentage) ⇒ Object


44
45
46
47
# File 'lib/ar_rollout.rb', line 44

def self.activate_percentage(feature, percentage)
  Rollout.where(name: feature).where('"percentage" IS NOT NULL').each(&:destroy)
  Rollout.create!(name: feature, percentage: percentage)
end

.activate_user(feature, user) ⇒ Object


15
16
17
18
# File 'lib/ar_rollout.rb', line 15

def self.activate_user(feature, user)
  permit_user(feature, user)
  Rollout.find_or_create_by_name_and_user_id!(feature, get_id(user))
end

.active?(name, user) ⇒ Boolean

Returns:

  • (Boolean)

116
117
118
119
120
121
122
123
# File 'lib/ar_rollout.rb', line 116

def self.active?(name, user)
  return false unless user
  unless OptOut.where(feature: name, user_id: get_id(user)).any?
    Rollout.where(name: name).where('"user_id" = ? OR user_id IS NULL', get_id(user)).any? do |rollout|
      rollout.match?(user)
    end
  end
end

.active_groupsObject


70
71
72
# File 'lib/ar_rollout.rb', line 70

def self.active_groups
  Rollout.where('"group" IS NOT NULL').collect(&:group).uniq.sort
end

.add_user_to_group(group, user) ⇒ Object


89
90
91
# File 'lib/ar_rollout.rb', line 89

def self.add_user_to_group(group, user)
  Membership.find_or_create_by_group_id_and_user_id!(get_group(group).id, get_id(user))
end

.all_active(user) ⇒ Object


125
126
127
128
# File 'lib/ar_rollout.rb', line 125

def self.all_active(user)
  return false unless user
  Rollout.matching(user).collect(&:name).sort
end

.change_group_name(old_name, new_name) ⇒ Object


82
83
84
85
86
87
# File 'lib/ar_rollout.rb', line 82

def self.change_group_name(old_name, new_name)
  if group = Group.find_by_name(old_name)
    group.update_attributes!(name: new_name)
    Rollout.find_all_by_group(old_name).each { |rollout| rollout.update_attributes!(group: new_name) }
  end
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (ArRollout)

    the object that the method was called on


11
12
13
# File 'lib/ar_rollout.rb', line 11

def self.configure
  yield self
end

.create_group(group) ⇒ Object


78
79
80
# File 'lib/ar_rollout.rb', line 78

def self.create_group(group)
  get_group(group)
end

.data_groupsObject


58
59
60
# File 'lib/ar_rollout.rb', line 58

def self.data_groups
  Group.all
end

.deactivate(feature) ⇒ Object


53
54
55
56
# File 'lib/ar_rollout.rb', line 53

def self.deactivate(feature)
  Rollout.where(name: feature).destroy_all
  OptOut.where(feature: feature).destroy_all
end

.deactivate_group(feature, group) ⇒ Object


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

def self.deactivate_group(feature, group)
  Rollout.find_all_by_name_and_group(feature, group).each(&:destroy)
end

.deactivate_percentage(feature) ⇒ Object


49
50
51
# File 'lib/ar_rollout.rb', line 49

def self.deactivate_percentage(feature)
  Rollout.where(name: feature).where('"percentage" IS NOT NULL').each(&:destroy)
end

.deactivate_user(feature, user) ⇒ Object


20
21
22
# File 'lib/ar_rollout.rb', line 20

def self.deactivate_user(feature, user)
  Rollout.find_all_by_name_and_user_id(feature, get_id(user)).each(&:destroy)
end

.define_group(name, &block) ⇒ Object


101
102
103
104
105
106
107
# File 'lib/ar_rollout.rb', line 101

def self.define_group(name, &block)
  @@defined_groups << name

  Rollout.send :define_method, "match_#{name}?" do |b|
    block.call(b)
  end
end

.defined_groupsObject


62
63
64
# File 'lib/ar_rollout.rb', line 62

def self.defined_groups
  @@defined_groups
end

.delete_group(group) ⇒ Object


97
98
99
# File 'lib/ar_rollout.rb', line 97

def self.delete_group(group)
  Group.find_by_name(group).try(:destroy)
end

.featuresObject


109
110
111
112
113
114
# File 'lib/ar_rollout.rb', line 109

def self.features
  scanned_feature_names = scanned_features.collect { |scanned_feature| scanned_feature[0] }
  Rollout.select('distinct("name")').where('"name" not in (?)', scanned_feature_names).inject(scanned_feature_names) do |arr, rollout|
    arr << rollout.name
  end.sort
end

.get_group(group) ⇒ Object


74
75
76
# File 'lib/ar_rollout.rb', line 74

def self.get_group(group)
  Group.find_or_create_by_name!(group) unless defined_groups.include?(group.intern)
end

.groupsObject


66
67
68
# File 'lib/ar_rollout.rb', line 66

def self.groups
  (defined_groups + data_groups.collect(&:name).collect(&:intern)).uniq.sort
end

.omit_user(feature, user) ⇒ Object


24
25
26
# File 'lib/ar_rollout.rb', line 24

def self.omit_user(feature, user)
  OptOut.find_or_create_by_feature_and_user_id!(feature, get_id(user))
end

.permit_user(feature, user) ⇒ Object


28
29
30
# File 'lib/ar_rollout.rb', line 28

def self.permit_user(feature, user)
  OptOut.find_by_feature_and_user_id(feature, get_id(user)).try(:destroy)
end

.remove_user_from_group(group, user) ⇒ Object


93
94
95
# File 'lib/ar_rollout.rb', line 93

def self.remove_user_from_group(group, user)
  Membership.find_by_group_id_and_user_id(get_group(group).id, get_id(user)).try(&:destroy)
end