jit_preloader 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/jit_preloader.gemspec +1 -1
- data/lib/jit_preloader.rb +5 -4
- data/lib/jit_preloader/active_record/associations/preloader/{association.rb → ar5_association.rb} +0 -0
- data/lib/jit_preloader/active_record/associations/preloader/ar6_association.rb +69 -0
- data/lib/jit_preloader/version.rb +1 -1
- metadata +6 -6
- data/Gemfile.lock +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed5a725f8d7f312a774f6a1a3c58e0f1345027d4
|
4
|
+
data.tar.gz: 9d0ef45b43032043aee85d162343bd51783a94e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5db4eb1d7b2fc74a2c7a0e8e0e7238b88e6270495f0b8cca71ed31de85da9ced40092186ab1dc188b25dc759d40a0d014288cc1f161534baf48943a684cf906a
|
7
|
+
data.tar.gz: 5ccd2555def314a25a8a022f2aac717e5114ac65a5b32a6fc31c9ffdf176d7db54ff8834ca01962bc037b09bda0a6182204478e387b457c081b03542a9321aa6
|
data/.gitignore
CHANGED
data/jit_preloader.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activerecord", "> 4.2", "<
|
21
|
+
spec.add_dependency "activerecord", "> 4.2", "< 7"
|
22
22
|
spec.add_dependency "activesupport"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler"
|
data/lib/jit_preloader.rb
CHANGED
@@ -8,16 +8,17 @@ require 'jit_preloader/active_record/base'
|
|
8
8
|
require 'jit_preloader/active_record/relation'
|
9
9
|
require 'jit_preloader/active_record/associations/collection_association'
|
10
10
|
require 'jit_preloader/active_record/associations/singular_association'
|
11
|
-
if Gem::Version.new(ActiveRecord::VERSION::STRING)
|
11
|
+
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("6.0.0")
|
12
|
+
require 'jit_preloader/active_record/associations/preloader/ar6_association'
|
13
|
+
elsif Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5.2.2")
|
14
|
+
require 'jit_preloader/active_record/associations/preloader/ar5_association'
|
15
|
+
else
|
12
16
|
require 'jit_preloader/active_record/associations/preloader/collection_association'
|
13
17
|
require 'jit_preloader/active_record/associations/preloader/singular_association'
|
14
|
-
else
|
15
|
-
require 'jit_preloader/active_record/associations/preloader/association'
|
16
18
|
end
|
17
19
|
require 'jit_preloader/preloader'
|
18
20
|
|
19
21
|
module JitPreloader
|
20
|
-
|
21
22
|
def self.globally_enabled=(value)
|
22
23
|
@enabled = value
|
23
24
|
end
|
data/lib/jit_preloader/active_record/associations/preloader/{association.rb → ar5_association.rb}
RENAMED
File without changes
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module JitPreloader
|
2
|
+
module PreloaderAssociation
|
3
|
+
|
4
|
+
# A monkey patch to ActiveRecord. The old method looked like the snippet
|
5
|
+
# below. Our changes here are that we remove records that are already
|
6
|
+
# part of the target, then attach all of the records to a new jit preloader.
|
7
|
+
#
|
8
|
+
# def run
|
9
|
+
# if !preload_scope || preload_scope.empty_scope?
|
10
|
+
# owners.each do |owner|
|
11
|
+
# associate_records_to_owner(owner, records_by_owner[owner] || [])
|
12
|
+
# end
|
13
|
+
# else
|
14
|
+
# # Custom preload scope is used and
|
15
|
+
# # the association can not be marked as loaded
|
16
|
+
# # Loading into a Hash instead
|
17
|
+
# records_by_owner
|
18
|
+
# end
|
19
|
+
# self
|
20
|
+
# end
|
21
|
+
def run
|
22
|
+
all_records = []
|
23
|
+
|
24
|
+
owners.each do |owner|
|
25
|
+
owned_records = records_by_owner[owner] || []
|
26
|
+
all_records.concat(Array(owned_records)) if owner.jit_preloader || JitPreloader.globally_enabled?
|
27
|
+
associate_records_to_owner(owner, owned_records)
|
28
|
+
end
|
29
|
+
|
30
|
+
JitPreloader::Preloader.attach(all_records) if all_records.any?
|
31
|
+
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
# Original method:
|
36
|
+
# def associate_records_to_owner(owner, records)
|
37
|
+
# association = owner.association(reflection.name)
|
38
|
+
# association.loaded!
|
39
|
+
# if reflection.collection?
|
40
|
+
# association.target.concat(records)
|
41
|
+
# else
|
42
|
+
# association.target = records.first unless records.empty?
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
def associate_records_to_owner(owner, records)
|
46
|
+
association = owner.association(reflection.name)
|
47
|
+
association.loaded!
|
48
|
+
|
49
|
+
if reflection.collection?
|
50
|
+
# It is possible that some of the records are loaded already.
|
51
|
+
# We don't want to duplicate them, but we also want to preserve
|
52
|
+
# the original copy so that we don't blow away in-memory changes.
|
53
|
+
new_records = association.target.any? ? records - association.target : records
|
54
|
+
association.target.concat(new_records)
|
55
|
+
else
|
56
|
+
association.target ||= records.first unless records.empty?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def build_scope
|
62
|
+
super.tap do |scope|
|
63
|
+
scope.jit_preload! if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
ActiveRecord::Associations::Preloader::Association.prepend(JitPreloader::PreloaderAssociation)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jit_preloader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle d'Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '4.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: activesupport
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,14 +154,14 @@ files:
|
|
154
154
|
- ".gitignore"
|
155
155
|
- ".rspec"
|
156
156
|
- Gemfile
|
157
|
-
- Gemfile.lock
|
158
157
|
- LICENSE
|
159
158
|
- README.md
|
160
159
|
- Rakefile
|
161
160
|
- jit_preloader.gemspec
|
162
161
|
- lib/jit_preloader.rb
|
163
162
|
- lib/jit_preloader/active_record/associations/collection_association.rb
|
164
|
-
- lib/jit_preloader/active_record/associations/preloader/
|
163
|
+
- lib/jit_preloader/active_record/associations/preloader/ar5_association.rb
|
164
|
+
- lib/jit_preloader/active_record/associations/preloader/ar6_association.rb
|
165
165
|
- lib/jit_preloader/active_record/associations/preloader/collection_association.rb
|
166
166
|
- lib/jit_preloader/active_record/associations/preloader/singular_association.rb
|
167
167
|
- lib/jit_preloader/active_record/associations/singular_association.rb
|
data/Gemfile.lock
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
jit_preloader (0.2.5)
|
5
|
-
activerecord (> 4.2, < 6)
|
6
|
-
activesupport
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: https://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activemodel (5.2.4.2)
|
12
|
-
activesupport (= 5.2.4.2)
|
13
|
-
activerecord (5.2.4.2)
|
14
|
-
activemodel (= 5.2.4.2)
|
15
|
-
activesupport (= 5.2.4.2)
|
16
|
-
arel (>= 9.0)
|
17
|
-
activesupport (5.2.4.2)
|
18
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
-
i18n (>= 0.7, < 2)
|
20
|
-
minitest (~> 5.1)
|
21
|
-
tzinfo (~> 1.1)
|
22
|
-
arel (9.0.0)
|
23
|
-
byebug (9.0.6)
|
24
|
-
concurrent-ruby (1.1.6)
|
25
|
-
database_cleaner (1.5.3)
|
26
|
-
db-query-matchers (0.10.0)
|
27
|
-
activesupport (>= 4.0, < 7)
|
28
|
-
rspec (~> 3.0)
|
29
|
-
diff-lcs (1.2.5)
|
30
|
-
i18n (1.8.2)
|
31
|
-
concurrent-ruby (~> 1.0)
|
32
|
-
minitest (5.14.0)
|
33
|
-
rake (13.0.1)
|
34
|
-
rspec (3.5.0)
|
35
|
-
rspec-core (~> 3.5.0)
|
36
|
-
rspec-expectations (~> 3.5.0)
|
37
|
-
rspec-mocks (~> 3.5.0)
|
38
|
-
rspec-core (3.5.4)
|
39
|
-
rspec-support (~> 3.5.0)
|
40
|
-
rspec-expectations (3.5.0)
|
41
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
-
rspec-support (~> 3.5.0)
|
43
|
-
rspec-mocks (3.5.0)
|
44
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
-
rspec-support (~> 3.5.0)
|
46
|
-
rspec-support (3.5.0)
|
47
|
-
sqlite3 (1.3.12)
|
48
|
-
thread_safe (0.3.6)
|
49
|
-
tzinfo (1.2.6)
|
50
|
-
thread_safe (~> 0.1)
|
51
|
-
|
52
|
-
PLATFORMS
|
53
|
-
ruby
|
54
|
-
|
55
|
-
DEPENDENCIES
|
56
|
-
bundler
|
57
|
-
byebug
|
58
|
-
database_cleaner
|
59
|
-
db-query-matchers
|
60
|
-
jit_preloader!
|
61
|
-
rake (~> 13.0)
|
62
|
-
rspec
|
63
|
-
sqlite3
|
64
|
-
|
65
|
-
BUNDLED WITH
|
66
|
-
2.1.4
|