ar_lazy_preload 2.1.1 → 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/README.md +8 -1
- 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/associated_context_builder.rb +3 -3
- 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
|
data/README.md
CHANGED
|
@@ -13,7 +13,10 @@ Used in production by:
|
|
|
13
13
|
- Toptal
|
|
14
14
|
- _Want to be here? Let me know_ 🙂
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
## Professional Support
|
|
17
|
+
|
|
18
|
+
Dealing with performance issues in your Rails app beyond N+1 queries?
|
|
19
|
+
I'm available for consulting — [get in touch](https://dmitrytsepelev.dev/consulting).
|
|
17
20
|
|
|
18
21
|
## Why should I use it?
|
|
19
22
|
|
|
@@ -64,6 +67,10 @@ posts = User.preload_associations_lazily.flat_map(&:posts)
|
|
|
64
67
|
# => SELECT * FROM posts WHERE user_id in (...)
|
|
65
68
|
```
|
|
66
69
|
|
|
70
|
+
## Companion gem for non-association N+1s
|
|
71
|
+
|
|
72
|
+
[`N1Loader`](https://github.com/djezzzl/n1_loader) complements ArLazyPreload for batched lazy loading in places where regular ActiveRecord associations are not available (for example, computed values, custom loaders, service/API calls, and GraphQL fields). ArLazyPreload can stay focused on association loading while N1Loader handles the remaining N+1-prone derived data.
|
|
73
|
+
|
|
67
74
|
## Gotchas
|
|
68
75
|
|
|
69
76
|
1. Lazy preloading [does not work](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/40/files) for ActiveRecord < 6 when `.includes` is called earlier:
|
|
@@ -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
|
-
spawn.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
|
|
@@ -23,7 +23,7 @@ module ArLazyPreload
|
|
|
23
23
|
|
|
24
24
|
# Takes all the associated records for the records, attached to the :parent_context and creates
|
|
25
25
|
# a preloading context for them
|
|
26
|
-
# rubocop:disable Metrics/MethodLength
|
|
26
|
+
# rubocop:disable Metrics/MethodLength
|
|
27
27
|
def perform
|
|
28
28
|
associated_records = parent_context.records.flat_map do |record|
|
|
29
29
|
next if record.nil?
|
|
@@ -32,7 +32,7 @@ module ArLazyPreload
|
|
|
32
32
|
next if reflection.nil?
|
|
33
33
|
|
|
34
34
|
record_association = record.association(association_name)
|
|
35
|
-
|
|
35
|
+
record_association.target
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
Context.register(
|
|
@@ -41,7 +41,7 @@ module ArLazyPreload
|
|
|
41
41
|
auto_preload: parent_context.auto_preload?
|
|
42
42
|
)
|
|
43
43
|
end
|
|
44
|
-
# rubocop:enable Metrics/MethodLength
|
|
44
|
+
# rubocop:enable Metrics/MethodLength
|
|
45
45
|
|
|
46
46
|
private
|
|
47
47
|
|
|
@@ -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
|