Sha256: ab8580a8409e5783299b3cff00b710fb0707ca23c2294ff4ec0dcc0a135bb769

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Ahoy
  module Stores
    class ActiveRecordStore < BaseStore
      def track_visit(options, &block)
        @visit =
          visit_model.new do |v|
            v.id = ahoy.visit_id
            v.visitor_id = ahoy.visitor_id
            v.user = user if v.respond_to?(:user=)
            v.started_at = options[:started_at]
          end

        set_visit_properties(visit)

        yield(visit) if block_given?

        begin
          visit.save!
          geocode(visit)
        rescue *unique_exception_classes
          # reset to nil so subsequent calls to track_event will load visit from DB
          @visit = nil
        end
      end

      def track_event(name, properties, options, &block)
        event =
          event_model.new do |e|
            e.id = options[:id]
            e.visit_id = ahoy.visit_id
            e.user = user if e.respond_to?(:user=)
            e.name = name
            e.properties = properties
            e.time = options[:time]
          end

        yield(event) if block_given?

        begin
          event.save!
        rescue *unique_exception_classes
          # do nothing
        end
      end

      def visit
        @visit ||= visit_model.where(id: ahoy.visit_id).first if ahoy.visit_id
      end

      protected

      def visit_model
        ::Visit
      end

      def event_model
        ::Ahoy::Event
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ahoy_matey-1.6.1 lib/ahoy/stores/active_record_store.rb
ahoy_matey-1.6.0 lib/ahoy/stores/active_record_store.rb
ahoy_matey-1.5.5 lib/ahoy/stores/active_record_store.rb