diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f90d82..4182fd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Added support for `faraday_middleware-aws-sigv4` - Added `credentials` option to `aws_credentials` - Added `modifier` option to `boost_by` +- Added `scope_results` option ## 2.4.0 diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index d9e639c..848691e 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -19,7 +19,7 @@ module Searchkick :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain, :fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load, :match, :misspellings, :model_includes, :offset, :operator, :order, :padding, :page, :per_page, :profile, - :request_params, :routing, :select, :similar, :smart_aggs, :suggest, :track, :type, :where] + :request_params, :routing, :scope_results, :select, :similar, :smart_aggs, :suggest, :track, :type, :where] raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? term = term.to_s @@ -113,7 +113,8 @@ module Searchkick match_suffix: @match_suffix, highlighted_fields: @highlighted_fields || [], misspellings: @misspellings, - term: term + term: term, + scope_results: options[:scope_results] } if options[:debug] diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 3b31045..152cea3 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -221,6 +221,10 @@ module Searchkick end end + if options[:scope_results] + records = options[:scope_results].call(records) + end + Searchkick.load_records(records, ids) end diff --git a/test/sql_test.rb b/test/sql_test.rb index f334336..c312694 100644 --- a/test/sql_test.rb +++ b/test/sql_test.rb @@ -211,4 +211,11 @@ class SqlTest < Minitest::Test assert records.first.association(associations[klass].first).loaded? end end + + def test_scope_results + skip unless defined?(ActiveRecord) + + store_names ["Product A", "Product B"] + assert_search "product", ["Product A"], scope_results: ->(r) { r.where(name: "Product A") } + end end -- libgit2 0.21.0