lib/ahoy/controller.rb in ahoy_matey-0.3.2 vs lib/ahoy/controller.rb in ahoy_matey-1.0.0
- old
+ new
@@ -1,43 +1,35 @@
+require "request_store"
+
module Ahoy
module Controller
def self.included(base)
base.helper_method :current_visit
base.helper_method :ahoy
- base.before_filter :set_ahoy_visitor_cookie
+ base.before_filter :set_ahoy_cookies
+ base.before_filter :track_ahoy_visit
base.before_filter do
- RequestStore.store[:ahoy_controller] ||= self
+ RequestStore.store[:ahoy] ||= ahoy
end
end
def ahoy
@ahoy ||= Ahoy::Tracker.new(controller: self)
end
def current_visit
- visit_token = current_visit_token
- if visit_token
- @current_visit ||= Ahoy.visit_model.where(visit_token: visit_token).first
- end
+ ahoy.visit
end
- def current_visit_token
- @current_visit_token ||= request.headers["Ahoy-Visit"] || cookies[:ahoy_visit]
+ def set_ahoy_cookies
+ ahoy.set_visitor_cookie
+ ahoy.set_visit_cookie
end
- def current_visitor_token
- @current_visitor_token ||= request.headers["Ahoy-Visitor"] || cookies[:ahoy_visitor] || current_visit.try(:visitor_token) || Ahoy.generate_id
- end
-
- def set_ahoy_visitor_cookie
- if !request.headers["Ahoy-Visitor"] && !cookies[:ahoy_visitor]
- cookie = {
- value: current_visitor_token,
- expires: 2.years.from_now
- }
- cookie[:domain] = Ahoy.domain if Ahoy.domain
- cookies[:ahoy_visitor] = cookie
+ def track_ahoy_visit
+ if ahoy.new_visit?
+ ahoy.track_visit(defer: !Ahoy.track_visits_immediately)
end
end
end
end