Class: Splut::Experiment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/splut/models/experiment.rb

Instance Method Summary collapse

Instance Method Details

#is_in_group?(splutable_thing) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/splut/models/experiment.rb', line 10

def is_in_group?(splutable_thing)
  existing_segment_participant = SegmentParticipant.find_by(variation_id: self.variations.collect(&:id),
                                                            splutable: splutable_thing)

end

#participate!(splutable_thing) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/splut/models/experiment.rb', line 16

def participate!(splutable_thing)

  if self.variations.none?
    raise "participate! was called on experiment #{self.name} (id #{self.id} but there are no variations)"
  end
  # will return the segment participant if the
  # thing is already in the experiment
  #
  #
  # will put the participant into the experiment if not
  #
  segment_participant = Splut::SegmentParticipant.find_by(
    variation_id: self.variations.collect(&:id),
    splutable: splutable_thing)

  if !segment_participant
    random = rand(self.variations.count).floor

    this_variation = self.variations[random]


    segment_participant = this_variation.segment_participants.create!(splutable: splutable_thing)

  else
    puts " #{splutable_thing} was already in variation #{this_variation}"
    this_variation = segment_participant.variation
  end

  impression = this_variation.impressions.create!(splutable: splutable_thing,
                                                  segment_participant: segment_participant)

  impression
end