Class: Ahoy::DatabaseStore

Inherits:
BaseStore show all
Defined in:
lib/ahoy/database_store.rb

Instance Attribute Summary

Attributes inherited from BaseStore

#user

Instance Method Summary collapse

Methods inherited from BaseStore

#exclude?, #generate_id, #initialize

Constructor Details

This class inherits a constructor from Ahoy::BaseStore

Instance Method Details

#authenticate(_) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/ahoy/database_store.rb', line 43

def authenticate(_)
  if visit && visit.respond_to?(:user) && !visit.user
    begin
      visit.user = user
      visit.save!
    rescue ActiveRecord::AssociationTypeMismatch
      # do nothing
    end
  end
end

#geocode(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ahoy/database_store.rb', line 30

def geocode(data)
  visit_token = data.delete(:visit_token)
  data = slice_data(visit_model, data)
  if defined?(Mongoid::Document) && visit_model < Mongoid::Document
    # upsert since visit might not be found due to eventual consistency
    visit_model.where(visit_token: visit_token).find_one_and_update({"$set": data}, {upsert: true})
  elsif visit
    visit.update!(data)
  else
    Ahoy.log "Visit for geocode not found: #{visit_token}"
  end
end

#track_event(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ahoy/database_store.rb', line 14

def track_event(data)
  visit = visit_or_create(started_at: data[:time])
  if visit
    event = event_model.new(slice_data(event_model, data))
    event.visit = visit
    event.time = visit.started_at if event.time < visit.started_at
    begin
      event.save!
    rescue => e
      raise e unless unique_exception?(e)
    end
  else
    Ahoy.log "Event excluded since visit not created: #{data[:visit_token]}"
  end
end

#track_visit(data) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/ahoy/database_store.rb', line 3

def track_visit(data)
  @visit = visit_model.create!(slice_data(visit_model, data))
rescue => e
  raise e unless unique_exception?(e)

  # so next call to visit will try to fetch from DB
  if defined?(@visit)
    remove_instance_variable(:@visit)
  end
end

#visitObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ahoy/database_store.rb', line 54

def visit
  unless defined?(@visit)
    if ahoy.send(:existing_visit_token) || ahoy.instance_variable_get(:@visit_token)
      # find_by raises error by default with Mongoid when not found
      @visit = visit_model.where(visit_token: ahoy.visit_token).take if ahoy.visit_token
    elsif !Ahoy.cookies? && ahoy.visitor_token
      @visit = visit_model.where(visitor_token: ahoy.visitor_token).where(started_at: Ahoy.visit_duration.ago..).order(started_at: :desc).first
    else
      @visit = nil
    end
  end
  @visit
end

#visit_or_create(started_at: nil) ⇒ Object

if we don’t have a visit, let’s try to create one first



69
70
71
72
# File 'lib/ahoy/database_store.rb', line 69

def visit_or_create(started_at: nil)
  ahoy.track_visit(started_at: started_at) if !visit && Ahoy.server_side_visits
  visit
end