Sha256: 8f9a6aec10154325469ea72c800220edea2a6ec23b0e38d89f781d016d523e24

Contents?: true

Size: 1.15 KB

Versions: 10

Compression:

Stored size: 1.15 KB

Contents

module JitPreloader
  module ActiveRecordAssociationsCollectionAssociation

    def load_target
      was_loaded = loaded?

      if !loaded? && owner.persisted? && owner.jit_preloader
        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

10 entries across 10 versions & 1 rubygems

Version Path
jit_preloader-1.0.1 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-1.0.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.3.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.2.5 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.2.4 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.2.3 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.2.2 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.2.1 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.2.0 lib/jit_preloader/active_record/associations/collection_association.rb
jit_preloader-0.1.0 lib/jit_preloader/active_record/associations/collection_association.rb