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



91
92
93
94
# File 'lib/ahoy/stores/active_record_token_store.rb', line 91

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

.uses_deprecated_subscribers?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/ahoy/stores/active_record_token_store.rb', line 96

def uses_deprecated_subscribers?
  @uses_deprecated_subscribers || false
end

Instance Method Details

#exclude?Boolean

Returns:

  • (Boolean)


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

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



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
50
51
52
53
54
55
# File 'lib/ahoy/stores/active_record_token_store.rb', line 25

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
        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
# 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.created_at = options[:started_at]
    end

  set_visit_properties(visit)

  yield(visit) if block_given?

  begin
    visit.save!
    geocode(visit)
  rescue *unique_exception_classes
    # do nothing
  end
end

#userObject



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

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



57
58
59
# File 'lib/ahoy/stores/active_record_token_store.rb', line 57

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