9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/identity_cache/cached/prefetcher.rb', line 9
def prefetch(klass, associations, records, load_strategy: LoadStrategy::Eager)
return if (records = records.to_a).empty?
case associations
when Symbol
fetch_association(load_strategy, klass, associations, records) {}
when Array
load_strategy.lazy_load do |lazy_loader|
associations.each do |association|
prefetch(klass, association, records, load_strategy: lazy_loader)
end
end
when Hash
load_strategy.lazy_load do |lazy_loader|
associations.each do |association, sub_associations|
fetch_association(lazy_loader, klass, association, records) do |next_level_records|
if sub_associations.present?
association_class = klass.reflect_on_association(association).klass
prefetch(association_class, sub_associations, next_level_records, load_strategy: lazy_loader)
end
end
end
end
else
raise TypeError, "Invalid associations class #{associations.class}"
end
end
|