Sha256: 13f447f0b113e037e384c3462f7961e5b0f518141324e7d37fa5bc994c11807c

Contents?: true

Size: 975 Bytes

Versions: 1

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true
require 'active_record'

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

    def preloaders_on(association, records, scope, polymorphic_parent = false)
      if association.is_a?(PreloadWithOptions)
        preloaders_for_one(association, records, scope, polymorphic_parent)
        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, polymorphic_parent)
        return
      end

      super
    end

    def preloaders_for_one(association, records, scope, polymorphic_parent)
      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

1 entries across 1 versions & 1 rubygems

Version Path
active_record_custom_preloader-2.0.0 lib/active_record_custom_preloader/associations_preloader.rb