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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a265f11a7c21888ebe4ac60ccce4ab42bf56ff8527217fa8946013df0c2b0324
4
- data.tar.gz: ce5ed84890259cff5d994a6618fc8017af7e46dc6acb974ef44ec5cc183e494f
3
+ metadata.gz: 0e0790a19fd33c7b141ba62725951c084f84cd0d1e8678e807081dac6d1b9c4c
4
+ data.tar.gz: 714b3e6785f5ad4dd7278be3549638ec8ab2447cad82cc3f9a5cd53694d23006
5
5
  SHA512:
6
- metadata.gz: 0e7220676078a6a8fb0ed8b93e7eeb81260d20ce692177466dfa97e6ee8915fd56bd524fbac81b039f48ef6ba43023d9838235d6540f0518de73053395ca7ae1
7
- data.tar.gz: 35280832594c531085e7d51047282852615029d4b019532cf00e098c069f85f98a4bfa9cc85f54866baa039c9124346420aa989919161a465d53dc2a873db473
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
- # ArLazyPreload
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
- <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>
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
- 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
+ <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
- 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.
16
+ ## Why should I use it?
15
17
 
16
- ## Installation
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
- ```ruby
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
- For example, if we define the following relation
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
- and use it in the following way
31
+ However, when we try to load posts, there will be one more request for posts:
33
32
 
34
33
  ```ruby
35
- users.map(&:first_name)
34
+ users.map(&:posts) # => SELECT * FROM posts WHERE user_id in (...)
36
35
  ```
37
36
 
38
- there will be one query because we've never accessed posts:
39
-
40
- ```sql
41
- SELECT * FROM users LIMIT 10
42
- ```
37
+ ## Auto preloading
43
38
 
44
- However, when we try to load posts
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
- users.map(&:posts)
42
+ ArLazyPreload.config.auto_preload = true
48
43
  ```
49
44
 
50
- there will be one more request for posts:
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
- ## Auto preloading
47
+ ## Installation
57
48
 
58
- If you want the gem to be even more lazy - you can configure it to load all the associations lazily without specifying them explicitly. In order to do that you'll need to change the configuration in the following way:
49
+ Add this line to your application's Gemfile, and you're all set:
59
50
 
60
51
  ```ruby
61
- ArLazyPreload.config.auto_preload = true
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.public_send(association_name)
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArLazyPreload
4
- VERSION = "0.2.6"
4
+ VERSION = "0.2.7"
5
5
  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.6
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: 2018-12-04 00:00:00.000000000 Z
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
- rubyforge_project:
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