Skip to content

Commit

Permalink
Merge pull request #26 from clio/table-alias-name
Browse files Browse the repository at this point in the history
When the association scope is chained with conditions of other tables, the names of join tables are also added to `references_values`. It becomes impossible to identify the table alias name from the `references_values`.

The PR provides a way to customize the table alias name when the default way cannot figure out the correct value.
  • Loading branch information
Arthur Chui authored May 7, 2020
2 parents 8c77836 + 34266dc commit 7f4b47d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
jit_preloader (0.2.3)
jit_preloader (0.2.5)
activerecord (> 4.2, < 6)
activesupport

Expand Down Expand Up @@ -63,4 +63,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
2.0.1
2.1.4
6 changes: 3 additions & 3 deletions lib/jit_preloader/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.prepended(base)
class << base
delegate :jit_preload, to: :all

def has_many_aggregate(assoc, name, aggregate, field, default: 0)
def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, default: 0)
method_name = "#{assoc}_#{name}"

define_method(method_name) do |conditions={}|
Expand All @@ -77,8 +77,8 @@ def has_many_aggregate(assoc, name, aggregate, field, default: 0)
association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope

# If the query uses an alias for the association, use that instead of the table name
table_alias_name = association_scope.references_values.first
table_reference = table_alias_name || aggregate_association.table_name
table_reference = table_alias_name
table_reference ||= association_scope.references_values.first || aggregate_association.table_name

conditions[table_reference] = { aggregate_association.foreign_key => primary_ids }

Expand Down
2 changes: 1 addition & 1 deletion lib/jit_preloader/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JitPreloader
VERSION = "0.2.4"
VERSION = "0.2.5"
end
13 changes: 13 additions & 0 deletions spec/lib/jit_preloader/preloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@
expect(contact_books.first.children).to include(child1, child2, child3)
end
end

context "when preloading an aggregate for a child model scoped by another join table" do
let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
let!(:contact1) { Company.create(name: "Without Email", contact_book: contact_book) }
let!(:contact2) { Company.create(name: "With Blank Email", email_address: EmailAddress.new(address: ""), contact_book: contact_book) }
let!(:contact3) { Company.create(name: "With Email", email_address: EmailAddress.new(address: "[email protected]"), contact_book: contact_book) }

it "can handle queries" do
contact_books = ContactBook.jit_preload.to_a
expect(contact_books.first.companies_with_blank_email_address_count).to eq 1
expect(contact_books.first.companies_with_blank_email_address).to eq [contact2]
end
end
end

context "when preloading an aggregate as polymorphic" do
Expand Down
3 changes: 3 additions & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class ContactBook < ActiveRecord::Base
has_many_aggregate :employees, :count, :count, "*"
has_many_aggregate :company_employees, :count, :count, "*"
has_many_aggregate :children, :count, :count, "*"

has_many :companies_with_blank_email_address, -> { joins(:email_address).where(email_addresses: { address: "" }) }, class_name: "Company"
has_many_aggregate :companies_with_blank_email_address, :count, :count, "*", table_alias_name: "contacts"
end

class Contact < ActiveRecord::Base
Expand Down

0 comments on commit 7f4b47d

Please sign in to comment.