Module: JitPreloader::PreloaderAssociation

Defined in:
lib/jit_preloader/active_record/associations/preloader/ar6_association.rb,
lib/jit_preloader/active_record/associations/preloader/ar7_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)

return if loaded?(owner)

association = owner.association(reflection.name)

if reflection.collection?
  association.target = records
else
  association.target = records.first
end

end



37
38
39
40
41
42
43
44
45
46
# File 'lib/jit_preloader/active_record/associations/preloader/ar6_association.rb', line 37

def associate_records_to_owner(owner, records)
  association = owner.association(reflection.name)
  if reflection.collection?
    new_records = association.target.any? ? records - association.target : records
    association.target.concat(new_records)
    association.loaded!
  else
    association.target = records.first
  end
end

#build_scopeObject



48
49
50
51
52
# File 'lib/jit_preloader/active_record/associations/preloader/ar6_association.rb', line 48

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

#runObject

self end



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

def run
  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