Module: N1Loader::ActiveRecord::Associations::Preloader

Defined in:
lib/n1_loader/active_record/associations_preloader_v5.rb,
lib/n1_loader/active_record/associations_preloader_v6.rb,
lib/n1_loader/active_record/associations_preloader_v7.rb

Overview

:nodoc:

Defined Under Namespace

Classes: N1LoaderReflection

Instance Method Summary collapse

Instance Method Details

#grouped_recordsObject

rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/n1_loader/active_record/associations_preloader_v7.rb', line 19

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

#preloaders_for_one(association, records, scope) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/n1_loader/active_record/associations_preloader_v5.rb', line 13

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

#preloaders_for_reflection(reflection, records) ⇒ Object



13
14
15
16
17
# File 'lib/n1_loader/active_record/associations_preloader_v6.rb', line 13

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

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