Sha256: 932f57b830424ca4c63bc43e95142936acfb91d6b1b969646d3dd5aa859b2987

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

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
      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
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ahoy_matey-0.3.2 lib/ahoy/controller.rb
ahoy_matey-0.3.1 lib/ahoy/controller.rb