Module: Goldiloader::AssociationReflectionPatch

Defined in:
lib/goldiloader/active_record_patches.rb

Instance Method Summary collapse

Instance Method Details

#eager_loadable?(target_klass) ⇒ Boolean

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.

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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