ar_lazy_preload 2.1.2 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ar_lazy_preload/active_record/association_relation.rb +1 -0
- data/lib/ar_lazy_preload/active_record/base.rb +5 -0
- data/lib/ar_lazy_preload/active_record/collection_association.rb +13 -0
- data/lib/ar_lazy_preload/active_record/collection_proxy.rb +7 -0
- data/lib/ar_lazy_preload/active_record/relation.rb +9 -2
- data/lib/ar_lazy_preload/context.rb +13 -0
- data/lib/ar_lazy_preload/contexts/base_context.rb +14 -24
- data/lib/ar_lazy_preload/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: aafa6eee8d36facbfcb8ebdf81ebb1d2f82655210304afacdd6d4943d1e844a3
|
|
4
|
+
data.tar.gz: d30aa21d55b033a80cf58d2fd4801ba065a68f737e03d9ee2886bf6045ea9c22
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 396b3e22f3aae6648b6e7297afa6956a416b37be3d89897f44c53f182120ee26c8f0bd70b70f1dca9761ec09e91b707c499720db46c607019f3984b4d030d49e
|
|
7
|
+
data.tar.gz: 2f58018265351c27246a5b2b606f6ea976bdca74ad045f08a4725f71d3ba7897f2b8fbe2e906994904b602e98e970db406d8d8b987d424d796fa02e9a731f283
|
|
@@ -23,9 +23,14 @@ module ArLazyPreload
|
|
|
23
23
|
def skip_preload
|
|
24
24
|
lazy_preload_context&.records&.delete(self)
|
|
25
25
|
self.lazy_preload_context = nil
|
|
26
|
+
@skip_preload = true
|
|
26
27
|
self
|
|
27
28
|
end
|
|
28
29
|
|
|
30
|
+
def skip_preload?
|
|
31
|
+
@skip_preload || false
|
|
32
|
+
end
|
|
33
|
+
|
|
29
34
|
def try_setup_auto_preload_context
|
|
30
35
|
ArLazyPreload::Context.register(records: [self]) if ArLazyPreload.config.auto_preload?
|
|
31
36
|
end
|
|
@@ -3,6 +3,19 @@
|
|
|
3
3
|
module ArLazyPreload
|
|
4
4
|
# ActiveRecord::CollectionAssociation patch with a hook for lazy preloading
|
|
5
5
|
module CollectionAssociation
|
|
6
|
+
attr_writer :preloads_associations_lazily
|
|
7
|
+
|
|
8
|
+
def preloads_associations_lazily?
|
|
9
|
+
@preloads_associations_lazily ||= false
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# The statement cache loads the target without Relation#load, so the records get their
|
|
13
|
+
# context here.
|
|
14
|
+
def load_target
|
|
15
|
+
need_context = preloads_associations_lazily? && !loaded?
|
|
16
|
+
super.tap { |records| Context.register_auto_preload(records: records) if need_context }
|
|
17
|
+
end
|
|
18
|
+
|
|
6
19
|
# rubocop:disable Metrics/AbcSize
|
|
7
20
|
def ids_reader
|
|
8
21
|
return super if owner.lazy_preload_context.blank?
|
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
module ArLazyPreload
|
|
4
4
|
# ActiveRecord::CollectionProxy patch for getting records for preload
|
|
5
5
|
module CollectionProxy
|
|
6
|
+
def preload_associations_lazily
|
|
7
|
+
proxy_association.preloads_associations_lazily = true
|
|
8
|
+
reset_scope
|
|
9
|
+
Context.register_auto_preload(records: target) if loaded?
|
|
10
|
+
self
|
|
11
|
+
end
|
|
12
|
+
|
|
6
13
|
private
|
|
7
14
|
|
|
8
15
|
def ar_lazy_preload_records
|
|
@@ -23,7 +23,7 @@ module ArLazyPreload
|
|
|
23
23
|
# Enhanced #load method will check if association has not been loaded yet and add a context
|
|
24
24
|
# for lazy preloading to loaded each record
|
|
25
25
|
def load
|
|
26
|
-
need_context = !loaded?
|
|
26
|
+
need_context = !loaded? || scheduled?
|
|
27
27
|
result = super
|
|
28
28
|
if need_context
|
|
29
29
|
Context.register(
|
|
@@ -44,7 +44,14 @@ module ArLazyPreload
|
|
|
44
44
|
#
|
|
45
45
|
# Same effect can be achieved by User.lazy_preload(posts: :comments)
|
|
46
46
|
def preload_associations_lazily
|
|
47
|
-
tap { |relation| relation.preloads_associations_lazily = true }
|
|
47
|
+
return spawn.tap { |relation| relation.preloads_associations_lazily = true } unless loaded?
|
|
48
|
+
|
|
49
|
+
self.preloads_associations_lazily = true
|
|
50
|
+
return self if scheduled?
|
|
51
|
+
|
|
52
|
+
preload_associations(records)
|
|
53
|
+
Context.register_auto_preload(records: records)
|
|
54
|
+
self
|
|
48
55
|
end
|
|
49
56
|
|
|
50
57
|
# Specify relationships to be loaded lazily when association is loaded for the first time. For
|
|
@@ -20,5 +20,18 @@ module ArLazyPreload
|
|
|
20
20
|
)
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
|
+
|
|
24
|
+
# Initiates auto preload context for given records, unless they already have one or skip
|
|
25
|
+
# preloading
|
|
26
|
+
def self.register_auto_preload(records:)
|
|
27
|
+
records_without_context, records_with_context =
|
|
28
|
+
records.partition { |record| record.lazy_preload_context.nil? }
|
|
29
|
+
# The whole context is replaced, so its records keep loading their associations together
|
|
30
|
+
lazy_contexts = records_with_context.map(&:lazy_preload_context).uniq.reject(&:auto_preload?)
|
|
31
|
+
|
|
32
|
+
records_to_register =
|
|
33
|
+
records_without_context.reject(&:skip_preload?) + lazy_contexts.flat_map(&:records)
|
|
34
|
+
register(records: records_to_register, auto_preload: true)
|
|
35
|
+
end
|
|
23
36
|
end
|
|
24
37
|
end
|
|
@@ -63,35 +63,25 @@ module ArLazyPreload
|
|
|
63
63
|
|
|
64
64
|
# Prepare context for intermediate associations in through chains
|
|
65
65
|
def prepare_through_association_contexts(association_name, filtered_records)
|
|
66
|
-
filtered_records
|
|
67
|
-
|
|
68
|
-
next if chain.empty?
|
|
66
|
+
intermediate_owners(association_name, filtered_records).each do |name, owners|
|
|
67
|
+
next if association_loaded?(name)
|
|
69
68
|
|
|
70
|
-
|
|
71
|
-
register_intermediate_context(klass, klass_records, intermediate_name)
|
|
72
|
-
end
|
|
69
|
+
register_intermediate_records(owners, name)
|
|
73
70
|
end
|
|
74
71
|
end
|
|
75
72
|
|
|
76
|
-
def
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
loaded_association_names.add(intermediate_name)
|
|
83
|
-
|
|
84
|
-
intermediate_records = collect_intermediate_records(klass_records, reflection)
|
|
85
|
-
return if intermediate_records.empty?
|
|
86
|
-
|
|
87
|
-
ArLazyPreload::Context.register(records: intermediate_records, auto_preload: true)
|
|
73
|
+
def intermediate_owners(association_name, filtered_records)
|
|
74
|
+
filtered_records.group_by(&:class).each_with_object({}) do |(klass, klass_records), owners|
|
|
75
|
+
through_association_chain(association_name, klass).each do |name|
|
|
76
|
+
(owners[name] ||= []).concat(klass_records)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
88
79
|
end
|
|
89
80
|
|
|
90
|
-
def
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
end.compact
|
|
81
|
+
def register_intermediate_records(owners, name)
|
|
82
|
+
associations = owners.map { |owner| owner.association(name) }.select(&:loaded?)
|
|
83
|
+
targets = associations.flat_map(&:target).compact.reject(&:lazy_preload_context)
|
|
84
|
+
ArLazyPreload::Context.register(records: targets, auto_preload: true)
|
|
95
85
|
end
|
|
96
86
|
|
|
97
87
|
# Extract chain of intermediate association names for through associations
|
|
@@ -103,7 +93,7 @@ module ArLazyPreload
|
|
|
103
93
|
current_reflection = reflection
|
|
104
94
|
|
|
105
95
|
while current_reflection&.options&.key?(:through)
|
|
106
|
-
through_name = current_reflection.options[:through]
|
|
96
|
+
through_name = current_reflection.options[:through].to_sym
|
|
107
97
|
chain.unshift(through_name)
|
|
108
98
|
current_reflection = klass.reflect_on_association(through_name)
|
|
109
99
|
end
|