debian-mirror-gitlab/app/models/experiment_subject.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
858 B
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
class ExperimentSubject < ApplicationRecord
include ::Gitlab::Experimentation::GroupTypes
belongs_to :experiment, inverse_of: :experiment_subjects
belongs_to :user
2021-09-04 01:27:46 +05:30
belongs_to :namespace
2021-02-22 17:27:13 +05:30
belongs_to :project
validates :experiment, presence: true
validates :variant, presence: true
validate :must_have_one_subject_present
enum variant: { GROUP_CONTROL => 0, GROUP_EXPERIMENTAL => 1 }
2021-09-04 01:27:46 +05:30
def self.valid_subject?(subject)
subject.is_a?(Namespace) || subject.is_a?(User) || subject.is_a?(Project)
end
2021-02-22 17:27:13 +05:30
private
def must_have_one_subject_present
if non_nil_subjects.length != 1
2021-09-04 01:27:46 +05:30
errors.add(:base, s_("ExperimentSubject|Must have exactly one of User, Namespace, or Project."))
2021-02-22 17:27:13 +05:30
end
end
def non_nil_subjects
2021-09-04 01:27:46 +05:30
@non_nil_subjects ||= [user, namespace, project].reject(&:blank?)
2021-02-22 17:27:13 +05:30
end
end