Sha256: 23021578c0b1346eb416b3b13185932df568184424d1ac3e3086c28ce591f12d

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module N1Loader
  module ActiveRecord
    module Associations
      module Preloader # :nodoc:
        N1LoaderReflection = Struct.new(:name, :loader) do
          def options
            {}
          end
        end

        def preloaders_for_reflection(reflection, records)
          return super unless reflection.is_a?(N1LoaderReflection)

          N1Loader::Preloader.new(records).preload(reflection.name)
        end

        def grouped_records # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
          n1_load_records, records = source_records.partition do |record|
            record.class.respond_to?(:n1_loaders) && record.class.n1_loaders[association]
          end

          h = n1_load_records.group_by do |record|
            N1LoaderReflection.new(association, record.class.n1_loaders[association])
          end

          polymorphic_parent = !root? && parent.polymorphic?
          records.each do |record|
            reflection = record.class._reflect_on_association(association)
            next if polymorphic_parent && !reflection || !record.association(association).klass

            (h[reflection] ||= []) << record
          end
          h
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
n1_loader-1.7.4 lib/n1_loader/active_record/associations_preloader_v7.rb
n1_loader-1.7.3 lib/n1_loader/active_record/associations_preloader_v7.rb
n1_loader-1.7.2 lib/n1_loader/active_record/associations_preloader_v7.rb