Module: JitPreloader::PreloaderAssociation

Defined in:
lib/jit_preloader/active_record/associations/preloader/ar5_association.rb,
lib/jit_preloader/active_record/associations/preloader/ar6_association.rb

Instance Method Summary collapse

Instance Method Details

#associate_records_to_owner(owner, records) ⇒ Object

Original method: def associate_records_to_owner(owner, records)

association = owner.association(reflection.name)
if reflection.collection?
  association.target = records
else
  association.target = records.first
end

end



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jit_preloader/active_record/associations/preloader/ar5_association.rb', line 38

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_scopeObject



55
56
57
58
59
# File 'lib/jit_preloader/active_record/associations/preloader/ar5_association.rb', line 55

def build_scope
  super.tap do |scope|
    scope.jit_preload! if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
  end
end

#runObject

self end



20
21
22
23
24
25
26
# File 'lib/jit_preloader/active_record/associations/preloader/ar5_association.rb', line 20

def run(preloader)
  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