ar_lazy_preload 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -37
- data/Rakefile +10 -0
- data/lib/ar_lazy_preload/associated_context_builder.rb +2 -2
- data/lib/ar_lazy_preload/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e0790a19fd33c7b141ba62725951c084f84cd0d1e8678e807081dac6d1b9c4c
|
4
|
+
data.tar.gz: 714b3e6785f5ad4dd7278be3549638ec8ab2447cad82cc3f9a5cd53694d23006
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3f9bfdd8dec8fb021f96ad79b686248e15dc30e23585a3c7e386033c72ec8864d75de8c8a21c4c0ef00e57e63e61c7bd147a6554d5e634f6282959f11bade82
|
7
|
+
data.tar.gz: d9537e25cc267fdc4278f2ae467945482abb78e2716d823c915ce2c5d82c7190ed9713ab8ad2cf5f219eceb4b72954c9eb25854b5b593e3c9726d0dcef50b8cb
|
data/README.md
CHANGED
@@ -1,67 +1,57 @@
|
|
1
|
-
[![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](https://cultofmartians.com/tasks/activerecord-lazy-preload.html)
|
2
|
-
[![Gem Version](https://badge.fury.io/rb/ar_lazy_preload.svg)](https://rubygems.org/gems/ar_lazy_preload)
|
3
|
-
[![Build Status](https://travis-ci.org/DmitryTsepelev/ar_lazy_preload.svg?branch=master)](https://travis-ci.org/DmitryTsepelev/ar_lazy_preload)
|
4
|
-
[![Maintainability](https://api.codeclimate.com/v1/badges/00d04595661820dfba80/maintainability)](https://codeclimate.com/github/DmitryTsepelev/ar_lazy_preload/maintainability)
|
5
|
-
[![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/ar_lazy_preload/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/ar_lazy_preload?branch=master)
|
1
|
+
# ArLazyPreload [![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](https://cultofmartians.com/tasks/activerecord-lazy-preload.html) [![Gem Version](https://badge.fury.io/rb/ar_lazy_preload.svg)](https://rubygems.org/gems/ar_lazy_preload) [![Build Status](https://travis-ci.org/DmitryTsepelev/ar_lazy_preload.svg?branch=master)](https://travis-ci.org/DmitryTsepelev/ar_lazy_preload) [![Maintainability](https://api.codeclimate.com/v1/badges/00d04595661820dfba80/maintainability)](https://codeclimate.com/github/DmitryTsepelev/ar_lazy_preload/maintainability) [![Coverage Status](https://coveralls.io/repos/github/DmitryTsepelev/ar_lazy_preload/badge.svg?branch=master)](https://coveralls.io/github/DmitryTsepelev/ar_lazy_preload?branch=master)
|
6
2
|
|
7
|
-
#
|
3
|
+
**ArLazyPreload** is a gem that brings association lazy load functionality to your Rails applications. There is a number of built-in methods to solve [N+1 problem](https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations), but sometimes a list of associations to preload is not obvious–this is when you can get most of this gem.
|
8
4
|
|
9
|
-
|
10
|
-
|
5
|
+
- **Simple**. The only thing you need to change is to use `#lazy_preload` instead of `#includes`, `#eager_load` or `#preload`
|
6
|
+
- **Fast**. Take a look at [benchmarks](https://travis-ci.org/DmitryTsepelev/ar_lazy_preload) (`TASK=bench` and `TASK=memory`)
|
7
|
+
- **Perfect fit for GraphQL**. Define a list of associations to load at the top-level resolver and let the gem do its job
|
8
|
+
- **Auto-preload support**. If you don't want to specify the association list–set `ArLazyPreload.config.auto_preload` to `true`
|
11
9
|
|
12
|
-
|
10
|
+
<p align="center">
|
11
|
+
<a href="https://evilmartians.com/?utm_source=ar_lazy_preload">
|
12
|
+
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
13
|
+
</a>
|
14
|
+
</p>
|
13
15
|
|
14
|
-
|
16
|
+
## Why should I use it?
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
Add this line to your application's Gemfile, and you're all set:
|
18
|
+
Lazy loading is super helpful when the list of associations to load is determined dynamically. For instance, in GraphQL this list comes from the API client, and you'll have to inspect the selection set to find out what associations are going to be used.
|
19
19
|
|
20
|
-
|
21
|
-
gem "ar_lazy_preload"
|
22
|
-
```
|
20
|
+
This gem uses a different approach: it won't load anything until the association is called for a first time. When it happens–it loads all the associated records for all records from the initial relation in a single query.
|
23
21
|
|
24
22
|
## Usage
|
25
23
|
|
26
|
-
|
24
|
+
Let's try `#lazy_preload` in action! The following code will perform a single SQL request (because we've never accessed posts):
|
27
25
|
|
28
26
|
```ruby
|
29
|
-
users = User.lazy_preload(:posts).limit(10)
|
27
|
+
users = User.lazy_preload(:posts).limit(10) # => SELECT * FROM users LIMIT 10
|
28
|
+
users.map(&:first_name)
|
30
29
|
```
|
31
30
|
|
32
|
-
|
31
|
+
However, when we try to load posts, there will be one more request for posts:
|
33
32
|
|
34
33
|
```ruby
|
35
|
-
users.map(&:
|
34
|
+
users.map(&:posts) # => SELECT * FROM posts WHERE user_id in (...)
|
36
35
|
```
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
```sql
|
41
|
-
SELECT * FROM users LIMIT 10
|
42
|
-
```
|
37
|
+
## Auto preloading
|
43
38
|
|
44
|
-
|
39
|
+
If you want the gem to be even lazier–you can configure it to load all the associations lazily without specifying them explicitly. To do that you'll need to change the configuration in the following way:
|
45
40
|
|
46
41
|
```ruby
|
47
|
-
|
42
|
+
ArLazyPreload.config.auto_preload = true
|
48
43
|
```
|
49
44
|
|
50
|
-
there
|
51
|
-
|
52
|
-
```sql
|
53
|
-
SELECT * FROM posts WHERE user_id in (...)
|
54
|
-
```
|
45
|
+
After that there is no need to call `#lazy_preload` on the association, everything would be loaded lazily.
|
55
46
|
|
56
|
-
##
|
47
|
+
## Installation
|
57
48
|
|
58
|
-
|
49
|
+
Add this line to your application's Gemfile, and you're all set:
|
59
50
|
|
60
51
|
```ruby
|
61
|
-
|
52
|
+
gem "ar_lazy_preload"
|
62
53
|
```
|
63
54
|
|
64
|
-
After that there is no need to call `lazy_preload` on the association, everything would be loaded lazily.
|
65
|
-
|
66
55
|
## License
|
56
|
+
|
67
57
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -13,3 +13,13 @@ if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
|
|
13
13
|
else
|
14
14
|
task default: [:rubocop, :spec]
|
15
15
|
end
|
16
|
+
|
17
|
+
task :bench do
|
18
|
+
cmd = %w[bundle exec ruby benchmark/main.rb]
|
19
|
+
exit system(*cmd)
|
20
|
+
end
|
21
|
+
|
22
|
+
task :memory do
|
23
|
+
cmd = %w[bundle exec ruby benchmark/memory.rb]
|
24
|
+
exit system(*cmd)
|
25
|
+
end
|
@@ -27,9 +27,9 @@ module ArLazyPreload
|
|
27
27
|
associated_records = parent_context.records.flat_map do |record|
|
28
28
|
next if record.nil?
|
29
29
|
|
30
|
-
record_association = record.
|
30
|
+
record_association = record.association(association_name)
|
31
31
|
reflection = reflection_cache[record.class]
|
32
|
-
reflection.collection? ? record_association.target : record_association
|
32
|
+
reflection.collection? ? record_association.target : record_association.reader
|
33
33
|
end
|
34
34
|
|
35
35
|
Context.register(records: associated_records, association_tree: child_association_tree)
|
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -195,8 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
195
|
- !ruby/object:Gem::Version
|
196
196
|
version: '0'
|
197
197
|
requirements: []
|
198
|
-
|
199
|
-
rubygems_version: 2.7.6
|
198
|
+
rubygems_version: 3.0.3
|
200
199
|
signing_key:
|
201
200
|
specification_version: 4
|
202
201
|
summary: lazy_preload implementation for ActiveRecord models
|