Commit 2a6ea1cda9a6b9aefb1406d8a097db6e5c8a65ce

Authored by Andrew
1 parent b9f5e2ca

Added scope_results option

CHANGELOG.md
... ... @@ -4,6 +4,7 @@
4 4 - Added support for `faraday_middleware-aws-sigv4`
5 5 - Added `credentials` option to `aws_credentials`
6 6 - Added `modifier` option to `boost_by`
  7 +- Added `scope_results` option
7 8  
8 9 ## 2.4.0
9 10  
... ...
lib/searchkick/query.rb
... ... @@ -19,7 +19,7 @@ module Searchkick
19 19 :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain,
20 20 :fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load,
21 21 :match, :misspellings, :model_includes, :offset, :operator, :order, :padding, :page, :per_page, :profile,
22   - :request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where]
  22 + :request_params, :routing, :scope_results, :select, :similar, :smart_aggs, :suggest, :track, :type, :where]
23 23 raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any?
24 24  
25 25 term = term.to_s
... ... @@ -113,7 +113,8 @@ module Searchkick
113 113 match_suffix: @match_suffix,
114 114 highlighted_fields: @highlighted_fields || [],
115 115 misspellings: @misspellings,
116   - term: term
  116 + term: term,
  117 + scope_results: options[:scope_results]
117 118 }
118 119  
119 120 if options[:debug]
... ...
lib/searchkick/results.rb
... ... @@ -221,6 +221,10 @@ module Searchkick
221 221 end
222 222 end
223 223  
  224 + if options[:scope_results]
  225 + records = options[:scope_results].call(records)
  226 + end
  227 +
224 228 Searchkick.load_records(records, ids)
225 229 end
226 230  
... ...
test/sql_test.rb
... ... @@ -211,4 +211,11 @@ class SqlTest < Minitest::Test
211 211 assert records.first.association(associations[klass].first).loaded?
212 212 end
213 213 end
  214 +
  215 + def test_scope_results
  216 + skip unless defined?(ActiveRecord)
  217 +
  218 + store_names ["Product A", "Product B"]
  219 + assert_search "product", ["Product A"], scope_results: ->(r) { r.where(name: "Product A") }
  220 + end
214 221 end
... ...