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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: c697833105982c2f6ee8312618f5d32550c493c00c2b8ca76c6ca1895e387171
4
- data.tar.gz: c679aad87494ee39f689ef1e9d071897b78308c3b0c25847009703ec0712e741
2
+ SHA1:
3
+ metadata.gz: '09ecd094d5ac62f0599c9ed43773b8fc8188af6f'
4
+ data.tar.gz: ab9723b5d2a0aa9c73f3ac6108fe573e24c67ec9
5
5
  SHA512:
6
- metadata.gz: 6eb1c49ec80fb2b9abf6cba22a38c55043de01e6d9138026b92ce000c460dd2dda20dc72d18ba94cd18b9393dea2cbc2fccfc4806ea6aa93b97bb323a697be9a
7
- data.tar.gz: 82b70f1ac6a12c6c87a980f06c1e85802fc5b16f566651c1945ed663d698b11ede6b0b0a4dbc2010b943d18d6955612c78f3281080ef8817e7e7fdc440979ec7
6
+ metadata.gz: d966d6459872e1c2d97a6c511823f781e6c3949e45e60cba2e1dd46a16a10d2932a7d9cadd8173771eef1b808355923bc2bcfa515bc2be821ddf909b1d52e5af
7
+ data.tar.gz: d680dbaa731dc7f0cf5621d0bf39bbcb45feb09419b6c43c90d6c0ac5fa3cb6ffc4c3e9be372df545c2b897a66cf134ab3e0c47fc40c8bc1c754398120caaf63
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jit_preloader (0.2.3)
4
+ jit_preloader (0.2.5)
5
5
  activerecord (> 4.2, < 6)
6
6
  activesupport
7
7
 
@@ -63,4 +63,4 @@ DEPENDENCIES
63
63
  sqlite3
64
64
 
65
65
  BUNDLED WITH
66
- 2.0.1
66
+ 2.1.4
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
- table_alias_name = association_scope.references_values.first
81
- table_reference = table_alias_name || aggregate_association.table_name
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
 
@@ -1,3 +1,3 @@
1
1
  module JitPreloader
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -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
@@ -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
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-04-02 00:00:00.000000000 Z
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
- rubygems_version: 3.0.6
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