Class: Ahoy::Stores::ActiveRecordTokenStore

Inherits:
BaseStore
  • Object
show all
Defined in:
lib/ahoy/stores/active_record_token_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseStore

#authenticate, #generate_id, #initialize, #report_exception

Constructor Details

This class inherits a constructor from Ahoy::Stores::BaseStore

Class Method Details

.uses_deprecated_subscribersObject



93
94
95
96
# File 'lib/ahoy/stores/active_record_token_store.rb', line 93

def uses_deprecated_subscribers
  warn "[DEPRECATION] Ahoy subscribers are deprecated"
  @uses_deprecated_subscribers = true
end

.uses_deprecated_subscribers?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ahoy/stores/active_record_token_store.rb', line 98

def uses_deprecated_subscribers?
  @uses_deprecated_subscribers || false
end

Instance Method Details

#exclude?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ahoy/stores/active_record_token_store.rb', line 63

def exclude?
  (!Ahoy.track_bots && bot?) ||
    (
      if Ahoy.exclude_method
        warn "[DEPRECATION] Ahoy.exclude_method is deprecated - use exclude? instead"
        if Ahoy.exclude_method.arity == 1
          Ahoy.exclude_method.call(controller)
        else
          Ahoy.exclude_method.call(controller, request)
        end
      else
        false
      end
    )
end

#track_event(name, properties, options, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ahoy/stores/active_record_token_store.rb', line 27

def track_event(name, properties, options, &block)
  if self.class.uses_deprecated_subscribers?
    options[:controller] ||= controller
    options[:user] ||= user
    options[:visit] ||= visit
    options[:visit_token] ||= ahoy.visit_token
    options[:visitor_token] ||= ahoy.visitor_token

    subscribers = Ahoy.subscribers
    if subscribers.any?
      subscribers.each do |subscriber|
        subscriber.track(name, properties, options.dup)
      end
    else
      $stderr.puts "No subscribers"
    end
  else
    event =
      event_model.new do |e|
        e.visit_id = visit.try(:id)
        e.user = user if e.respond_to?(:user=)
        e.name = name
        e.properties = properties
        e.time = options[:time]
      end

    yield(event) if block_given?

    event.save!
  end
end

#track_visit(options) {|visit| ... } ⇒ Object

Yields:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ahoy/stores/active_record_token_store.rb', line 4

def track_visit(options, &block)
  @visit =
    visit_model.new do |v|
      v.visit_token = ahoy.visit_token
      v.visitor_token = ahoy.visitor_token
      v.user = user if v.respond_to?(:user=)
      v.started_at = options[:started_at] if v.respond_to?(:started_at)
      v.created_at = options[:started_at] if v.respond_to?(:created_at)
    end

  set_visit_properties(visit)

  yield(visit) if block_given?

  begin
    visit.save!
    geocode(visit)
  rescue *unique_exception_classes
    # reset to nil so subsequent calls to track_event will load visit from DB
    @visit = nil
  end
end

#userObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ahoy/stores/active_record_token_store.rb', line 79

def user
  @user ||= begin
    user_method = Ahoy.user_method
    if user_method.respond_to?(:call)
      user_method.call(controller)
    elsif user_method
      controller.send(user_method)
    else
      super
    end
  end
end

#visitObject



59
60
61
# File 'lib/ahoy/stores/active_record_token_store.rb', line 59

def visit
  @visit ||= (visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token)
end