jit_preloader 0.2.1 → 0.3.0

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
- SHA256:
3
- metadata.gz: e859fc3b5826b985e1b51721bb6d05dc215b2434b250e553f85606ffba196ce3
4
- data.tar.gz: d3637cf1b4cf27da79db1ad6dea4be978d9091ae96278abc2cbd02d5c2256076
2
+ SHA1:
3
+ metadata.gz: ed5a725f8d7f312a774f6a1a3c58e0f1345027d4
4
+ data.tar.gz: 9d0ef45b43032043aee85d162343bd51783a94e3
5
5
  SHA512:
6
- metadata.gz: a2d6a3f714be0c14a0f026d4b824f0c4f2176818af5338e94af11f5fe83af41b81181368e32ea42ad0cf00b5a78de598fd8010b77afe36b8ee01745ac9dca236
7
- data.tar.gz: fc80b435fa33b15f6686ca061943f050497f6485edd4b63d0a7c505403cfb8fe6019b85615f195b6d6b02fd09cf325970f93e1b3e8c7503bd42ec37f1df5f2c6
6
+ metadata.gz: 5db4eb1d7b2fc74a2c7a0e8e0e7238b88e6270495f0b8cca71ed31de85da9ced40092186ab1dc188b25dc759d40a0d014288cc1f161534baf48943a684cf906a
7
+ data.tar.gz: 5ccd2555def314a25a8a022f2aac717e5114ac65a5b32a6fc31c9ffdf176d7db54ff8834ca01962bc037b09bda0a6182204478e387b457c081b03542a9321aa6
data/.gitignore CHANGED
@@ -43,7 +43,7 @@ build-iPhoneSimulator/
43
43
 
44
44
  # for a library or gem, you might want to ignore these files since the code is
45
45
  # intended to run in multiple environments; otherwise, check them in:
46
- # Gemfile.lock
46
+ Gemfile.lock
47
47
  # .ruby-version
48
48
  # .ruby-gemset
49
49
 
data/README.md CHANGED
@@ -173,6 +173,43 @@ end
173
173
 
174
174
  ```
175
175
 
176
+ ### Preloading a subset of an association
177
+
178
+ There are often times when you want to preload a subset of an association, or change how the SQL statement is generated. For example, if a `Contact` model has
179
+ an `addresses` association, you may want to be able to get all of the addresses that belong to a specific country without introducing an N+1 query.
180
+ This is a method `preload_scoped_relation` that is available that can handle this for you.
181
+
182
+ ```ruby
183
+ #old
184
+ class Contact < ActiveRecord::Base
185
+ has_many :addresses
186
+ has_many :usa_addresses, ->{ where(country: Country.find_by_name("USA")) }
187
+ end
188
+
189
+ Contact.jit_preload.all.each do |contact|
190
+ # This will preload the association as expected, but it must be defined as an association in advance
191
+ contact.usa_addresses
192
+
193
+ # This will preload as the entire addresses association, and filters it in memory
194
+ contact.addresses.select{|address| address.country == Country.find_by_name("USA") }
195
+
196
+ # This is an N+1 query
197
+ contact.addresses.where(country: Country.find_by_name("USA"))
198
+ end
199
+
200
+ # New
201
+ Contact.jit_preload.all.each do |contact|
202
+ contact.preload_scoped_relation(
203
+ name: "USA Addresses",
204
+ base_association: :addresses,
205
+ preload_scope: Address.where(country: Country.find_by_name("USA"))
206
+ )
207
+ end
208
+ # SELECT * FROM contacts
209
+ # SELECT * FROM countries WHERE name = "USA" LIMIT 1
210
+ # SELECT "addresses".* FROM "addresses" WHERE "addresses"."country_id" = 10 AND "addresses"."contact_id" IN (1, 2, 3, ...)
211
+ ```
212
+
176
213
  ### Jit preloading globally across your application
177
214
 
178
215
  The JitPreloader can be globally enabled, in which case most N+1 queries in your app should just disappear. It is off by default.
@@ -201,7 +238,7 @@ This is mostly a magic bullet, but it doesn't solve all database-related problem
201
238
  ```ruby
