Sha256: fcc1ad02e28efd66af3f779e533cf467071f9fb681a8a4894a1db5effa5b918f

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 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_one(association, records, scope)
          grouped_records(association, records).flat_map do |reflection, klasses|
            next N1Loader::Preloader.new(records).preload(reflection.name) if reflection.is_a?(N1LoaderReflection)

            klasses.map do |rhs_klass, rs|
              loader = preloader_for(reflection, rs).new(rhs_klass, rs, reflection, scope)
              loader.run self
              loader
            end
          end
        end

        def grouped_records(association, records)
          n1_load_records, records = records.partition do |record|
            record.class.respond_to?(:n1_loaders) && record.class.n1_loaders.key?(association)
          end

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

          hash.merge(super)
        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_v5.rb
n1_loader-1.7.3 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.7.2 lib/n1_loader/active_record/associations_preloader_v5.rb