diff options
Diffstat (limited to 'app/models/experiment.rb')
-rw-r--r-- | app/models/experiment.rb | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/app/models/experiment.rb b/app/models/experiment.rb index f179a1fc6ce..a4cacab25ee 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -2,17 +2,24 @@ class Experiment < ApplicationRecord has_many :experiment_users + has_many :experiment_subjects, inverse_of: :experiment 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) + def self.add_user(name, group_type, user, context = {}) + find_or_create_by!(name: name).record_user_and_group(user, group_type, context) + end - experiment.record_user_and_group(user, group_type) + def self.record_conversion_event(name, user) + find_or_create_by!(name: name).record_conversion_event_for_user(user) 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) + def record_user_and_group(user, group_type, context = {}) + experiment_users.find_or_initialize_by(user: user).update!(group_type: group_type, context: context) + end + + def record_conversion_event_for_user(user) + experiment_users.find_by(user: user, converted_at: nil)&.touch(:converted_at) end end |