summaryrefslogtreecommitdiff
path: root/app/models/experiment.rb
blob: f179a1fc6ce4eb5e6b173f3da38694874a3669e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# frozen_string_literal: true

class Experiment < ApplicationRecord
  has_many :experiment_users

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

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

    experiment.record_user_and_group(user, group_type)
  end

  # Create or update the recorded experiment_user row for the user in this experiment.
  def record_user_and_group(user, group_type)
    experiment_users.find_or_initialize_by(user: user).update!(group_type: group_type)
  end
end