module Goldiloader::AssociationPatch

Public Instance Methods

auto_include?() click to toggle source
# File lib/goldiloader/active_record_patches.rb, line 113
def auto_include?
  # We only auto include associations that don't have in-memory changes since the
  # Rails association Preloader clobbers any in-memory changes
  !loaded? && target.blank? && eager_loadable?
end
fully_load?() click to toggle source
# File lib/goldiloader/active_record_patches.rb, line 119
def fully_load?
  !loaded? && options.fetch(:fully_load) { self.class.default_fully_load }
end

Private Instance Methods

eager_loadable?() click to toggle source
# File lib/goldiloader/active_record_patches.rb, line 125
def eager_loadable?
  klass && reflection.eager_loadable?(klass)
end
load_with_auto_include() { || ... } click to toggle source
# File lib/goldiloader/active_record_patches.rb, line 129
def load_with_auto_include
  return yield unless Goldiloader.enabled?

  if loaded? && !stale_target?
    target
  elsif !auto_include?
    yield
  elsif owner.auto_include_context.size == 1
    # Bypassing the preloader for a single model reduces object allocations by ~5% in benchmarks
    result = yield
    # As of https://github.com/rails/rails/commit/bd3b28f7f181dce53e872daa23dda101498b8fb4
    # ActiveRecord does not use ActiveRecord::Relation#exec_queries to resolve association
    # queries
    Goldiloader::AutoIncludeContext.register_models(result)
    result
  else
    Goldiloader::AssociationLoader.load(owner, reflection.name)
    target
  end
end