summaryrefslogtreecommitdiff
path: root/app/models/experiment.rb
blob: 2564038553623de8bc98070f48d15dd02bfac51b (plain)
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
# frozen_string_literal: true

class Experiment < ApplicationRecord
  has_many :experiment_users
  has_many :users, through: :experiment_users
  has_many :control_group_users, -> { merge(ExperimentUser.control) }, through: :experiment_users, source: :user
  has_many :experimental_group_users, -> { merge(ExperimentUser.experimental) }, through: :experiment_users, source: :user

  validates :name, presence: true, uniqueness: true, length: { maximum: 255 }

  def self.add_user(name, group_type, user)
    experiment = find_or_create_by(name: name)

    return unless experiment
    return if experiment.experiment_users.where(user: user).exists?

    group_type == ::Gitlab::Experimentation::GROUP_CONTROL ? experiment.add_control_user(user) : experiment.add_experimental_user(user)
  end

  def add_control_user(user)
    control_group_users << user
  end

  def add_experimental_user(user)
    experimental_group_users << user
  end
end