Skip to content

Commit

Permalink
Allow for further filtering of subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Willett committed Jul 13, 2018
1 parent 1d53250 commit c6418b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/pg_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def pg_search_scope(name, options)
->(query) { {:query => query}.merge(options) }
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 @@ -10,12 +10,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)

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(DisableEagerLoading)
.extend(WithPgSearchRank)
Expand Down Expand Up @@ -94,14 +94,16 @@ def pg_search_scope_application_count_plus_plus
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 @@ -165,8 +167,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

0 comments on commit c6418b5

Please sign in to comment.