Sha256: cb56f97c5f3be4443bdc978ededeed88040d73fa3be08810f68a03682bd747d2

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Goldiloader
  module AssociationLoader
    extend self

    def load(model, association_name)
      models = model.auto_include_context.models.select do |peer|
        load?(peer, association_name)
      end

      eager_load(models, association_name)
    end

    private

    def eager_load(models, association_name)
      if Goldiloader::Compatibility.pre_rails_7?
        ::ActiveRecord::Associations::Preloader.new.preload(models, [association_name])
      else
        ::ActiveRecord::Associations::Preloader.new(records: models, associations: [association_name]).call
      end
    end

    def load?(model, association_name)
      # Need to make sure the model actually has the association which won't always
      # be the case in STI hierarchies e.g. only a subclass might have the association
      has_association?(model, association_name) &&
        model.association(association_name).auto_include?
    end

    def has_association?(model, association_name) # rubocop:disable Naming/PredicateName
      model.class.reflect_on_association(association_name).present?
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
goldiloader-5.4.0 lib/goldiloader/association_loader.rb
goldiloader-5.3.1 lib/goldiloader/association_loader.rb
goldiloader-5.3.0 lib/goldiloader/association_loader.rb
goldiloader-4.2.1 lib/goldiloader/association_loader.rb
goldiloader-5.2.2 lib/goldiloader/association_loader.rb
goldiloader-5.2.1 lib/goldiloader/association_loader.rb
goldiloader-5.2.0 lib/goldiloader/association_loader.rb
goldiloader-5.1.0 lib/goldiloader/association_loader.rb
goldiloader-5.0.0 lib/goldiloader/association_loader.rb
goldiloader-4.2.0 lib/goldiloader/association_loader.rb
goldiloader-4.1.2 lib/goldiloader/association_loader.rb