ar_lazy_preload 1.1.2 → 2.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 +7 -0
- data/lib/ar_lazy_preload/contexts/base_context.rb +1 -1
- data/lib/ar_lazy_preload/railtie.rb +1 -23
- data/lib/ar_lazy_preload/version.rb +1 -1
- data/lib/ar_lazy_preload.rb +25 -1
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82d5aeed661b9ab42f1d15f78cb8db84cc8b426ddfe26ae1442f297800ec0707
|
4
|
+
data.tar.gz: 1c89c0b2f8025143186551e06629c40a37bf8c207b7605c71481b02c158f89f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acbedff3a73e2b26ee0a905c9b5059e9e27297abccd2a16c0a68c3bd67001a4d45721b83aaad21738b36d6c7bce8d41ea6756bd0f1bd54973f339ab2cc26ea68
|
7
|
+
data.tar.gz: 5285e4116f8ba49bc7aa2cbfc1962bfff112785ebd7dbed823ebadf606e9eef1f0772f7f30312344a00cbe8c76af37fc0378d422933073d8758bebe5ae6bcd85
|
data/README.md
CHANGED
@@ -7,6 +7,12 @@
|
|
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
|
+
Used in production by:
|
11
|
+
|
12
|
+
- [Fund that flip](https://evilmartians.com/chronicles/big-refactoring-fix-that-app-for-fund-that-flip)
|
13
|
+
- Toptal
|
14
|
+
- _Want to be here? Let me know_ 🙂
|
15
|
+
|
10
16
|
## Why should I use it?
|
11
17
|
|
12
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.
|
@@ -68,6 +74,7 @@ posts = User.preload_associations_lazily.flat_map(&:posts)
|
|
68
74
|
|
69
75
|
2. When `#size` is called on association (e.g., `User.lazy_preload(:posts).map { |u| u.posts.size }`), lazy preloading won't happen, because `#size` method performs `SELECT COUNT()` database request instead of loading the association when association haven't been loaded yet ([here](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/42) is the issue, and [here](https://blazarblogs.wordpress.com/2019/07/27/activerecord-size-vs-count-vs-length/) is the explanation article about `size`, `length` and `count`).
|
70
76
|
|
77
|
+
3. Lazy preloading for **ActiveStorage** variants is not working automatically because of the way how it is implemented ([here](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/70) is the issue). You can preload them manually by calling `#with_all_variant_records`/`#with_attached_#{attachment_name}` on association. Example: `User.with_attached_avatar.first(10).map { |u| u.avatar.variant(:small).processed.url }`
|
71
78
|
|
72
79
|
## Installation
|
73
80
|
|
@@ -92,7 +92,7 @@ module ArLazyPreload
|
|
92
92
|
|
93
93
|
def preloadable_reflection?(klass, reflection)
|
94
94
|
scope = reflection.scope
|
95
|
-
preloadable_scope = scope&.arity&.zero?
|
95
|
+
preloadable_scope = scope&.arity&.zero? || ::ActiveRecord::VERSION::MAJOR >= 7
|
96
96
|
through_reflection =
|
97
97
|
reflection.options[:through] && klass.reflect_on_association(reflection.options[:through])
|
98
98
|
preloadable_through_reflection =
|
@@ -1,32 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "ar_lazy_preload/active_record/base"
|
4
|
-
require "ar_lazy_preload/active_record/relation"
|
5
|
-
require "ar_lazy_preload/active_record/association"
|
6
|
-
require "ar_lazy_preload/active_record/collection_association"
|
7
|
-
require "ar_lazy_preload/active_record/merger"
|
8
|
-
require "ar_lazy_preload/active_record/association_relation"
|
9
|
-
require "ar_lazy_preload/active_record/collection_proxy"
|
10
|
-
|
11
3
|
module ArLazyPreload
|
12
4
|
class Railtie < Rails::Railtie
|
13
5
|
config.to_prepare do |_app|
|
14
6
|
ActiveSupport.on_load(:active_record) do
|
15
|
-
|
16
|
-
|
17
|
-
ActiveRecord::Relation.prepend(Relation)
|
18
|
-
ActiveRecord::AssociationRelation.prepend(AssociationRelation)
|
19
|
-
ActiveRecord::Relation::Merger.prepend(Merger)
|
20
|
-
|
21
|
-
[
|
22
|
-
ActiveRecord::Associations::CollectionAssociation,
|
23
|
-
ActiveRecord::Associations::Association
|
24
|
-
].each { |klass| klass.prepend(Association) }
|
25
|
-
|
26
|
-
ActiveRecord::Associations::CollectionAssociation.prepend(CollectionAssociation)
|
27
|
-
ActiveRecord::Associations::CollectionProxy.prepend(CollectionProxy)
|
28
|
-
|
29
|
-
ArLazyPreload::Preloader.patch_for_rails_7! if ActiveRecord::VERSION::MAJOR >= 7
|
7
|
+
ArLazyPreload.install_hooks
|
30
8
|
end
|
31
9
|
end
|
32
10
|
end
|
data/lib/ar_lazy_preload.rb
CHANGED
@@ -1,12 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "ar_lazy_preload/configuration"
|
4
|
-
require "ar_lazy_preload/railtie"
|
4
|
+
require "ar_lazy_preload/railtie" if defined?(::Rails)
|
5
|
+
|
6
|
+
require "ar_lazy_preload/active_record/base"
|
7
|
+
require "ar_lazy_preload/active_record/relation"
|
8
|
+
require "ar_lazy_preload/active_record/association"
|
9
|
+
require "ar_lazy_preload/active_record/collection_association"
|
10
|
+
require "ar_lazy_preload/active_record/merger"
|
11
|
+
require "ar_lazy_preload/active_record/association_relation"
|
12
|
+
require "ar_lazy_preload/active_record/collection_proxy"
|
5
13
|
|
6
14
|
module ArLazyPreload
|
7
15
|
class << self
|
8
16
|
def config
|
9
17
|
@config ||= Configuration.new
|
10
18
|
end
|
19
|
+
|
20
|
+
def install_hooks
|
21
|
+
ActiveRecord::Base.include(Base)
|
22
|
+
|
23
|
+
ActiveRecord::Relation.prepend(Relation)
|
24
|
+
ActiveRecord::AssociationRelation.prepend(AssociationRelation)
|
25
|
+
ActiveRecord::Relation::Merger.prepend(Merger)
|
26
|
+
|
27
|
+
ActiveRecord::Associations::CollectionAssociation.prepend(Association)
|
28
|
+
ActiveRecord::Associations::Association.prepend(Association)
|
29
|
+
|
30
|
+
ActiveRecord::Associations::CollectionAssociation.prepend(CollectionAssociation)
|
31
|
+
ActiveRecord::Associations::CollectionProxy.prepend(CollectionProxy)
|
32
|
+
|
33
|
+
ArLazyPreload::Preloader.patch_for_rails_7! if ActiveRecord::VERSION::MAJOR >= 7
|
34
|
+
end
|
11
35
|
end
|
12
36
|
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: 2.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: 2024-03-02 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: '
|
20
|
-
type: :
|
19
|
+
version: '6.1'
|
20
|
+
type: :development
|
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: '
|
26
|
+
version: '6.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - '='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.57.2
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 1.57.2
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: db-query-matchers
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -212,7 +212,7 @@ homepage: https://github.com/DmitryTsepelev/ar_lazy_preload
|
|
212
212
|
licenses:
|
213
213
|
- MIT
|
214
214
|
metadata: {}
|
215
|
-
post_install_message:
|
215
|
+
post_install_message:
|
216
216
|
rdoc_options: []
|
217
217
|
require_paths:
|
218
218
|
- lib
|
@@ -220,15 +220,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
220
220
|
requirements:
|
221
221
|
- - ">="
|
222
222
|
- !ruby/object:Gem::Version
|
223
|
-
version: '
|
223
|
+
version: '3.0'
|
224
224
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
225
225
|
requirements:
|
226
226
|
- - ">="
|
227
227
|
- !ruby/object:Gem::Version
|
228
228
|
version: '0'
|
229
229
|
requirements: []
|
230
|
-
rubygems_version: 3.
|
231
|
-
signing_key:
|
230
|
+
rubygems_version: 3.4.10
|
231
|
+
signing_key:
|
232
232
|
specification_version: 4
|
233
233
|
summary: lazy_preload implementation for ActiveRecord models
|
234
234
|
test_files: []
|