module JitPreloader::PreloaderAssociation

Public Instance Methods

associate_records_to_owner(owner, records) click to toggle source

Original method: def associate_records_to_owner(owner, records)

association = owner.association(reflection.name)
association.loaded!
if reflection.collection?
  association.target.concat(records)
else
  association.target = records.first unless records.empty?
end

end

# File lib/jit_preloader/active_record/associations/preloader/ar5_association.rb, line 40
def associate_records_to_owner(owner, records)
  association = owner.association(reflection.name)
  association.loaded!

  if reflection.collection?
    # It is possible that some of the records are loaded already.
    # We don't want to duplicate them, but we also want to preserve
    # the original copy so that we don't blow away in-memory changes.
    new_records = association.target.any? ? records - association.target : records
    association.target.concat(new_records)
    association.loaded!
  else
    association.target ||= records.first unless records.empty?
  end
end
build_scope() click to toggle source
Calls superclass method
# File lib/jit_preloader/active_record/associations/preloader/ar5_association.rb, line 57
def build_scope
  super.tap do |scope|
    scope.jit_preload! if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
  end
end
run(preloader) click to toggle source
owners.each do |owner|
  associate_records_to_owner(owner, records[convert_key(owner[owner_key_name])] || [])
end

end

Calls superclass method
# File lib/jit_preloader/active_record/associations/preloader/ar5_association.rb, line 20
def run(preloader)
  return unless (reflection.scope.nil? || reflection.scope.arity == 0) && klass.ancestors.include?(ActiveRecord::Base)

  super.tap do
    if preloaded_records.any? && preloaded_records.none?(&:jit_preloader)
      JitPreloader::Preloader.attach(preloaded_records) if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
    end
  end
end