jit_preloader 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +2 -2
- data/README.md +1 -0
- data/lib/jit_preloader/active_record/base.rb +3 -3
- data/lib/jit_preloader/version.rb +1 -1
- data/spec/lib/jit_preloader/preloader_spec.rb +13 -0
- data/spec/support/models.rb +3 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '09ecd094d5ac62f0599c9ed43773b8fc8188af6f'
|
4
|
+
data.tar.gz: ab9723b5d2a0aa9c73f3ac6108fe573e24c67ec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d966d6459872e1c2d97a6c511823f781e6c3949e45e60cba2e1dd46a16a10d2932a7d9cadd8173771eef1b808355923bc2bcfa515bc2be821ddf909b1d52e5af
|
7
|
+
data.tar.gz: d680dbaa731dc7f0cf5621d0bf39bbcb45feb09419b6c43c90d6c0ac5fa3cb6ffc4c3e9be372df545c2b897a66cf134ab3e0c47fc40c8bc1c754398120caaf63
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -208,6 +208,7 @@ end
|
|
208
208
|
# SELECT * FROM contacts
|
209
209
|
# SELECT * FROM countries WHERE name = "USA" LIMIT 1
|
210
210
|
# SELECT "addresses".* FROM "addresses" WHERE "addresses"."country_id" = 10 AND "addresses"."contact_id" IN (1, 2, 3, ...)
|
211
|
+
```
|
211
212
|
|
212
213
|
### Jit preloading globally across your application
|
213
214
|
|
@@ -55,7 +55,7 @@ module JitPreloadExtension
|
|
55
55
|
class << base
|
56
56
|
delegate :jit_preload, to: :all
|
57
57
|
|
58
|
-
def has_many_aggregate(assoc, name, aggregate, field, default: 0)
|
58
|
+
def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, default: 0)
|
59
59
|
method_name = "#{assoc}_#{name}"
|
60
60
|
|
61
61
|
define_method(method_name) do |conditions={}|
|
@@ -77,8 +77,8 @@ module JitPreloadExtension
|
|
77
77
|
association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope
|
78
78
|
|
79
79
|
# If the query uses an alias for the association, use that instead of the table name
|
80
|
-
|
81
|
-
table_reference
|
80
|
+
table_reference = table_alias_name
|
81
|
+
table_reference ||= association_scope.references_values.first || aggregate_association.table_name
|
82
82
|
|
83
83
|
conditions[table_reference] = { aggregate_association.foreign_key => primary_ids }
|
84
84
|
|
@@ -106,6 +106,19 @@ RSpec.describe JitPreloader::Preloader do
|
|
106
106
|
expect(contact_books.first.children).to include(child1, child2, child3)
|
107
107
|
end
|
108
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
|
109
122
|
end
|
110
123
|
|
111
124
|
context "when preloading an aggregate as polymorphic" do
|
data/spec/support/models.rb
CHANGED
@@ -12,6 +12,9 @@ class ContactBook < ActiveRecord::Base
|
|
12
12
|
has_many_aggregate :employees, :count, :count, "*"
|
13
13
|
has_many_aggregate :company_employees, :count, :count, "*"
|
14
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"
|
15
18
|
end
|
16
19
|
|
17
20
|
class Contact < ActiveRecord::Base
|
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.5
|
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-
|
11
|
+
date: 2020-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -193,7 +193,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
|
-
|
196
|
+
rubyforge_project:
|
197
|
+
rubygems_version: 2.6.14
|
197
198
|
signing_key:
|
198
199
|
specification_version: 4
|
199
200
|
summary: Tool to understand N+1 queries and to remove them
|