module Goldiloader::AssociationReflectionPatch

Public Instance Methods

eager_loadable?(target_klass) click to toggle source

Note we need to pass the association’s target class as an argument since it won’t be known outside the context of an association instance for polymorphic associations.

# File lib/goldiloader/active_record_patches.rb, line 82
def eager_loadable?(target_klass)
  @eager_loadable_cache ||= Hash.new do |cache, target_klass_key|
    cache[target_klass_key] = if scope.nil?
                                # Associations without any scoping options are eager loadable
                                true
                              elsif scope.arity > 0
                                # The scope will be evaluated for every model instance so it can't
                                # be eager loaded
                                false
                              else
                                scope_info = Goldiloader::ScopeInfo.new(scope_for(target_klass_key.unscoped))
                                scope_info.auto_include? &&
                                  !scope_info.limit? &&
                                  !scope_info.offset? &&
                                  (!has_one? || !scope_info.order?)
                              end
  end
  @eager_loadable_cache[target_klass]
end