class Goldiloader::AutoIncludeContext

Attributes

models[R]

Public Class Methods

new() click to toggle source
# File lib/goldiloader/auto_include_context.rb, line 9
def initialize
  @models = []
end
register_models(models, included_associations = nil) click to toggle source
# File lib/goldiloader/auto_include_context.rb, line 13
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

Public Instance Methods

register_model(models)
Alias for: register_models
register_models(models) click to toggle source
# File lib/goldiloader/auto_include_context.rb, line 39
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
Also aliased as: register_model