Sha256: 16e3b4e123a17170edb3b1b3964c281a71212c9e18ecb587495bbc5708e0e5ba

Contents?: true

Size: 887 Bytes

Versions: 8

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true
require 'active_record'

module ActiveRecordCustomPreloader
  class AssociationsPreloader < ::ActiveRecord::Associations::Preloader
    private

    def preloaders_on(association, records, scope)
      if association.is_a?(PreloadWithOptions)
        preloaders_for_one(association, records, scope)
        return
      end

      klass = records.first.class
      if klass.respond_to?(:has_custom_loader?) && klass.has_custom_loader?(association)
        association = PreloadWithOptions.new(association)
        preloaders_for_one(association, records, scope)
        return
      end

      super
    end

    def preloaders_for_one(association, records, scope)
      if records.size > 0 && association.is_a?(PreloadWithOptions)
        association.loader_for(records.first.class).preload(records)
        return
      end

      super
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
active_record_custom_preloader-1.0.0 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.5.1 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.5.0 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.4.2 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.4.1 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.4.0 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.3.0 lib/active_record_custom_preloader/associations_preloader.rb
active_record_custom_preloader-0.2.2 lib/active_record_custom_preloader/associations_preloader.rb