lib/ahoy/controller.rb in ahoy_matey-0.2.2 vs lib/ahoy/controller.rb in ahoy_matey-0.3.0
- old
+ new
@@ -2,23 +2,36 @@
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 do
RequestStore.store[:ahoy_controller] ||= self
end
end
+ def ahoy
+ @ahoy ||= Ahoy::Tracker.new(controller: self)
+ end
+
def current_visit
- visit_token = cookies[:ahoy_visit] || request.headers["Ahoy-Visit"]
+ visit_token = current_visit_token
if visit_token
@current_visit ||= Ahoy.visit_model.where(visit_token: visit_token).first
end
end
- def ahoy
- @ahoy ||= Ahoy::Tracker.new(controller: self)
+ def current_visit_token
+ @current_visit_token ||= request.headers["Ahoy-Visit"] || cookies[:ahoy_visit]
+ 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
+ cookies[:ahoy_visitor] = current_visitor_token if !request.headers["Ahoy-Visitor"] && !cookies[:ahoy_visitor]
end
end
end