Sha256: 097bbe876f6ad1a77fc810293c9d8f339e79c687c9cdd0470d0c165671b3675e

Contents?: true

Size: 1.17 KB

Versions: 19

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module N1Loader
  module ActiveRecord
    module Associations
      module Preloader # :nodoc:
        N1LoaderReflection = Struct.new(:key, :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.key) 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_loader_defined?) && record.class.n1_loader_defined?(association)
          end

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

          hash.merge(super)
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
n1_loader-1.7.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.6 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.5 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.4 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.3 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.2 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.1 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.6.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.5.1 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.5.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.4.4 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.4.3 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.4.2 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.4.1 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.4.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.3.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.2.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.1.0 lib/n1_loader/active_record/associations_preloader_v5.rb
n1_loader-1.0.0 lib/n1_loader/active_record/associations_preloader_v5.rb