Sha256: 146396e346d9ae1db92ffa2a97b891352517e57b4315721d2b754990cf81b653

Contents?: true

Size: 884 Bytes

Versions: 3

Compression:

Stored size: 884 Bytes

Contents

module Spree
  class Tracker < Spree::Base
    TRACKING_ENGINES = %i(google_analytics segment).freeze
    enum engine: TRACKING_ENGINES

    after_commit :clear_cache

    validates :analytics_id, presence: true, uniqueness: { scope: [:engine, :store_id], case_sensitive: false }
    validates :store, presence: true

    scope :active, -> { where(active: true) }

    belongs_to :store

    def self.current(engine = nil, store = nil)
      engine ||= TRACKING_ENGINES.first
      store  ||= Spree::Store.default

      tracker = Rails.cache.fetch("current_tracker/#{engine}/#{store.id}") do
        active.find_by(store: store, engine: engine)
      end
      tracker.analytics_id.present? ? tracker : nil if tracker
    end

    def clear_cache
      TRACKING_ENGINES.each do |engine|
        Rails.cache.delete("current_tracker/#{engine}/#{store_id}")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_analytics_trackers-2.1.1 app/models/spree/tracker.rb
spree_analytics_trackers-2.1.0 app/models/spree/tracker.rb
spree_analytics_trackers-2.0.0 app/models/spree/tracker.rb