2020-08-10 17:09:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Experiment < ApplicationRecord
|
2020-10-30 05:08:39 -04:00
|
|
|
include ::Gitlab::Experimentation::GroupTypes
|
|
|
|
|
2020-08-10 17:09:44 -04:00
|
|
|
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?
|
|
|
|
|
2020-10-30 05:08:39 -04:00
|
|
|
group_type == GROUP_CONTROL ? experiment.add_control_user(user) : experiment.add_experimental_user(user)
|
2020-08-10 17:09:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_control_user(user)
|
|
|
|
control_group_users << user
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_experimental_user(user)
|
|
|
|
experimental_group_users << user
|
|
|
|
end
|
|
|
|
end
|