Sha256: 81c57fc79ba9cb0951e3429aea0afc1af664a6dcd0288ea5bfe8d5a5606d2132

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 KB

Contents

module JitPreloader
  module ActiveRecordRelation

    def jit_preload(*args)
      spawn.jit_preload!(*args)
    end

    def jit_preload!(*args)
      @jit_preload = true
      self
    end

    def jit_preload?
      @jit_preload
    end

    def calculate(*args)
      if respond_to?(:proxy_association) && proxy_association.owner && proxy_association.owner.jit_n_plus_one_tracking
        ActiveSupport::Notifications.publish("n_plus_one_query",
                                             source: proxy_association.owner,
                                             association: "#{proxy_association.reflection.name}.#{args.first}")
      end

      super(*args)
    end

    def exec_queries
      super.tap do |records|
        if limit_value != 1
          records.each{ |record| record.jit_n_plus_one_tracking = true }
          if jit_preload? || JitPreloader.globally_enabled?
            JitPreloader::Preloader.attach(records)
          end
        end
      end
    end

  end
end

ActiveRecord::Relation.prepend(JitPreloader::ActiveRecordRelation)

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
jit_preloader-3.1.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-3.0.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-2.1.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-2.0.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-1.0.4 lib/jit_preloader/active_record/relation.rb
jit_preloader-1.0.3 lib/jit_preloader/active_record/relation.rb
jit_preloader-1.0.2 lib/jit_preloader/active_record/relation.rb
jit_preloader-1.0.1 lib/jit_preloader/active_record/relation.rb
jit_preloader-1.0.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.3.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.2.5 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.2.4 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.2.3 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.2.2 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.2.1 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.2.0 lib/jit_preloader/active_record/relation.rb
jit_preloader-0.1.0 lib/jit_preloader/active_record/relation.rb