Module: Ahoy::Controller

Defined in:
lib/ahoy/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/ahoy/controller.rb', line 5

def self.included(base)
  if base.respond_to?(:helper_method)
    base.helper_method :current_visit
    base.helper_method :ahoy
  end
  base.before_action :set_ahoy_cookies, unless: -> { Ahoy.api_only }
  base.before_action :track_ahoy_visit, unless: -> { Ahoy.api_only }
  base.before_action :set_ahoy_request_store
end

Instance Method Details

#ahoyObject



15
16
17
# File 'lib/ahoy/controller.rb', line 15

def ahoy
  @ahoy ||= Ahoy::Tracker.new(controller: self)
end

#current_visitObject



19
20
21
# File 'lib/ahoy/controller.rb', line 19

def current_visit
  ahoy.visit
end

#set_ahoy_cookiesObject



23
24
25
26
27
28
29
30
31
# File 'lib/ahoy/controller.rb', line 23

def set_ahoy_cookies
  if Ahoy.cookies
    ahoy.set_visitor_cookie
    ahoy.set_visit_cookie
  else
    # delete cookies if exist
    ahoy.reset
  end
end

#set_ahoy_request_storeObject



43
44
45
# File 'lib/ahoy/controller.rb', line 43

def set_ahoy_request_store
  RequestStore.store[:ahoy] ||= ahoy
end

#track_ahoy_visitObject



33
34
35
36
37
38
39
40
41
# File 'lib/ahoy/controller.rb', line 33

def track_ahoy_visit
  defer = Ahoy.server_side_visits != true

  if defer && !Ahoy.cookies
    # avoid calling new_visit?, which triggers a database call
  elsif ahoy.new_visit?
    ahoy.track_visit(defer: defer)
  end
end