jit_preloader 0.2.1 → 0.2.2
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2439886049ccbcd7845882f8fa47524e500ffc96d8b9620711288853c47c131
|
4
|
+
data.tar.gz: 189adc93418cf777c6eb33617934d92426cfbdc105a05a10c639f9dd387736f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 932327300aea76f21906abf912ba85d655fbeb04b6cf940c1c650e21d39a72682d4fb55d529c389897ed7d5b45d9a41cec411a5c7eb932f2216df03b91fef47e
|
7
|
+
data.tar.gz: 591bc82f9d763b963a78d6869da2f6346c7c3d49b044e48d0a12529fd61c94b104c3e0cc8e7e08a8a8fd9f4feee4ce73e66fbc72cf36ef11477852949d012802
|
@@ -1,74 +1,70 @@
|
|
1
1
|
module JitPreloadExtension
|
2
|
+
attr_accessor :jit_preloader
|
3
|
+
attr_accessor :jit_n_plus_one_tracking
|
4
|
+
attr_accessor :jit_preload_aggregates
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
attr_accessor :jit_n_plus_one_tracking
|
8
|
-
attr_accessor :jit_preload_aggregates
|
9
|
-
|
10
|
-
def reload(*args)
|
11
|
-
clear_jit_preloader!
|
12
|
-
super
|
13
|
-
end
|
6
|
+
def reload(*args)
|
7
|
+
clear_jit_preloader!
|
8
|
+
super
|
9
|
+
end
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
11
|
+
def clear_jit_preloader!
|
12
|
+
self.jit_preload_aggregates = {}
|
13
|
+
if jit_preloader
|
14
|
+
jit_preloader.records.delete(self)
|
15
|
+
self.jit_preloader = nil
|
21
16
|
end
|
22
|
-
|
23
17
|
end
|
24
18
|
|
25
|
-
|
26
|
-
|
19
|
+
def self.prepended(base)
|
20
|
+
class << base
|
21
|
+
delegate :jit_preload, to: :all
|
27
22
|
|
28
|
-
|
29
|
-
|
23
|
+
def has_many_aggregate(assoc, name, aggregate, field, default: 0)
|
24
|
+
method_name = "#{assoc}_#{name}"
|
30
25
|
|
31
|
-
|
32
|
-
|
26
|
+
define_method(method_name) do |conditions={}|
|
27
|
+
self.jit_preload_aggregates ||= {}
|
33
28
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
29
|
+
key = "#{method_name}|#{conditions.sort.hash}"
|
30
|
+
return jit_preload_aggregates[key] if jit_preload_aggregates.key?(key)
|
31
|
+
if jit_preloader
|
32
|
+
reflection = association(assoc).reflection
|
33
|
+
primary_ids = jit_preloader.records.collect{|r| r[reflection.active_record_primary_key] }
|
34
|
+
klass = reflection.klass
|
40
35
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
aggregate_association = reflection
|
37
|
+
while aggregate_association.through_reflection
|
38
|
+
aggregate_association = aggregate_association.through_reflection
|
39
|
+
end
|
45
40
|
|
46
|
-
|
47
|
-
|
41
|
+
association_scope = klass.all.merge(association(assoc).scope).unscope(where: aggregate_association.foreign_key)
|
42
|
+
association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope
|
48
43
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
44
|
+
conditions[aggregate_association.table_name] = { aggregate_association.foreign_key => primary_ids }
|
45
|
+
if reflection.type.present?
|
46
|
+
conditions[reflection.type] = self.class.name
|
47
|
+
end
|
48
|
+
group_by = "#{aggregate_association.table_name}.#{aggregate_association.foreign_key}"
|
54
49
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
50
|
+
preloaded_data = Hash[association_scope
|
51
|
+
.where(conditions)
|
52
|
+
.group(group_by)
|
53
|
+
.send(aggregate, field)
|
54
|
+
]
|
60
55
|
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
jit_preloader.records.each do |record|
|
57
|
+
record.jit_preload_aggregates ||= {}
|
58
|
+
record.jit_preload_aggregates[key] = preloaded_data[record.id] || default
|
59
|
+
end
|
60
|
+
else
|
61
|
+
self.jit_preload_aggregates[key] = send(assoc).where(conditions).send(aggregate, field) || default
|
64
62
|
end
|
65
|
-
|
66
|
-
self.jit_preload_aggregates[key] = send(assoc).where(conditions).send(aggregate, field) || default
|
63
|
+
jit_preload_aggregates[key]
|
67
64
|
end
|
68
|
-
jit_preload_aggregates[key]
|
69
65
|
end
|
70
66
|
end
|
71
67
|
end
|
72
68
|
end
|
73
69
|
|
74
|
-
ActiveRecord::Base.send(:
|
70
|
+
ActiveRecord::Base.send(:prepend, JitPreloadExtension)
|
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.2.
|
4
|
+
version: 0.2.2
|
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: 2019-
|
11
|
+
date: 2019-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -148,7 +148,6 @@ files:
|
|
148
148
|
- lib/jit_preloader.rb
|
149
149
|
- lib/jit_preloader/active_record/associations/collection_association.rb
|
150
150
|
- lib/jit_preloader/active_record/associations/preloader/association.rb
|
151
|
-
- lib/jit_preloader/active_record/associations/preloader/association.rb~
|
152
151
|
- lib/jit_preloader/active_record/associations/preloader/collection_association.rb
|
153
152
|
- lib/jit_preloader/active_record/associations/preloader/singular_association.rb
|
154
153
|
- lib/jit_preloader/active_record/associations/singular_association.rb
|
@@ -1,34 +0,0 @@
|
|
1
|
-
class ActiveRecord::Associations::Preloader::Association
|
2
|
-
private
|
3
|
-
# A monkey patch to ActiveRecord. The old method looked like the snippet
|
4
|
-
# below. Our changes here are that we remove records that are already
|
5
|
-
# part of the target, then attach all of the records to a new jit preloader.
|
6
|
-
#
|
7
|
-
# def preload(preloader)
|
8
|
-
# associated_records_by_owner(preloader).each do |owner, records|
|
9
|
-
# association = owner.association(reflection.name)
|
10
|
-
# association.loaded!
|
11
|
-
# association.target.concat(records)
|
12
|
-
# records.each { |record| association.set_inverse_instance(record) }
|
13
|
-
# end
|
14
|
-
# end
|
15
|
-
|
16
|
-
def preload(preloader)
|
17
|
-
return unless (reflection.scope.nil? || reflection.scope.arity == 0) && klass.ancestors.include?(ActiveRecord::Base)
|
18
|
-
all_records = []
|
19
|
-
associated_records_by_owner(preloader).each do |owner, records|
|
20
|
-
association = owner.association(reflection.name)
|
21
|
-
association.loaded!
|
22
|
-
# It is possible that some of the records are loaded already.
|
23
|
-
# We don't want to duplicate them, but we also want to preserve
|
24
|
-
# the original copy so that we don't blow away in-memory changes.
|
25
|
-
new_records = association.target.any? ? records - association.target : records
|
26
|
-
|
27
|
-
association.target.concat(new_records)
|
28
|
-
new_records.each { |record| association.set_inverse_instance(record) }
|
29
|
-
|
30
|
-
all_records.concat(records) if owner.jit_preloader || JitPreloader.globally_enabled?
|
31
|
-
end
|
32
|
-
JitPreloader::Preloader.attach(all_records) if all_records.any?
|
33
|
-
end
|
34
|
-
end
|