Sha256: 264072eada8d7f3dbd16b9ac5c16c00257efbbcef0ae2ca638776d4b901d4bcc

Contents?: true

Size: 1017 Bytes

Versions: 1

Compression:

Stored size: 1017 Bytes

Contents

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 do
        RequestStore.store[:ahoy_controller] ||= self
      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
    end

    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ahoy_matey-0.3.0 lib/ahoy/controller.rb