Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for further filtering of subquery #511

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/pg_search/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def pg_search_scope(name, options)
raise ArgumentError, "pg_search_scope expects a Hash or Proc"
end

define_singleton_method(name) do |*args|
define_singleton_method(name) do |*args, &block|
config = Configuration.new(options_proc.call(*args), self)
scope_options = ScopeOptions.new(config)
scope_options.apply(self)
scope_options.apply(self, &block)
end
end

Expand Down
12 changes: 7 additions & 5 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def initialize(config)
@feature_options = config.feature_options
end

def apply(scope)
def apply(scope, &block)
scope = include_table_aliasing_for_rank(scope)
rank_table_alias = scope.pg_search_rank_table_alias(include_counter: true)

scope
.joins(rank_join(rank_table_alias))
.joins(rank_join(rank_table_alias, &block))
.order(Arel.sql("#{rank_table_alias}.rank DESC, #{order_within_rank}"))
.extend(WithPgSearchRank)
.extend(WithPgSearchHighlight[feature_for(:tsearch)])
Expand Down Expand Up @@ -79,14 +79,16 @@ def increment_counter
delegate :connection, :quoted_table_name, to: :model

def subquery
model
relation = model
.unscoped
.select("#{primary_key} AS pg_search_id")
.select("#{rank} AS rank")
.joins(subquery_join)
.where(conditions)
.limit(nil)
.offset(nil)

block_given? ? yield(relation) : relation
end

def conditions
Expand Down Expand Up @@ -141,8 +143,8 @@ def rank
end
end

def rank_join(rank_table_alias)
"INNER JOIN (#{subquery.to_sql}) AS #{rank_table_alias} ON #{primary_key} = #{rank_table_alias}.pg_search_id"
def rank_join(rank_table_alias, &block)
"INNER JOIN (#{subquery(&block).to_sql}) AS #{rank_table_alias} ON #{primary_key} = #{rank_table_alias}.pg_search_id"
end

def include_table_aliasing_for_rank(scope)
Expand Down
15 changes: 15 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
table do |t|
t.string "title"
t.text "content"
t.integer "tenant_id", null: false, default: 1
t.integer "parent_model_id"
t.integer "importance"
t.index ["tenant_id"], name: "index_model_with_pg_searches_on_tenant_id"
end

model do
Expand Down Expand Up @@ -178,6 +180,19 @@
ModelWithPgSearch.pg_search_scope :search_content, against: :content
end

context "when searching by tenant" do
it "uses the block to constrain the subquery to the tenant" do
ModelWithPgSearch.create!(tenant_id: 1, content: "foo")
tenant_2 = ModelWithPgSearch.create!(tenant_id: 2, content: "foo")

results = ModelWithPgSearch.search_content("foo") do |subquery|
subquery.where(tenant_id: 2)
end

expect(results).to eq [tenant_2]
end
end

context "when chained after a select() scope" do
it "honors the select" do
included = ModelWithPgSearch.create!(content: "foo", title: "bar")
Expand Down