Sha256: d6e005e6bcedcf3faa3f94063c5a75ae8ecf69e8a7c41d13ab4d319157a766fa

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Goldiloader
  class AutoIncludeContext
    attr_reader :models

    delegate :size, to: :models

    def initialize
      @models = []
    end

    def self.register_models(models, included_associations = nil)
      auto_include_context = Goldiloader::AutoIncludeContext.new
      auto_include_context.register_models(models)

      Array.wrap(included_associations).each do |included_association|
        associations = if included_association.is_a?(Hash)
                         included_association.keys
                       else
                         Array.wrap(included_association)
                       end
        nested_associations = if included_association.is_a?(Hash)
                                included_association
                              else
                                Hash.new([])
                              end

        associations.each do |association|
          nested_models = models.flat_map do |model|
            model.association(association).target
          end.compact

          register_models(nested_models, nested_associations[association])
        end
      end
    end

    def register_models(models)
      # Don't use Array() or Array.wrap() because they will check respond_to?(:to_ary)
      # which for ActiveStorage::Attachment will delegate to the blob association which
      # triggers an infinite eager loading loop on the association
      models = [models] unless models.is_a?(Array)
      models.each do |model|
        next if model.nil?

        model.auto_include_context = self
        self.models << model
      end
      self
    end

    alias_method :register_model, :register_models

    prepend Goldiloader::CustomPreloads
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
goldiloader-5.4.0 lib/goldiloader/auto_include_context.rb
goldiloader-5.3.1 lib/goldiloader/auto_include_context.rb
goldiloader-5.3.0 lib/goldiloader/auto_include_context.rb
goldiloader-5.2.2 lib/goldiloader/auto_include_context.rb
goldiloader-5.2.1 lib/goldiloader/auto_include_context.rb
goldiloader-5.2.0 lib/goldiloader/auto_include_context.rb
goldiloader-5.1.0 lib/goldiloader/auto_include_context.rb