ar_lazy_preload 0.3.2 → 0.4.0
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 +10 -0
- data/lib/ar_lazy_preload/active_record/base.rb +1 -0
- data/lib/ar_lazy_preload/active_record/relation.rb +20 -1
- data/lib/ar_lazy_preload/associated_context_builder.rb +7 -3
- data/lib/ar_lazy_preload/context.rb +2 -2
- data/lib/ar_lazy_preload/contexts/auto_preload_context.rb +4 -0
- data/lib/ar_lazy_preload/contexts/base_context.rb +4 -0
- data/lib/ar_lazy_preload/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 302e5031cc0d1046c7ef7400dca6767fb13e4f8e68ba98a7cfa20b648f64d60a
|
4
|
+
data.tar.gz: 40cc089d2e4863636d21f13bc9273f0c1341c209275d9162e6ce3ecad05c7e24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00b77bb2a441ad78f74d96776d36073dbdcc29d811d98de5430ec750217c4f6d8bbf103465debd67c67fd3d86a296f8ab39d2d3e098f193fb348657d0d38c30f
|
7
|
+
data.tar.gz: 0e06520adf62f0bb9f0a1bb7b6d9d667b4478b206a4ffcb1e7c40efd2314d7d0205e72cc27bbb214a1a93e6a3d4a5cd5c5def11d458dfcb3054bd4240162c818
|
data/README.md
CHANGED
@@ -50,6 +50,16 @@ If you want to turn automatic preload off for a specific record, you can call `.
|
|
50
50
|
users.first.skip_preload.posts # => SELECT * FROM posts WHERE user_id = ?
|
51
51
|
```
|
52
52
|
|
53
|
+
### Relation auto preloading
|
54
|
+
|
55
|
+
Another alternative for auto preloading is using relation `#preload_associations_lazily` method
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
posts = User.preload_associations_lazily.flat_map(&:posts)
|
59
|
+
# => SELECT * FROM users LIMIT 10
|
60
|
+
# => SELECT * FROM posts WHERE user_id in (...)
|
61
|
+
```
|
62
|
+
|
53
63
|
## Installation
|
54
64
|
|
55
65
|
Add this line to your application's Gemfile, and you're all set:
|
@@ -5,6 +5,8 @@ require "ar_lazy_preload/context"
|
|
5
5
|
module ArLazyPreload
|
6
6
|
# ActiveRecord::Relation patch with lazy preloading support
|
7
7
|
module Relation
|
8
|
+
attr_writer :preloads_associations_lazily
|
9
|
+
|
8
10
|
# Enhanced #load method will check if association has not been loaded yet and add a context
|
9
11
|
# for lazy preloading to loaded each record
|
10
12
|
def load
|
@@ -13,12 +15,25 @@ module ArLazyPreload
|
|
13
15
|
if need_context
|
14
16
|
Context.register(
|
15
17
|
records: ar_lazy_preload_records,
|
16
|
-
association_tree: lazy_preload_values
|
18
|
+
association_tree: lazy_preload_values,
|
19
|
+
auto_preload: preloads_associations_lazily?
|
17
20
|
)
|
18
21
|
end
|
19
22
|
result
|
20
23
|
end
|
21
24
|
|
25
|
+
# Lazily autoloads all associations. For example:
|
26
|
+
#
|
27
|
+
# users = User.preload_associations_lazily
|
28
|
+
# users.each do |user|
|
29
|
+
# user.posts.flat_map {|post| post.comments.map(&:id)}
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# Same effect can be achieved by User.lazy_preload(posts: :comments)
|
33
|
+
def preload_associations_lazily
|
34
|
+
spawn.tap { |relation| relation.preloads_associations_lazily = true }
|
35
|
+
end
|
36
|
+
|
22
37
|
# Specify relationships to be loaded lazily when association is loaded for the first time. For
|
23
38
|
# example:
|
24
39
|
#
|
@@ -56,6 +71,10 @@ module ArLazyPreload
|
|
56
71
|
@records
|
57
72
|
end
|
58
73
|
|
74
|
+
def preloads_associations_lazily?
|
75
|
+
@preloads_associations_lazily ||= false
|
76
|
+
end
|
77
|
+
|
59
78
|
attr_writer :lazy_preload_values
|
60
79
|
end
|
61
80
|
end
|
@@ -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
|
-
def perform
|
26
|
+
def perform # rubocop:disable Metrics/MethodLength
|
27
27
|
associated_records = parent_context.records.flat_map do |record|
|
28
28
|
next if record.nil?
|
29
29
|
|
@@ -32,14 +32,18 @@ module ArLazyPreload
|
|
32
32
|
reflection.collection? ? record_association.target : record_association.reader
|
33
33
|
end
|
34
34
|
|
35
|
-
Context.register(
|
35
|
+
Context.register(
|
36
|
+
records: associated_records,
|
37
|
+
association_tree: child_association_tree,
|
38
|
+
auto_preload: parent_context.auto_preload?
|
39
|
+
)
|
36
40
|
end
|
37
41
|
|
38
42
|
private
|
39
43
|
|
40
44
|
def child_association_tree
|
41
45
|
# `association_tree` is unnecessary when auto preload is enabled
|
42
|
-
return nil if
|
46
|
+
return nil if parent_context.auto_preload?
|
43
47
|
|
44
48
|
AssociationTreeBuilder.new(parent_context.association_tree).subtree_for(association_name)
|
45
49
|
end
|
@@ -7,10 +7,10 @@ require "ar_lazy_preload/contexts/lazy_preload_context"
|
|
7
7
|
module ArLazyPreload
|
8
8
|
class Context
|
9
9
|
# Initiates lazy preload context for given records
|
10
|
-
def self.register(records:, association_tree:)
|
10
|
+
def self.register(records:, association_tree:, auto_preload: false)
|
11
11
|
return if records.empty?
|
12
12
|
|
13
|
-
if ArLazyPreload.config.auto_preload?
|
13
|
+
if ArLazyPreload.config.auto_preload? || auto_preload
|
14
14
|
Contexts::AutoPreloadContext.new(records: records)
|
15
15
|
elsif association_tree.any?
|
16
16
|
Contexts::LazyPreloadContext.new(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_lazy_preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|