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

28 lines
1 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
class Experiment < ApplicationRecord
has_many :experiment_users
2021-02-22 17:27:13 +05:30
has_many :experiment_subjects, inverse_of: :experiment
2020-10-24 23:57:45 +05:30
validates :name, presence: true, uniqueness: true, length: { maximum: 255 }
2021-02-22 17:27:13 +05:30
def self.add_user(name, group_type, user, context = {})
find_or_create_by!(name: name).record_user_and_group(user, group_type, context)
end
2020-10-24 23:57:45 +05:30
2021-02-22 17:27:13 +05:30
def self.record_conversion_event(name, user)
find_or_create_by!(name: name).record_conversion_event_for_user(user)
2020-10-24 23:57:45 +05:30
end
2021-01-29 00:20:46 +05:30
# Create or update the recorded experiment_user row for the user in this experiment.
2021-02-22 17:27:13 +05:30
def record_user_and_group(user, group_type, context = {})
2021-03-08 18:12:59 +05:30
experiment_user = experiment_users.find_or_initialize_by(user: user)
merged_context = experiment_user.context.deep_merge(context.deep_stringify_keys)
experiment_user.update!(group_type: group_type, context: merged_context)
2021-02-22 17:27:13 +05:30
end
def record_conversion_event_for_user(user)
experiment_users.find_by(user: user, converted_at: nil)&.touch(:converted_at)
2020-10-24 23:57:45 +05:30
end
end