class Vanity::Adapters::ActiveRecordAdapter::VanityParticipant

Participant model

Public Class Methods

retrieve(experiment, identity, create = true, update_with = nil) click to toggle source

Finds the participant by experiment and identity. If create is true then it will create the participant if not found. If a hash is passed then this will be passed to create if creating, or will be used to update the found participant.

# File lib/vanity/adapters/active_record_adapter.rb, line 104
def self.retrieve(experiment, identity, create = true, update_with = nil)
  retried = false
  begin
    if record = VanityParticipant.where(:experiment_id => experiment.to_s, :identity => identity.to_s).first
      record.update_attributes(update_with) if update_with
    elsif create
      record = VanityParticipant.create({ :experiment_id => experiment.to_s, :identity => identity.to_s }.merge(update_with || {}))
    end
    record
  rescue ActiveRecord::RecordNotUnique => e
    if retried
      raise e
    else
      retried = true
      retry
    end
  end
end