Class: Vanity::Adapters::ActiveRecordAdapter::VanityParticipant

Inherits:
VanityRecord
  • Object
show all
Defined in:
lib/vanity/adapters/active_record_adapter.rb

Overview

Participant model

Class Method Summary collapse

Methods inherited from VanityRecord

needs_attr_accessible?, rails_agnostic_find_or_create_by

Class Method Details

.retrieve(experiment, identity, create = true, update_with = nil) ⇒ Object

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.


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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