ar_lazy_preload 0.7.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -8
- data/lib/ar_lazy_preload/active_record/base.rb +11 -0
- data/lib/ar_lazy_preload/active_record/relation.rb +2 -4
- data/lib/ar_lazy_preload/context.rb +1 -1
- data/lib/ar_lazy_preload/contexts/base_context.rb +2 -5
- data/lib/ar_lazy_preload/preloader.rb +24 -0
- data/lib/ar_lazy_preload/railtie.rb +2 -0
- data/lib/ar_lazy_preload/version.rb +1 -1
- metadata +10 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c318927cb52d775d3a85e91fb24b7102e9fcb8004ced72fc1482ec0c7c4d1269
|
4
|
+
data.tar.gz: 8ceb003e63266df93da708925b261fab06334758366ce68b361609c2dbd07682
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d52b4b8293a9e0c2eaa47bf21b603d1066d7b18b738e724a3b16b20188a56326bd4a52d9397481ef81fcbec368e1929ff4679fe7cfb47ba3d704421eda2ad83
|
7
|
+
data.tar.gz: 716b102784e7c22119320d7cc5ae513dcb8f2097eb5dba31e50fbbe64940f96324eff7ed2410eea6171a899880b4fc81683d2b43233607ea12740693b8358b69
|
data/README.md
CHANGED
@@ -1,18 +1,12 @@
|
|
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://
|
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://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/rspec.yml/badge.svg)](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/rspec.yml) [![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) ![](https://ruby-gem-downloads-badge.herokuapp.com/ar_lazy_preload?type=total)
|
2
2
|
|
3
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.
|
4
4
|
|
5
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 [
|
6
|
+
- **Fast**. Take a look at [performance benchmark](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/bench.yml) and [memory benchmark](https://github.com/DmitryTsepelev/ar_lazy_preload/actions/workflows/memory.yml)
|
7
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
8
|
- **Auto-preload support**. If you don't want to specify the association list–set `ArLazyPreload.config.auto_preload` to `true`
|
9
9
|
|
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>
|
15
|
-
|
16
10
|
## Why should I use it?
|
17
11
|
|
18
12
|
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.
|
@@ -50,6 +44,8 @@ If you want to turn automatic preload off for a specific record, you can call `.
|
|
50
44
|
users.first.skip_preload.posts # => SELECT * FROM posts WHERE user_id = ?
|
51
45
|
```
|
52
46
|
|
47
|
+
> *Warning* : Using the `ArLazyPreload.config.auto_preload` feature makes ArLazyPreload try to preload *every* association target throughout your app, and in any other gem that makes association target calls. When enabling the setting in an existing app, you may find some edge cases where previous working queries now fail, and you should test most of your app paths to ensure that there are no such issues.
|
48
|
+
|
53
49
|
### Relation auto preloading
|
54
50
|
|
55
51
|
Another alternative for auto preloading is using relation `#preload_associations_lazily` method
|
@@ -81,6 +77,10 @@ Add this line to your application's Gemfile, and you're all set:
|
|
81
77
|
gem "ar_lazy_preload"
|
82
78
|
```
|
83
79
|
|
80
|
+
## Credits
|
81
|
+
|
82
|
+
Initially sponsored by [Evil Martians](http://evilmartians.com).
|
83
|
+
|
84
84
|
## License
|
85
85
|
|
86
86
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -6,16 +6,27 @@ module ArLazyPreload
|
|
6
6
|
def self.included(base)
|
7
7
|
base.class.delegate :lazy_preload, to: :all
|
8
8
|
base.class.delegate :preload_associations_lazily, to: :all
|
9
|
+
base.after_create { try_setup_auto_preload_context }
|
9
10
|
end
|
10
11
|
|
11
12
|
attr_accessor :lazy_preload_context
|
12
13
|
|
13
14
|
delegate :try_preload_lazily, to: :lazy_preload_context, allow_nil: true
|
14
15
|
|
16
|
+
def reload
|
17
|
+
super.tap { try_setup_auto_preload_context }
|
18
|
+
end
|
19
|
+
|
15
20
|
def skip_preload
|
16
21
|
lazy_preload_context&.records&.delete(self)
|
17
22
|
self.lazy_preload_context = nil
|
18
23
|
self
|
19
24
|
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def try_setup_auto_preload_context
|
29
|
+
ArLazyPreload::Context.register(records: [self]) if ArLazyPreload.config.auto_preload?
|
30
|
+
end
|
20
31
|
end
|
21
32
|
end
|
@@ -12,11 +12,9 @@ module ArLazyPreload
|
|
12
12
|
def preload_associations(records)
|
13
13
|
preload = preload_values
|
14
14
|
preload += includes_values unless eager_loading?
|
15
|
-
|
15
|
+
|
16
16
|
preload.each do |associations|
|
17
|
-
|
18
|
-
preloader_associations = preloader.preload records, associations
|
19
|
-
preloader_associations.each do |preloader_association|
|
17
|
+
ArLazyPreload::Preloader.new(records, associations).preload.each do |preloader_association|
|
20
18
|
handle_preloaded_records(preloader_association.preloaded_records)
|
21
19
|
end
|
22
20
|
end
|
@@ -8,7 +8,7 @@ require "ar_lazy_preload/contexts/temporary_preload_config"
|
|
8
8
|
module ArLazyPreload
|
9
9
|
class Context
|
10
10
|
# Initiates lazy preload context for given records
|
11
|
-
def self.register(records:, association_tree
|
11
|
+
def self.register(records:, association_tree: nil, auto_preload: false)
|
12
12
|
return if records.empty?
|
13
13
|
|
14
14
|
if ArLazyPreload.config.auto_preload? || auto_preload
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "set"
|
4
4
|
require "ar_lazy_preload/associated_context_builder"
|
5
|
+
require "ar_lazy_preload/preloader"
|
5
6
|
|
6
7
|
module ArLazyPreload
|
7
8
|
module Contexts
|
@@ -63,7 +64,7 @@ module ArLazyPreload
|
|
63
64
|
# help of the TemporaryPreloadConfig
|
64
65
|
def preload_records(association_name, records)
|
65
66
|
TemporaryPreloadConfig.within_context do
|
66
|
-
|
67
|
+
ArLazyPreload::Preloader.new(records, [association_name]).preload
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
@@ -75,10 +76,6 @@ module ArLazyPreload
|
|
75
76
|
@loaded_association_names ||= Set.new
|
76
77
|
end
|
77
78
|
|
78
|
-
def preloader
|
79
|
-
@preloader ||= ActiveRecord::Associations::Preloader.new
|
80
|
-
end
|
81
|
-
|
82
79
|
def preloadable_record?(association_name, record)
|
83
80
|
preloadable_reflections_cache.dig(record.class, association_name)
|
84
81
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ArLazyPreload
|
4
|
+
class Preloader
|
5
|
+
def initialize(records, associations)
|
6
|
+
@records = records
|
7
|
+
@associations = associations
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def patch_for_rails_7!
|
12
|
+
define_method(:preload) do
|
13
|
+
ActiveRecord::Associations::Preloader.new(
|
14
|
+
records: @records, associations: @associations
|
15
|
+
).call
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def preload
|
21
|
+
ActiveRecord::Associations::Preloader.new.preload(@records, @associations)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -25,6 +25,8 @@ module ArLazyPreload
|
|
25
25
|
|
26
26
|
ActiveRecord::Associations::CollectionAssociation.prepend(CollectionAssociation)
|
27
27
|
ActiveRecord::Associations::CollectionProxy.prepend(CollectionProxy)
|
28
|
+
|
29
|
+
ArLazyPreload::Preloader.patch_for_rails_7! if ActiveRecord::VERSION::MAJOR >= 7
|
28
30
|
end
|
29
31
|
end
|
30
32
|
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:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -205,13 +205,14 @@ files:
|
|
205
205
|
- lib/ar_lazy_preload/contexts/lazy_preload_context.rb
|
206
206
|
- lib/ar_lazy_preload/contexts/temporary_preload_config.rb
|
207
207
|
- lib/ar_lazy_preload/preloaded_records_converter.rb
|
208
|
+
- lib/ar_lazy_preload/preloader.rb
|
208
209
|
- lib/ar_lazy_preload/railtie.rb
|
209
210
|
- lib/ar_lazy_preload/version.rb
|
210
211
|
homepage: https://github.com/DmitryTsepelev/ar_lazy_preload
|
211
212
|
licenses:
|
212
213
|
- MIT
|
213
214
|
metadata: {}
|
214
|
-
post_install_message:
|
215
|
+
post_install_message:
|
215
216
|
rdoc_options: []
|
216
217
|
require_paths:
|
217
218
|
- lib
|
@@ -219,15 +220,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
219
220
|
requirements:
|
220
221
|
- - ">="
|
221
222
|
- !ruby/object:Gem::Version
|
222
|
-
version: '2.
|
223
|
+
version: '2.6'
|
223
224
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
224
225
|
requirements:
|
225
226
|
- - ">="
|
226
227
|
- !ruby/object:Gem::Version
|
227
228
|
version: '0'
|
228
229
|
requirements: []
|
229
|
-
rubygems_version: 3.1.
|
230
|
-
signing_key:
|
230
|
+
rubygems_version: 3.1.6
|
231
|
+
signing_key:
|
231
232
|
specification_version: 4
|
232
233
|
summary: lazy_preload implementation for ActiveRecord models
|
233
234
|
test_files: []
|