202
239
  Contact.all.each do |contact|
203
240
  contact.emails.reload # Reloading the association
204
- contact.addresses.where(billing: true).to_a # Querying the association
241
+ contact.addresses.where(billing: true).to_a # Querying the association (Use: preload_scoped_relation to avoid these)
205
242
  end
206
243
  ```
207
244
 
@@ -215,7 +252,7 @@ end
215
252
 
216
253
  ## Contributing
217
254
 
218
- 1. Fork it ( https://github.com/[my-github-username]/jit_preloader/fork )
255
+ 1. Fork it ( https://github.com/clio/jit_preloader/fork )
219
256
  2. Create your feature branch (`git checkout -b my-new-feature`)
220
257
  3. Commit your changes (`git commit -am 'Add some feature'`)
221
258
  4. Push to the branch (`git push origin my-new-feature`)
@@ -18,13 +18,14 @@ 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", "< 6"
21
+ spec.add_dependency "activerecord", "> 4.2", "< 7"
22
22
  spec.add_dependency "activesupport"
23
23
 
24
24
  spec.add_development_dependency "bundler"
25
- spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rake", "~> 13.0"
26
26
  spec.add_development_dependency "rspec"
27
27
  spec.add_development_dependency "database_cleaner"
28
28
  spec.add_development_dependency "sqlite3"
29
29
  spec.add_development_dependency "byebug"
30
+ spec.add_development_dependency "db-query-matchers"
30
31
  end
@@ -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) < Gem::Version.new("5.2.2")
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
@@ -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)
@@ -1,74 +1,119 @@
1
1
  module JitPreloadExtension
2
+ attr_accessor :jit_preloader
3
+ attr_accessor :jit_n_plus_one_tracking
4
+ attr_accessor :jit_preload_aggregates
5
+ attr_accessor :jit_preload_scoped_relations
2
6
 
3
- extend ActiveSupport::Concern
7
+ def reload(*args)
8
+ clear_jit_preloader!
9
+ super
10
+ end
11
+
12
+ def clear_jit_preloader!
13
+ self.jit_preload_aggregates = {}
14
+ self.jit_preload_scoped_relations = {}
15
+ if jit_preloader
16
+ jit_preloader.records.delete(self)
17
+ self.jit_preloader = nil
18
+ end
19
+ end
20
+
21
+ def preload_scoped_relation(name:, base_association:, preload_scope: nil)
22
+ return jit_preload_scoped_relations[name] if jit_preload_scoped_relations&.key?(name)
4
23
 
5
- included do
6
- attr_accessor :jit_preloader
7
- attr_accessor :jit_n_plus_one_tracking
8
- attr_accessor :jit_preload_aggregates
24
+ records = jit_preloader&.records || [self]
25
+ previous_association_values = {}
9
26
 
10
- def reload(*args)
11
- clear_jit_preloader!
12
- super
27
+ records.each do |record|
28
+ association = record.association(base_association)
29
+ if association.loaded?
30
+ previous_association_values[record] = association.target
31
+ association.reset
32
+ end
13
33
  end
14
34
 
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
35
+ ActiveRecord::Associations::Preloader.new.preload(
36
+ records,
37
+ base_association,
38
+ preload_scope
39
+ )
40
+
41
+ records.each do |record|
42
+ record.jit_preload_scoped_relations ||= {}
43
+ association = record.association(base_association)
44
+ record.jit_preload_scoped_relations[name] = association.target
45
+ association.reset
46
+ if previous_association_values.key?(record)
47
+ association.target = previous_association_values[record]
20
48
  end
21
49
  end
22
50
 
51
+ jit_preload_scoped_relations[name]
23
52
  end
24
53
 
25
- class_methods do
26
- delegate :jit_preload, to: :all
54
+ def self.prepended(base)
55
+ class << base
56
+ delegate :jit_preload, to: :all
27
57
 
28
- def has_many_aggregate(assoc, name, aggregate, field, default: 0)
29
- method_name = "#{assoc}_#{name}"
58
+ def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, default: 0)
59
+ method_name = "#{assoc}_#{name}"
30
60
 
31
- define_method(method_name) do |conditions={}|
32
- self.jit_preload_aggregates ||= {}
61
+ define_method(method_name) do |conditions={}|
62
+ self.jit_preload_aggregates ||= {}
33
63
 
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
64
+ key = "#{method_name}|#{conditions.sort.hash}"
65
+ return jit_preload_aggregates[key] if jit_preload_aggregates.key?(key)
66
+ if jit_preloader
67
+ reflection = association(assoc).reflection
68
+ primary_ids = jit_preloader.records.collect{|r| r[reflection.active_record_primary_key] }
69
+ klass = reflection.klass
40
70
 
41
- aggregate_association = reflection
42
- while aggregate_association.through_reflection
43
- aggregate_association = aggregate_association.through_reflection
44
- end
71
+ aggregate_association = reflection
72
+ while aggregate_association.through_reflection
73
+ aggregate_association = aggregate_association.through_reflection
74
+ end
45
75
 
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
76
+ association_scope = klass.all.merge(association(assoc).scope).unscope(where: aggregate_association.foreign_key)
77
+ association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope
48
78
 
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}"
79
+ # If the query uses an alias for the association, use that instead of the table name
80
+ table_reference = table_alias_name
81
+ table_reference ||= association_scope.references_values.first || aggregate_association.table_name
82
+
83
+ conditions[table_reference] = { aggregate_association.foreign_key => primary_ids }
84
+
85
+ # If the association is a STI child model, specify its type in the condition so that it
86
+ # doesn't include results from other child models
87
+ parent_is_base_class = aggregate_association.klass.superclass.abstract_class? || aggregate_association.klass.superclass == ActiveRecord::Base
88
+ has_type_column = aggregate_association.klass.column_names.include?(aggregate_association.klass.inheritance_column)
89
+ is_child_sti_model = !parent_is_base_class && has_type_column
90
+ if is_child_sti_model
91
+ conditions[table_reference].merge!({ aggregate_association.klass.inheritance_column => aggregate_association.klass.sti_name })
92
+ end
93
+
94
+ if reflection.type.present?
95
+ conditions[reflection.type] = self.class.name
96
+ end
97
+ group_by = "#{table_reference}.#{aggregate_association.foreign_key}"
54
98
 
55
- preloaded_data = Hash[association_scope
56
- .where(conditions)
57
- .group(group_by)
58
- .send(aggregate, field)
59
- ]
99
+ preloaded_data = Hash[association_scope
100
+ .where(conditions)
101
+ .group(group_by)
102
+ .send(aggregate, field)
103
+ ]
60
104
 
61
- jit_preloader.records.each do |record|
62
- record.jit_preload_aggregates ||= {}
63
- record.jit_preload_aggregates[key] = preloaded_data[record.id] || default
105
+ jit_preloader.records.each do |record|
106
+ record.jit_preload_aggregates ||= {}
107
+ record.jit_preload_aggregates[key] = preloaded_data[record.id] || default
108
+ end
109
+ else
110
+ self.jit_preload_aggregates[key] = send(assoc).where(conditions).send(aggregate, field) || default
64
111
  end
65
- else
66
- self.jit_preload_aggregates[key] = send(assoc).where(conditions).send(aggregate, field) || default
112
+ jit_preload_aggregates[key]
67
113
  end
68
- jit_preload_aggregates[key]
69
114
  end
70
115
  end
71
116
  end
72
117
  end
73
118
 
74
- ActiveRecord::Base.send(:include, JitPreloadExtension)
119
+ ActiveRecord::Base.send(:prepend, JitPreloadExtension)
@@ -1,3 +1,3 @@
1
1
  module JitPreloader
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+ require "db-query-matchers"
3
+
4
+ RSpec.describe "ActiveRecord::Base Extensions" do
5
+
6
+ let(:canada) { Country.create(name: "Canada") }
7
+ let(:usa) { Country.create(name: "U.S.A") }
8
+
9
+ describe "#preload_scoped_relation" do
10
+ def call(contact)
11
+ contact.preload_scoped_relation(
12
+ name: "American Addresses",
13
+ base_association: :addresses,
14
+ preload_scope: Address.where(country: usa)
15
+ )
16
+ end
17
+
18
+ before do
19
+ Contact.create(name: "Bar", addresses: [
20
+ Address.new(street: "123 Fake st", country: canada),
21
+ Address.new(street: "21 Jump st", country: usa),
22
+ Address.new(street: "90210 Beverly Hills", country: usa)
23
+ ])
24
+
25
+ Contact.create(name: "Foo", addresses: [
26
+ Address.new(street: "1 First st", country: canada),
27
+ Address.new(street: "10 Tenth Ave", country: usa)
28
+ ])
29
+ end
30
+
31
+ context "when operating on a single object" do
32
+ it "will load the objects for that object" do
33
+ contact = Contact.first
34
+ expect(call(contact)).to match_array contact.addresses.where(country: usa).to_a
35
+ end
36
+ end
37
+
38
+ it "memoizes the result" do
39
+ contacts = Contact.jit_preload.limit(2).to_a
40
+
41
+ expect do
42
+ expect(call(contacts.first))
43
+ expect(call(contacts.first))
44
+ end.to make_database_queries(count: 1)
45
+ end
46
+
47
+ context "when reloading the object" do
48
+ it "clears the memoization" do
49
+ contacts = Contact.jit_preload.limit(2).to_a
50
+
51
+ expect do
52
+ expect(call(contacts.first))
53
+ end.to make_database_queries(count: 1)
54
+ contacts.first.reload
55
+ expect do
56
+ expect(call(contacts.first))
57
+ end.to make_database_queries(count: 1)
58
+ end
59
+ end
60
+
61
+ it "will issue one query for the group of objects" do
62
+ contacts = Contact.jit_preload.limit(2).to_a
63
+
64
+ usa_addresses = contacts.first.addresses.where(country: usa).to_a
65
+ expect do
66
+ expect(call(contacts.first)).to match_array usa_addresses
67
+ end.to make_database_queries(count: 1)
68
+
69
+ usa_addresses = contacts.last.addresses.where(country: usa).to_a
70
+ expect do
71
+ expect(call(contacts.last)).to match_array usa_addresses
72
+ end.to_not make_database_queries
73
+ end
74
+
75
+ it "doesn't load the value into the association" do
76
+ contacts = Contact.jit_preload.limit(2).to_a
77
+ call(contacts.first)
78
+
79
+ expect(contacts.first.association(:addresses)).to_not be_loaded
80
+ expect(contacts.last.association(:addresses)).to_not be_loaded
81
+ end
82
+
83
+ context "when the association is already loaded" do
84
+ it "doesn't change the value of the association" do
85
+ contacts = Contact.jit_preload.limit(2).to_a
86
+ contacts.each{|contact| contact.addresses.to_a }
87
+ contacts.each{|contact| call(contact) }
88
+
89
+ expect(contacts.first.association(:addresses)).to be_loaded
90
+ expect(contacts.last.association(:addresses)).to be_loaded
91
+ end
92
+ end
93
+ end
94
+ end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe JitPreloader::Preloader do
4
-
5
4
  let!(:contact1) do
6
5
  addresses = [
7
6
  Address.new(street: "123 Fake st", country: canada),
@@ -49,6 +48,79 @@ RSpec.describe JitPreloader::Preloader do
49
48
  ->(event, data){ source_map[data[:source]] << data[:association] }
50
49
  end
51
50
 
51
+ context "for single table inheritance" do
52
+ context "when preloading an aggregate for a child model" do
53
+ let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
54
+ let!(:company1) { Company.create(name: "Company1", contact_book: contact_book) }
55
+ let!(:company2) { Company.create(name: "Company2", contact_book: contact_book) }
56
+
57
+ it "can handle queries" do
58
+ contact_books = ContactBook.jit_preload.to_a
59
+ expect(contact_books.first.companies_count).to eq 2
60
+ end
61
+ end
62
+
63
+ context "when preloading an aggregate of a child model through its base model" do
64
+ let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
65
+ let!(:contact) { Contact.create(name: "Contact", contact_book: contact_book) }
66
+ let!(:company1) { Company.create(name: "Company1", contact_book: contact_book) }
67
+ let!(:company2) { Company.create(name: "Company2", contact_book: contact_book) }
68
+ let!(:contact_employee1) { Employee.create(name: "Contact Employee1", contact: contact) }
69
+ let!(:contact_employee2) { Employee.create(name: "Contact Employee2", contact: contact) }
70
+ let!(:company_employee1) { Employee.create(name: "Company Employee1", contact: company1) }
71
+ let!(:company_employee2) { Employee.create(name: "Company Employee2", contact: company2) }
72
+
73
+ it "can handle queries" do
74
+ contact_books = ContactBook.jit_preload.to_a
75
+ expect(contact_books.first.employees_count).to eq 4
76
+ end
77
+ end
78
+
79
+ context "when preloading an aggregate of a nested child model through another child model" do
80
+ let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
81
+ let!(:contact) { Contact.create(name: "Contact", contact_book: contact_book) }
82
+ let!(:company1) { Company.create(name: "Company1", contact_book: contact_book) }
83
+ let!(:company2) { Company.create(name: "Company2", contact_book: contact_book) }
84
+ let!(:contact_employee1) { Employee.create(name: "Contact Employee1", contact: contact) }
85
+ let!(:contact_employee2) { Employee.create(name: "Contact Employee2", contact: contact) }
86
+ let!(:company_employee1) { Employee.create(name: "Company Employee1", contact: company1) }
87
+ let!(:company_employee2) { Employee.create(name: "Company Employee2", contact: company2) }
88
+
89
+ it "can handle queries" do
90
+ contact_books = ContactBook.jit_preload.to_a
91
+ expect(contact_books.first.company_employees_count).to eq 2
92
+ end
93
+ end
94
+
95
+ context "when preloading an aggregate of a nested child model through a many-to-many relationship with another child model" do
96
+ let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
97
+ let!(:child1) { Child.create(name: "Child1") }
98
+ let!(:child2) { Child.create(name: "Child2") }
99
+ let!(:child3) { Child.create(name: "Child3") }
100
+ let!(:parent1) { Parent.create(name: "Parent1", contact_book: contact_book, children: [child1, child2]) }
101
+ let!(:parent2) { Parent.create(name: "Parent2", contact_book: contact_book, children: [child2, child3]) }
102
+
103
+ it "can handle queries" do
104
+ contact_books = ContactBook.jit_preload.to_a
105
+ expect(contact_books.first.children_count).to eq 4
106
+ expect(contact_books.first.children).to include(child1, child2, child3)
107
+ end
108
+ end
109
+
110
+ context "when preloading an aggregate for a child model scoped by another join table" do
111
+ let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
112
+ let!(:contact1) { Company.create(name: "Without Email", contact_book: contact_book) }
113
+ let!(:contact2) { Company.create(name: "With Blank Email", email_address: EmailAddress.new(address: ""), contact_book: contact_book) }
114
+ let!(:contact3) { Company.create(name: "With Email", email_address: EmailAddress.new(address: "a@a.com"), contact_book: contact_book) }
115
+
116
+ it "can handle queries" do
117
+ contact_books = ContactBook.jit_preload.to_a
118
+ expect(contact_books.first.companies_with_blank_email_address_count).to eq 1
119
+ expect(contact_books.first.companies_with_blank_email_address).to eq [contact2]
120
+ end
121
+ end
122
+ end
123
+
52
124
  context "when preloading an aggregate as polymorphic" do
53
125
  let(:contact_owner_counts) { [2] }
54
126
 
@@ -1,12 +1,14 @@
1
1
  class Database
2
2
  def self.tables
3
3
  [
4
- "CREATE TABLE contacts (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255), contact_owner_id INTEGER, contact_owner_type VARCHAR(255))",
4
+ "CREATE TABLE contact_books (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255))",
5
+ "CREATE TABLE contacts (id INTEGER NOT NULL PRIMARY KEY, type VARCHAR(255), contact_book_id INTEGER, contact_id INTEGER, name VARCHAR(255), contact_owner_id INTEGER, contact_owner_type VARCHAR(255))",
5
6
  "CREATE TABLE contact_owners (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255))",
6
7
  "CREATE TABLE addresses (id INTEGER NOT NULL PRIMARY KEY, contact_id INTEGER NOT NULL, country_id INTEGER NOT NULL, street VARCHAR(255))",
7
8
  "CREATE TABLE email_addresses (id INTEGER NOT NULL PRIMARY KEY, contact_id INTEGER NOT NULL, address VARCHAR(255))",
8
9
  "CREATE TABLE phone_numbers (id INTEGER NOT NULL PRIMARY KEY, contact_id INTEGER NOT NULL, phone VARCHAR(10))",
9
10
  "CREATE TABLE countries (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255))",
11
+ "CREATE TABLE parents_children (id INTEGER NOT NULL PRIMARY KEY, parent_id INTEGER, child_id INTEGER)",
10
12
  ]
11
13
  end
12
14
 
@@ -1,15 +1,58 @@
1
+ class ContactBook < ActiveRecord::Base
2
+ has_many :contacts
3
+ has_many :employees, through: :contacts
4
+
5
+ has_many :companies
6
+ has_many :company_employees, through: :companies, source: :employees
7
+
8
+ has_many :parents
9
+ has_many :children, through: :parents
10
+
11
+ has_many_aggregate :companies, :count, :count, "*"
12
+ has_many_aggregate :employees, :count, :count, "*"
13
+ has_many_aggregate :company_employees, :count, :count, "*"
14
+ has_many_aggregate :children, :count, :count, "*"
15
+
16
+ has_many :companies_with_blank_email_address, -> { joins(:email_address).where(email_addresses: { address: "" }) }, class_name: "Company"
17
+ has_many_aggregate :companies_with_blank_email_address, :count, :count, "*", table_alias_name: "contacts"
18
+ end
19
+
1
20
  class Contact < ActiveRecord::Base
21
+ belongs_to :contact_book
2
22
  belongs_to :contact_owner, polymorphic: true
3
23
 
4
24
  has_many :addresses
5
25
  has_many :phone_numbers
6
26
  has_one :email_address
27
+ has_many :employees
7
28
 
8
29
  has_many_aggregate :addresses, :max_street_length, :maximum, "LENGTH(street)"
9
30
  has_many_aggregate :phone_numbers, :count, :count, "id"
10
31
  has_many_aggregate :addresses, :count, :count, "*"
11
32
  end
12
33
 
34
+ class Company < Contact
35
+ end
36
+
37
+ class Employee < Contact
38
+ belongs_to :contact
39
+ end
40
+
41
+ class ParentsChild < ActiveRecord::Base
42
+ belongs_to :parent
43
+ belongs_to :child
44
+ end
45
+
46
+ class Parent < Contact
47
+ has_many :parents_child
48
+ has_many :children, through: :parents_child
49
+ end
50
+
51
+ class Child < Contact
52
+ has_many :parents_child
53
+ has_many :parents, through: :parents_child
54
+ end
55
+
13
56
  class Address < ActiveRecord::Base
14
57
  belongs_to :contact
15
58
  belongs_to :country
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.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: 2019-08-16 00:00:00.000000000 Z
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: '6'
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: '6'
32
+ version: '7'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -64,14 +64,14 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '10.0'
67
+ version: '13.0'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '10.0'
74
+ version: '13.0'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rspec
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -128,6 +128,20 @@ dependencies:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: db-query-matchers
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
131
145
  description: The JitPreloader has the ability to send notifications when N+1 queries
132
146
  occur to help guage how problematic they are for your code base and a way to remove
133
147
  all of the commons explicitly or automatically
@@ -140,15 +154,14 @@ files:
140
154
  - ".gitignore"
141
155
  - ".rspec"
142
156
  - Gemfile
143
- - Gemfile.lock
144
157
  - LICENSE
145
158
  - README.md
146
159
  - Rakefile
147
160
  - jit_preloader.gemspec
148
161
  - lib/jit_preloader.rb
149
162
  - lib/jit_preloader/active_record/associations/collection_association.rb
150
- - lib/jit_preloader/active_record/associations/preloader/association.rb
151
- - lib/jit_preloader/active_record/associations/preloader/association.rb~
163
+ - lib/jit_preloader/active_record/associations/preloader/ar5_association.rb
164
+ - lib/jit_preloader/active_record/associations/preloader/ar6_association.rb
152
165
  - lib/jit_preloader/active_record/associations/preloader/collection_association.rb
153
166
  - lib/jit_preloader/active_record/associations/preloader/singular_association.rb
154
167
  - lib/jit_preloader/active_record/associations/singular_association.rb
@@ -156,6 +169,7 @@ files:
156
169
  - lib/jit_preloader/active_record/relation.rb
157
170
  - lib/jit_preloader/preloader.rb
158
171
  - lib/jit_preloader/version.rb
172
+ - spec/lib/jit_preloader/active_record/base_spec.rb
159
173
  - spec/lib/jit_preloader/preloader_spec.rb
160
174
  - spec/spec_helper.rb
161
175
  - spec/support/database.rb
@@ -179,11 +193,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
193
  - !ruby/object:Gem::Version
180
194
  version: '0'
181
195
  requirements: []
182
- rubygems_version: 3.0.3
196
+ rubyforge_project:
197
+ rubygems_version: 2.6.14
183
198
  signing_key:
184
199
  specification_version: 4
185
200
  summary: Tool to understand N+1 queries and to remove them
186
201
  test_files:
202
+ - spec/lib/jit_preloader/active_record/base_spec.rb
187
203
  - spec/lib/jit_preloader/preloader_spec.rb
188
204
  - spec/spec_helper.rb
189
205
  - spec/support/database.rb
@@ -1,62 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- jit_preloader (0.2.1)
5
- activerecord (> 4.2, < 6)
6
- activesupport
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activemodel (5.2.2)
12
- activesupport (= 5.2.2)
13
- activerecord (5.2.2)
14
- activemodel (= 5.2.2)
15
- activesupport (= 5.2.2)
16
- arel (>= 9.0)
17
- activesupport (5.2.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.4)
25
- database_cleaner (1.5.3)
26
- diff-lcs (1.2.5)
27
- i18n (1.6.0)
28
- concurrent-ruby (~> 1.0)
29
- minitest (5.11.3)
30
- rake (10.5.0)
31
- rspec (3.5.0)
32
- rspec-core (~> 3.5.0)
33
- rspec-expectations (~> 3.5.0)
34
- rspec-mocks (~> 3.5.0)
35
- rspec-core (3.5.4)
36
- rspec-support (~> 3.5.0)
37
- rspec-expectations (3.5.0)
38
- diff-lcs (>= 1.2.0, < 2.0)
39
- rspec-support (~> 3.5.0)
40
- rspec-mocks (3.5.0)
41
- diff-lcs (>= 1.2.0, < 2.0)
42
- rspec-support (~> 3.5.0)
43
- rspec-support (3.5.0)
44
- sqlite3 (1.3.12)
45
- thread_safe (0.3.6)
46
- tzinfo (1.2.5)
47
- thread_safe (~> 0.1)
48
-
49
- PLATFORMS
50
- ruby
51
-
52
- DEPENDENCIES
53
- bundler
54
- byebug
55
- database_cleaner
56
- jit_preloader!
57
- rake (~> 10.0)
58
- rspec
59
- sqlite3
60
-
61
- BUNDLED WITH
62
- 2.0.1
@@ -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