jit_preloader 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e859fc3b5826b985e1b51721bb6d05dc215b2434b250e553f85606ffba196ce3
4
- data.tar.gz: d3637cf1b4cf27da79db1ad6dea4be978d9091ae96278abc2cbd02d5c2256076
3
+ metadata.gz: a2439886049ccbcd7845882f8fa47524e500ffc96d8b9620711288853c47c131
4
+ data.tar.gz: 189adc93418cf777c6eb33617934d92426cfbdc105a05a10c639f9dd387736f8
5
5
  SHA512:
6
- metadata.gz: a2d6a3f714be0c14a0f026d4b824f0c4f2176818af5338e94af11f5fe83af41b81181368e32ea42ad0cf00b5a78de598fd8010b77afe36b8ee01745ac9dca236
7
- data.tar.gz: fc80b435fa33b15f6686ca061943f050497f6485edd4b63d0a7c505403cfb8fe6019b85615f195b6d6b02fd09cf325970f93e1b3e8c7503bd42ec37f1df5f2c6
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
- extend ActiveSupport::Concern
4
-
5
- included do
6
- attr_accessor :jit_preloader
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
- def clear_jit_preloader!
16
- self.jit_preload_aggregates = {}
17
- if jit_preloader
18
- jit_preloader.records.delete(self)
19
- self.jit_preloader = nil
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
- class_methods do
26
- delegate :jit_preload, to: :all
19
+ def self.prepended(base)
20
+ class << base
21
+ delegate :jit_preload, to: :all
27
22
 
28
- def has_many_aggregate(assoc, name, aggregate, field, default: 0)
29
- method_name = "#{assoc}_#{name}"
23
+ def has_many_aggregate(assoc, name, aggregate, field, default: 0)
24
+ method_name = "#{assoc}_#{name}"
30
25
 
31
- define_method(method_name) do |conditions={}|
32
- self.jit_preload_aggregates ||= {}
26
+ define_method(method_name) do |conditions={}|
27
+ self.jit_preload_aggregates ||= {}
33
28
 
34
- key = "#{method_name}|#{conditions.sort.hash}"
35
- return jit_preload_aggregates[key] if jit_preload_aggregates.key?(key)
36
- if jit_preloader
37
- reflection = association(assoc).reflection
38
- primary_ids = jit_preloader.records.collect{|r| r[reflection.active_record_primary_key] }
39
- klass = reflection.klass
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
- aggregate_association = reflection
42
- while aggregate_association.through_reflection
43
- aggregate_association = aggregate_association.through_reflection
44
- end
36
+ aggregate_association = reflection
37
+ while aggregate_association.through_reflection
38
+ aggregate_association = aggregate_association.through_reflection
39
+ end
45
40
 
46
- association_scope = klass.all.merge(association(assoc).scope).unscope(where: aggregate_association.foreign_key)
47
- association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope
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
- conditions[aggregate_association.table_name] = { aggregate_association.foreign_key => primary_ids }
50
- if reflection.type.present?
51
- conditions[reflection.type] = self.class.name
52
- end
53
- group_by = "#{aggregate_association.table_name}.#{aggregate_association.foreign_key}"
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
- preloaded_data = Hash[association_scope
56
- .where(conditions)
57
- .group(group_by)
58
- .send(aggregate, field)
59
- ]
50
+ preloaded_data = Hash[association_scope
51
+ .where(conditions)
52
+ .group(group_by)
53
+ .send(aggregate, field)
54
+ ]
60
55
 
61
- jit_preloader.records.each do |record|
62
- record.jit_preload_aggregates ||= {}
63
- record.jit_preload_aggregates[key] = preloaded_data[record.id] || default
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
- else
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(:include, JitPreloadExtension)
70
+ ActiveRecord::Base.send(:prepend, JitPreloadExtension)
@@ -1,3 +1,3 @@
1
1
  module JitPreloader
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
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.1
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-08-16 00:00:00.000000000 Z
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