Sha256: 30dd3c9641b483f7436c8687b3f79adaa3c92b1408280c520d09a75a78646c02

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 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

        visit_properties.keys.each do |key|
          visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=")
        end

        yield(visit) if block_given?

        begin
          visit.save!
        rescue ActiveRecord::RecordNotUnique
          # do nothing
        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
            e.name = name
            e.properties = properties
            e.time = options[:time]
          end

        yield(event) if block_given?

        begin
          event.save!
        rescue ActiveRecord::RecordNotUnique
          # 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.0.2 lib/ahoy/stores/active_record_store.rb
ahoy_matey-1.0.1 lib/ahoy/stores/active_record_store.rb
ahoy_matey-1.0.0 lib/ahoy/stores/active_record_store.rb