ar_lazy_preload 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/ar_lazy_preload/context.rb +19 -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: 7060cc83164f3fff3a468514246ecbb5799accc1999dab8581c97db4d10f016a
|
4
|
+
data.tar.gz: 98075d278f0d04fa68ae9b31ddc60e0348b6a896460f512bc27195113095e700
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aad5db14a66fb29fa11cfbd2968e2b3dbd54110ee51f89d95ca6a5c9538125c46732a03d7d4539f3b1c251b2ae6a5c4c986b3a677d93bd76d6384c6e843c11bb
|
7
|
+
data.tar.gz: 482285553c6d4b1057e0d5ee173eef21f14a7e5c5214c093614ee3730677d3c32f0039b5b387da4b05023713b7868ae75b22d23cf18edd0200f1176fd6b78b4a
|
data/README.md
CHANGED
@@ -6,6 +6,9 @@
|
|
6
6
|
|
7
7
|
# ArLazyPreload
|
8
8
|
|
9
|
+
<a href="https://evilmartians.com/?utm_source=ar_lazy_preload">
|
10
|
+
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
11
|
+
|
9
12
|
Lazy loading associations for the ActiveRecord models. `#includes`, `#eager_load` and `#preload` are built-in methods to avoid N+1 problem, but sometimes when DB request is made we don't know what associations we are going to need later (for instance when your API allows client to define a list of loaded associations dynamically). The only possible solution for such cases is to load _all_ the associations we might need, but it can be a huge overhead.
|
10
13
|
|
11
14
|
This gem allows to set up _lazy_ preloading for associations - it won't load anything until association is called for a first time, but when it happens - it loads all the associated records for all records from the initial relation in a single query.
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "set"
|
3
4
|
require "ar_lazy_preload/associated_context_builder"
|
4
5
|
|
5
6
|
module ArLazyPreload
|
@@ -34,11 +35,17 @@ module ArLazyPreload
|
|
34
35
|
|
35
36
|
preloader.preload(records, association_name)
|
36
37
|
AssociatedContextBuilder.prepare(parent_context: self, association_name: association_name)
|
38
|
+
# Our tracking of loading state
|
39
|
+
# Otherwise `#preload` will be called many times even when association is loaded
|
40
|
+
mark_association_as_loaded(association_name)
|
37
41
|
end
|
38
42
|
|
39
43
|
private
|
40
44
|
|
41
45
|
def association_needs_preload?(association_name, node_tree = association_tree)
|
46
|
+
# Check whether association loading state
|
47
|
+
# to avoid calling preload unnecessarily
|
48
|
+
return false if association_loaded?(association_name)
|
42
49
|
return true if ArLazyPreload.config.auto_preload?
|
43
50
|
|
44
51
|
node_tree.any? do |node|
|
@@ -50,6 +57,18 @@ module ArLazyPreload
|
|
50
57
|
end
|
51
58
|
end
|
52
59
|
|
60
|
+
def mark_association_as_loaded(association_name)
|
61
|
+
loaded_association_names.add(association_name)
|
62
|
+
end
|
63
|
+
|
64
|
+
def association_loaded?(association_name)
|
65
|
+
loaded_association_names.include?(association_name)
|
66
|
+
end
|
67
|
+
|
68
|
+
def loaded_association_names
|
69
|
+
@loaded_association_names ||= Set.new
|
70
|
+
end
|
71
|
+
|
53
72
|
def preloader
|
54
73
|
@preloader ||= ActiveRecord::Associations::Preloader.new
|
55
74
|
end
|
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.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|