class JitPreloader::Preloader

Attributes

records[RW]

Public Class Methods

_load(args) click to toggle source
# File lib/jit_preloader/preloader.rb, line 32
def self._load(args)
  nil
end
attach(records) click to toggle source
# File lib/jit_preloader/preloader.rb, line 6
def self.attach(records)
  new.tap do |loader|
    loader.records = records.dup
    records.each do |record|
      record.jit_preloader = loader
    end
  end
end

Public Instance Methods

_dump(level) click to toggle source

We do not want the jit_preloader to be dumpable If you dump a ActiveRecord::Base object that has a jit_preloader instance variable you will also end up dumping all of the records the preloader has reference to. Imagine getting N objects from a query and dumping each one of those into a cache each object would dump N+1 objects which means you'll end up storing O(N^2) memory. Thats no good. So instead, we will just nullify the jit_preloader on load

# File lib/jit_preloader/preloader.rb, line 28
def _dump(level)
  ""
end
jit_preload(association) click to toggle source
# File lib/jit_preloader/preloader.rb, line 15
def jit_preload(association)
  # It is possible that the records array has multiple different classes (think single table inheritance).
  # Thus, it is possible that some of the records don't have an association.
  records_with_association = records.reject{|r| r.class.reflect_on_association(association).nil? }
  preload records_with_association, association
end