ar_lazy_preload 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/ar_lazy_preload/active_record/relation.rb +29 -0
- data/lib/ar_lazy_preload/context.rb +1 -0
- data/lib/ar_lazy_preload/contexts/base_context.rb +11 -1
- data/lib/ar_lazy_preload/contexts/temporary_preload_config.rb +23 -0
- data/lib/ar_lazy_preload/preloaded_records_converter.rb +21 -0
- data/lib/ar_lazy_preload/version.rb +1 -1
- metadata +24 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46e7c82ea00d2d4f0da30a522644d91b54f277a7e915d576c7e7fd959f5f3a12
|
4
|
+
data.tar.gz: c255f0fd18c255dc42bf4d321c598d4967960e5e4cb0370385e00b6b7b5963ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe5b062187652784cfd99ff7c201906400247204d77b91742d2350bf487f9542415f360808d6ad2dcc78f7f56aa496c1064781cebfd4122d6b6c1e706e079769
|
7
|
+
data.tar.gz: 7cc876e17f94ac05518654b9483a6a70c62b37e4226653ed323435aefa3d4544c6b722e82f3ecdba0af2b96513044d4ee02863d4b4b270d12579a6f3b7405ed8
|
data/README.md
CHANGED
@@ -62,7 +62,7 @@ posts = User.preload_associations_lazily.flat_map(&:posts)
|
|
62
62
|
|
63
63
|
## Gotchas
|
64
64
|
|
65
|
-
1. Lazy preloading [does not work](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/40/files) when `.includes` is called earlier:
|
65
|
+
1. Lazy preloading [does not work](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/40/files) for ActiveRecord < 6 when `.includes` is called earlier:
|
66
66
|
|
67
67
|
```ruby
|
68
68
|
Post.includes(:user).preload_associations_lazily.each do |p|
|
@@ -1,12 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "ar_lazy_preload/context"
|
4
|
+
require "ar_lazy_preload/contexts/temporary_preload_config"
|
5
|
+
require "ar_lazy_preload/preloaded_records_converter"
|
4
6
|
|
5
7
|
module ArLazyPreload
|
6
8
|
# ActiveRecord::Relation patch with lazy preloading support
|
7
9
|
module Relation
|
8
10
|
attr_writer :preloads_associations_lazily
|
9
11
|
|
12
|
+
def preload_associations(records)
|
13
|
+
preload = preload_values
|
14
|
+
preload += includes_values unless eager_loading?
|
15
|
+
preloader = nil
|
16
|
+
preload.each do |associations|
|
17
|
+
preloader ||= build_preloader
|
18
|
+
preloader_associations = preloader.preload records, associations
|
19
|
+
preloader_associations.each do |preloader_association|
|
20
|
+
handle_preloaded_records(preloader_association.preloaded_records)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
10
25
|
# Enhanced #load method will check if association has not been loaded yet and add a context
|
11
26
|
# for lazy preloading to loaded each record
|
12
27
|
def load
|
@@ -75,6 +90,20 @@ module ArLazyPreload
|
|
75
90
|
@preloads_associations_lazily ||= false
|
76
91
|
end
|
77
92
|
|
93
|
+
def handle_preloaded_records(preloaded_records)
|
94
|
+
return unless Contexts::TemporaryPreloadConfig.enabled? || preloads_associations_lazily?
|
95
|
+
|
96
|
+
records_array = PreloadedRecordsConverter.call(preloaded_records)
|
97
|
+
|
98
|
+
return unless records_array&.any?
|
99
|
+
|
100
|
+
Context.register(
|
101
|
+
records: records_array,
|
102
|
+
association_tree: lazy_preload_values,
|
103
|
+
auto_preload: true
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
78
107
|
attr_writer :lazy_preload_values
|
79
108
|
end
|
80
109
|
end
|
@@ -47,8 +47,8 @@ module ArLazyPreload
|
|
47
47
|
filtered_records = records.select do |record|
|
48
48
|
reflection_names_cache[record.class].include?(association_name)
|
49
49
|
end
|
50
|
-
preloader.preload(filtered_records, association_name)
|
51
50
|
|
51
|
+
preload_records(association_name, filtered_records)
|
52
52
|
loaded_association_names.add(association_name)
|
53
53
|
|
54
54
|
AssociatedContextBuilder.prepare(
|
@@ -57,6 +57,16 @@ module ArLazyPreload
|
|
57
57
|
)
|
58
58
|
end
|
59
59
|
|
60
|
+
# Method preloads associations for the specific sets of the records
|
61
|
+
# and provides automatically provides context for the records
|
62
|
+
# loaded using `includes` inside Relation#preload_associations with the
|
63
|
+
# help of the TemporaryPreloadConfig
|
64
|
+
def preload_records(association_name, records)
|
65
|
+
TemporaryPreloadConfig.within_context do
|
66
|
+
preloader.preload(records, association_name)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
60
70
|
def association_loaded?(association_name)
|
61
71
|
loaded_association_names.include?(association_name)
|
62
72
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ArLazyPreload
|
4
|
+
module Contexts
|
5
|
+
# Preload config that used to enable preloading only for specfic part of the application
|
6
|
+
class TemporaryPreloadConfig
|
7
|
+
THREAD_KEY = "temporary_preload_context_enabled"
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def enabled?
|
11
|
+
Thread.current[THREAD_KEY] == true
|
12
|
+
end
|
13
|
+
|
14
|
+
def within_context
|
15
|
+
Thread.current[THREAD_KEY] = true
|
16
|
+
yield
|
17
|
+
ensure
|
18
|
+
Thread.current[THREAD_KEY] = nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ArLazyPreload
|
4
|
+
class PreloadedRecordsConverter
|
5
|
+
# For different versions of rails we have different records class
|
6
|
+
# for ~> 6.1.0 it returns plain array
|
7
|
+
# for ~> 6.0.0 it returns ActiveRecord::Relation
|
8
|
+
def self.call(preloaded_records)
|
9
|
+
case preloaded_records
|
10
|
+
when Array
|
11
|
+
preloaded_records
|
12
|
+
when ::ActiveRecord::Relation
|
13
|
+
raise(ArgumentError, "The relation is not preloaded") unless preloaded_records.loaded?
|
14
|
+
|
15
|
+
preloaded_records.to_a
|
16
|
+
else
|
17
|
+
raise(ArgumentError, "Unsupported class for preloaded records")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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.6.
|
4
|
+
version: 0.6.1
|
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: 2021-03-04 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: '
|
19
|
+
version: '5.0'
|
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: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,6 +164,20 @@ dependencies:
|
|
164
164
|
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: pry
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
167
181
|
description: lazy_preload implementation for ActiveRecord models
|
168
182
|
email:
|
169
183
|
- dmitry.a.tsepelev@gmail.com
|
@@ -189,13 +203,15 @@ files:
|
|
189
203
|
- lib/ar_lazy_preload/contexts/auto_preload_context.rb
|
190
204
|
- lib/ar_lazy_preload/contexts/base_context.rb
|
191
205
|
- lib/ar_lazy_preload/contexts/lazy_preload_context.rb
|
206
|
+
- lib/ar_lazy_preload/contexts/temporary_preload_config.rb
|
207
|
+
- lib/ar_lazy_preload/preloaded_records_converter.rb
|
192
208
|
- lib/ar_lazy_preload/railtie.rb
|
193
209
|
- lib/ar_lazy_preload/version.rb
|
194
210
|
homepage: https://github.com/DmitryTsepelev/ar_lazy_preload
|
195
211
|
licenses:
|
196
212
|
- MIT
|
197
213
|
metadata: {}
|
198
|
-
post_install_message:
|
214
|
+
post_install_message:
|
199
215
|
rdoc_options: []
|
200
216
|
require_paths:
|
201
217
|
- lib
|
@@ -210,8 +226,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
210
226
|
- !ruby/object:Gem::Version
|
211
227
|
version: '0'
|
212
228
|
requirements: []
|
213
|
-
rubygems_version: 3.
|
214
|
-
signing_key:
|
229
|
+
rubygems_version: 3.1.1
|
230
|
+
signing_key:
|
215
231
|
specification_version: 4
|
216
232
|
summary: lazy_preload implementation for ActiveRecord models
|
217
233
|
test_files: []
|