jit_preloader 0.0.7 → 0.0.8

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
  SHA1:
3
- metadata.gz: 0d26d02e1483223dda866884820aec9f06532e4a
4
- data.tar.gz: c0d4c36e1c7300030113a3ca1ba4dfb22681a062
3
+ metadata.gz: 612515b854cf998692cc121a1b5a04b802f6d287
4
+ data.tar.gz: fdd24d5df16d9e4239013bba560f1f6ebeee6059
5
5
  SHA512:
6
- metadata.gz: 897e6e6246d4568fdef1f34dbe29c37a54a64b0d1ff58ff4d2cdc3dae7a9765ecf87d99f94247624666cdd099549b5ceea864336333d9719130299147d7bc3d2
7
- data.tar.gz: a0d912d630bf11b8a7071a0a7949713eb6f67578735d186a53b18eb8d1f36fe89a5d289c0662c498196120d78b0a58e04558497aba3c1a2794712c669ac6ac6a
6
+ metadata.gz: f3514afa58d8942b0e506e6e1daeba465f6214f45fb89016bc98a22b8f5e27e15a7bf18fc0adb589072c07c608d3d9fd14a20dba790446c9534e76769a6f70e4
7
+ data.tar.gz: 11b3e498bd08b46c9cadce50ad68f0a3ffcccc9df6aa9edc1625ec1bdb2f10fc709f23631d39c0523bd73acdc5507032cf689a5df1cf31069900f48da5a033ea
@@ -6,6 +6,11 @@ module JitPreloadExtension
6
6
  attr_accessor :jit_preloader
7
7
  attr_accessor :jit_n_plus_one_tracking
8
8
  attr_accessor :jit_preload_aggregates
9
+
10
+ def reload(*args)
11
+ self.jit_preload_aggregates = {}
12
+ super
13
+ end
9
14
  end
10
15
 
11
16
  class_methods do
@@ -14,10 +19,11 @@ module JitPreloadExtension
14
19
  def has_many_aggregate(assoc, name, aggregate, field, default: 0)
15
20
  method_name = "#{assoc}_#{name}"
16
21
 
17
- define_method(method_name) do
22
+ define_method(method_name) do |conditions={}|
18
23
  self.jit_preload_aggregates ||= {}
19
24
 
20
- return jit_preload_aggregates[method_name] if jit_preload_aggregates.key?(method_name)
25
+ key = "#{method_name}|#{conditions.sort.hash}"
26
+ return jit_preload_aggregates[key] if jit_preload_aggregates.key?(key)
21
27
  if jit_preloader
22
28
  reflection = association(assoc).reflection
23
29
  primary_ids = jit_preloader.records.collect{|r| r[reflection.active_record_primary_key] }
@@ -26,27 +32,23 @@ module JitPreloadExtension
26
32
  association_scope = klass
27
33
  association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope
28
34
 
35
+ conditions[reflection.foreign_key] = primary_ids
36
+
29
37
  preloaded_data = Hash[association_scope
30
- .where(reflection.foreign_key => primary_ids)
38
+ .where(conditions)
31
39
  .group(reflection.foreign_key)
32
40
  .send(aggregate, field)
33
41
  ]
34
42
 
35
43
  jit_preloader.records.each do |record|
36
44
  record.jit_preload_aggregates ||= {}
37
- record.jit_preload_aggregates[method_name] = preloaded_data[record.id] || default
45
+ record.jit_preload_aggregates[key] = preloaded_data[record.id] || default
38
46
  end
39
47
  else
40
- self.jit_preload_aggregates[method_name] = send(assoc).send(aggregate, field) || default
48
+ self.jit_preload_aggregates[key] = send(assoc).where(conditions).send(aggregate, field) || default
41
49
  end
42
- jit_preload_aggregates[method_name]
50
+ jit_preload_aggregates[key]
43
51
  end
44
-
45
- def reload(*args)
46
- self.jit_preload_aggregates = {}
47
- super
48
- end
49
-
50
52
  end
51
53
  end
52
54
  end
@@ -1,3 +1,3 @@
1
1
  module JitPreloader
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -5,7 +5,8 @@ RSpec.describe JitPreloader::Preloader do
5
5
  let!(:contact1) do
6
6
  addresses = [
7
7
  Address.new(street: "123 Fake st", country: canada),
8
- Address.new(street: "21 Jump st", country: usa)
8
+ Address.new(street: "21 Jump st", country: usa),
9
+ Address.new(street: "90210 Beverly Hills", country: usa)
9
10
  ]
10
11
  phones = [
11
12
  PhoneNumber.new(phone: "4445556666"),
@@ -40,9 +41,9 @@ RSpec.describe JitPreloader::Preloader do
40
41
  end
41
42
 
42
43
  context "when preloading an aggregate" do
43
- let(:addresses_counts) { [2, 0, 2] }
44
+ let(:addresses_counts) { [3, 0, 2] }
44
45
  let(:phone_number_counts) { [2, 0, 1] }
45
- let(:maxes) { [11, 0, 12] }
46
+ let(:maxes) { [19, 0, 12] }
46
47
 
47
48
  context "without jit_preload" do
48
49
  it "generates N+1 query notifications for each one" do
@@ -60,7 +61,10 @@ RSpec.describe JitPreloader::Preloader do
60
61
  end
61
62
 
62
63
  context "with jit_preload" do
63
- it "doesn NOT generate N+1 query notifications" do
64
+ let(:usa_addresses_counts) { [2, 0, 1] }
65
+ let(:can_addresses_counts) { [1, 0, 1] }
66
+
67
+ it "does NOT generate N+1 query notifications" do
64
68
  ActiveSupport::Notifications.subscribed(callback, "n_plus_one_query") do
65
69
  Contact.jit_preload.each_with_index do |c, i|
66
70
  expect(c.addresses_count).to eql addresses_counts[i]
@@ -71,6 +75,13 @@ RSpec.describe JitPreloader::Preloader do
71
75
 
72
76
  expect(source_map).to eql({})
73
77
  end
78
+
79
+ it "can handle dynamic queries" do
80
+ Contact.jit_preload.each_with_index do |c, i|
81
+ expect(c.addresses_count(country: usa)).to eql usa_addresses_counts[i]
82
+ expect(c.addresses_count(country: canada)).to eql can_addresses_counts[i]
83
+ end
84
+ end
74
85
  end
75
86
  end
76
87
 
@@ -193,6 +204,16 @@ RSpec.describe JitPreloader::Preloader do
193
204
  expect(source_map).to eql({})
194
205
  end
195
206
  end
207
+
208
+ context "reload" do
209
+ it "clears the jit_preload_aggregates" do
210
+ contact = Contact.jit_preload.first
211
+
212
+ contact.addresses_count
213
+
214
+ expect { contact.reload }.to change{ contact.jit_preload_aggregates }.to({})
215
+ end
216
+ end
196
217
  end
197
218
  end
198
219
 
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.0.7
4
+ version: 0.0.8
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: 2017-03-16 00:00:00.000000000 Z
11
+ date: 2017-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord