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.19"
- @@defined_groups =
[]
Class Method Summary collapse
- .activate_group(feature, group) ⇒ Object
- .activate_percentage(feature, percentage) ⇒ Object
- .activate_user(feature, user) ⇒ Object
- .activate_user_in_group(group, user) ⇒ Object
- .active?(name, user) ⇒ Boolean
- .active_groups ⇒ Object
- .all_active(user) ⇒ Object
- .configure {|_self| ... } ⇒ Object
- .deactivate_all(feature) ⇒ Object
- .deactivate_group(feature, group) ⇒ Object
- .deactivate_user(feature, user) ⇒ Object
- .define_group(name, &block) ⇒ Object
- .defined_groups ⇒ Object
- .degrade_feature(name) ⇒ Object
- .exclude_user(feature, user) ⇒ Object
- .features ⇒ Object
- .get_group(group) ⇒ Object
- .groups ⇒ Object
- .info(feature) ⇒ Object
Class Method Details
.activate_group(feature, group) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/ar_rollout.rb', line 45 def self.activate_group(feature, group) return false if feature.nil? || group.nil? unless defined_groups.include? group get_group(group) end Rollout.find_or_create_by_name_and_group(feature, group) end |
.activate_percentage(feature, percentage) ⇒ Object
57 58 59 60 |
# File 'lib/ar_rollout.rb', line 57 def self.activate_percentage(feature, percentage) Rollout.where("name = ? and percentage is not null", feature).destroy_all Rollout.create(name: feature, percentage: percentage) end |
.activate_user(feature, user) ⇒ Object
29 30 31 32 33 |
# File 'lib/ar_rollout.rb', line 29 def self.activate_user(feature, user) return false if feature.nil? || user.nil? res_id = [Fixnum, String].include?(user.class) ? user : user.id Rollout.find_or_create_by_name_and_user_id(feature, res_id) end |
.activate_user_in_group(group, user) ⇒ Object
66 67 68 69 |
# File 'lib/ar_rollout.rb', line 66 def self.activate_user_in_group(group, user) res_id = [Fixnum, String].include?(user.class) ? user : user.id Membership.find_or_create_by_group_id_and_user_id(get_group(group).id, res_id) end |
.active?(name, user) ⇒ Boolean
83 84 85 86 87 88 89 90 |
# File 'lib/ar_rollout.rb', line 83 def self.active?(name, user) return false unless user unless OptOut.where(feature: name, user_id: user.id).any? Rollout.where(name: name).where("user_id = ? or user_id is NULL", user.id.to_i).any? do |rollout| rollout.match?(user) end end end |
.active_groups ⇒ Object
79 80 81 |
# File 'lib/ar_rollout.rb', line 79 def self.active_groups Rollout.where("'group' is NOT NULL").map(&:group).uniq end |
.all_active(user) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ar_rollout.rb', line 92 def self.all_active(user) return false unless user rollouts = [] Rollout.where("user_id = ? or user_id is NULL", user.id.to_i).each do |rollout| unless OptOut.where(feature: name, user_id: user.id).any? rollouts << rollout.name if rollout.match?(user) end end rollouts.uniq end |
.configure {|_self| ... } ⇒ Object
9 10 11 |
# File 'lib/ar_rollout.rb', line 9 def self.configure yield self end |
.deactivate_all(feature) ⇒ Object
71 72 73 |
# File 'lib/ar_rollout.rb', line 71 def self.deactivate_all(feature) Rollout.find_all_by_name(feature).map(&:destroy) end |
.deactivate_group(feature, group) ⇒ Object
53 54 55 |
# File 'lib/ar_rollout.rb', line 53 def self.deactivate_group(feature, group) Rollout.find_all_by_name_and_group(feature, group).map(&:destroy) end |
.deactivate_user(feature, user) ⇒ Object
35 36 37 38 |
# File 'lib/ar_rollout.rb', line 35 def self.deactivate_user(feature, user) res_id = [Fixnum, String].include?(user.class) ? user : user.id Rollout.find_all_by_name_and_user_id(feature, res_id).map(&:destroy) end |
.define_group(name, &block) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/ar_rollout.rb', line 21 def self.define_group(name, &block) @@defined_groups << name Rollout.send :define_method, "match_#{name}?" do |b| block.call(b) end end |
.defined_groups ⇒ Object
13 14 15 |
# File 'lib/ar_rollout.rb', line 13 def self.defined_groups @@defined_groups end |
.degrade_feature(name) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/ar_rollout.rb', line 103 def self.degrade_feature(name) yield rescue StandardError => e Rollout.where(name: name).each do |rollout| rollout.increment!(:failure_count) end raise e end |
.exclude_user(feature, user) ⇒ Object
40 41 42 43 |
# File 'lib/ar_rollout.rb', line 40 def self.exclude_user(feature, user) res_id = [Fixnum, String].include?(user.class) ? user : user.id OptOut.create(feature: feature, user_id: res_id) end |
.features ⇒ Object
75 76 77 |
# File 'lib/ar_rollout.rb', line 75 def self.features Rollout.select("distinct(name)").order(:name).map(&:name) end |
.get_group(group) ⇒ Object
62 63 64 |
# File 'lib/ar_rollout.rb', line 62 def self.get_group(group) Group.find_or_create_by_name(group) end |
.groups ⇒ Object
17 18 19 |
# File 'lib/ar_rollout.rb', line 17 def self.groups (@@defined_groups + Group.select(:name).collect(&:name).collect(&:intern)).uniq.sort end |
.info(feature) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/ar_rollout.rb', line 112 def self.info(feature) { :percentage => (_active_percentage(feature) || 0).to_i, :groups => _active_groups(feature).map { |g| g.to_sym }, :users => _active_user_ids(feature) } end |