Sha256: e8bcc319d8932158574446ba712953835ce195d138dd5b75953ad2a5f471239a

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

module JitPreloader
  module ActiveRecordAssociationsCollectionAssociation

    def load_target
      was_loaded = loaded?

      if !loaded? && owner.persisted? && owner.jit_preloader && (reflection.scope.nil? || reflection.scope.arity == 0)
        owner.jit_preloader.jit_preload(reflection.name)
      end

      jit_loaded = loaded?

      super.tap do |records|
        # We should not act on non-persisted objects, or ones that are already loaded.
        if owner.persisted? && !was_loaded
          # If we went through a JIT preload, then we will have attached another JitPreloader elsewhere.
          JitPreloader::Preloader.attach(records) if records.any? && !jit_loaded && JitPreloader.globally_enabled?

          # If the records were not pre_loaded
          records.each{ |record| record.jit_n_plus_one_tracking = true }

          if !jit_loaded && owner.jit_n_plus_one_tracking
            ActiveSupport::Notifications.publish("n_plus_one_query",
                                                 source: owner, association: reflection.name)
          end
        end
      end
    end
  end
end

ActiveRecord::Associations::CollectionAssociation.prepend(JitPreloader::ActiveRecordAssociationsCollectionAssociation)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
jit_preloader-3.1.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-3.0.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-2.1.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-2.0.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-1.0.4 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-1.0.3 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-1.0.2 lib/jit_preloader/active_record/associations/collection_association.rb