Sha256: 534ae5cbe4d280711641b645e93dbd0f5496666f4c33f57729c25c4a52bbaf7a

Contents?: true

Size: 1.42 KB

Versions: 17

Compression:

Stored size: 1.42 KB

Contents

module Workarea
  module CurrentTracking
    extend ActiveSupport::Concern
    include HttpCaching

    included do
      before_action :ensure_current_metrics
      helper_method :current_visit, :current_metrics

      delegate :current_metrics_id, :current_metrics_id=, :browser,
        to: :current_visit, allow_nil: true
    end

    def current_visit
      request.env['workarea.visit']
    end

    def current_metrics
      current_visit.metrics
    end

    def current_referrer
      current_visit.referrer
    end

    def update_tracking!(email: current_user&.email)
      if email.blank?
        cookies.delete(:email)
      elsif email != cookies.signed[:email]
        Metrics::User.find_or_initialize_by(id: email).merge!(current_visit&.metrics)
        cookies.permanent.signed[:email] = email
      end

      request.env['workarea.visit'] = Visit.new(request.env)
    end

    # This method is mostly for tests, but could surface in an implementation.
    # Since Rails doesn't set and load a session until needed, an initial
    # request won't have a session ID for `current_visit` to check when loading
    # metrics. This ensures there will be one.
    def ensure_current_metrics
      return if current_metrics_id.present? || http_caching?

      # This forces Rails to initialize the session, which provides an ID for metrics
      session.delete(:foo)
      self.current_metrics_id = session.id.cookie_value
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
workarea-core-3.5.18 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.17 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.16 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.15 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.14 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.13 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.12 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.11 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.10 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.9 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.8 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.7 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.6 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.5 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.4 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.3 app/controllers/workarea/current_tracking.rb
workarea-core-3.5.2 app/controllers/workarea/current_tracking.rb