Class: Goal

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/goal.rb

Constant Summary collapse

@@cache =
nil

Class Method Summary collapse

Class Method Details

.clear_cacheObject



53
54
55
# File 'app/models/goal.rb', line 53

def self.clear_cache
  @@cache = {}
end

.ensure_cache(sid) ⇒ Object



79
80
81
# File 'app/models/goal.rb', line 79

def Goal.ensure_cache(sid)
  refresh_cache(sid) if @@cache==nil
end

.goalsObject



66
67
68
# File 'app/models/goal.rb', line 66

def Goal.goals
  @@cache
end

.has_goals?(sid) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'app/models/goal.rb', line 74

def Goal.has_goals?(sid)
  ensure_cache(sid)
  @@cache[sid].size > 0 rescue false
end

.load(sid, id) ⇒ Object



70
71
72
# File 'app/models/goal.rb', line 70

def Goal.load(sid, id)
  @@cache[sid][id]
end

.record_request(sid, url, cookies, user, started, session) ⇒ Object



10
11
12
13
14
15
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
49
# File 'app/models/goal.rb', line 10

def Goal.record_request(sid, url, cookies, user, started, session)
  begin
  Goal.ensure_cache(sid)

  if started.is_a?(String)
    started = Time.parse(started)
  end
  @@cache[sid].each do |index, goal|
    scored = false
    if goal.goal_type=='URL'
      if goal.match_type=='Exact Match'
       scored = true if url == goal.match_value
      elsif goal.match_type=='Starts'
       scored = true if url.starts_with?(goal.match_value)
      end 
    elsif goal.goal_type=='Session Time'
      next if session["hitgoal_#{goal.id}"] # already hit
      scored = true if started + goal.session_minutes.to_i.minutes < Time.now
      session["hitgoal_#{goal.id}"] = true
    end

    if scored # i.e. goal has been met
      recorded = false # we record if they're in an experiment, but also if they're not.  if they are the goal hit will be recorded in the experiment checking loop, so don't do it again.  if not, do do it
      goal.experiments.each do |experiment|
        option = cookies["experiment_#{experiment.id}".to_sym].to_i rescue 0
        if option>0
          goal.goals_users << GoalsUser.new(:user=>user, :experiment_option=>option, :kit_session_id=>session[:session_id], :system_id=>sid)
          experiment.update_attributes("goals#{option}".to_sym=>experiment.send("goals#{option}") + 1)
          recorded = true
        end
      end
        
      goal.goals_users << GoalsUser.new(:user=>user, :kit_session_id=>session[:session_id]) unless recorded
    end
  end
  rescue Exception => e
    logger.debug(e.message)
    logger.debug(e.backtrace.join('\r\n'))
  end
end

.refresh_cache(sid) ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/models/goal.rb', line 57

def Goal.refresh_cache(sid)
  Goal.clear_cache

  Goal.all.each do |goal|
    @@cache[sid] ||= {}
    @@cache[sid][goal.id] = goal
  end
end