Skip to content

Commit 9a15875

Browse files
committed
Extend the block scoping from PR Casecommons#542 extends to associated_against subqueries. Further reduce big table scans, reduce corpus of scans.
1 parent b04a220 commit 9a15875

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

lib/pg_search/configuration/association.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def table_name
1919
@model.reflect_on_association(@name).table_name
2020
end
2121

22-
def join(primary_key)
23-
"LEFT OUTER JOIN (#{relation(primary_key).to_sql}) #{subselect_alias} ON #{subselect_alias}.id = #{primary_key}"
22+
def join(primary_key, &block)
23+
"LEFT OUTER JOIN (#{relation(primary_key, &block).to_sql}) #{subselect_alias} ON #{subselect_alias}.id = #{primary_key}"
2424
end
2525

2626
def subselect_alias
@@ -49,9 +49,13 @@ def selects_for_multiple_association
4949
end.join(", ")
5050
end
5151

52-
def relation(primary_key)
52+
def relation(primary_key, &block)
5353
result = @model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}")
5454
result = result.group(primary_key) unless singular_association?
55+
56+
# Apply optional scoping block to associated relation
57+
result = block.call(result) if block_given?
58+
5559
result
5660
end
5761

lib/pg_search/scope_options.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ def increment_counter
7979

8080
delegate :connection, :quoted_table_name, to: :model
8181

82-
def subquery
82+
def subquery(&block)
8383
relation = model
8484
.unscoped
8585
.select("#{primary_key} AS pg_search_id")
8686
.select("#{rank} AS rank")
87-
.joins(subquery_join)
87+
.joins(subquery_join(&block))
8888
.where(conditions)
8989
.limit(nil)
9090
.offset(nil)
@@ -128,10 +128,10 @@ def primary_key
128128
"#{quoted_table_name}.#{connection.quote_column_name(model.primary_key)}"
129129
end
130130

131-
def subquery_join
131+
def subquery_join(&block)
132132
if config.associations.any?
133133
config.associations.map do |association|
134-
association.join(primary_key)
134+
association.join(primary_key, &block)
135135
end.join(" ")
136136
end
137137
end

0 commit comments

Comments
 (0)