Commit 61673c4816337b32f196278da863e11bb200d8b7

Authored by Andrew Kane
1 parent 642ec519

Raise warning when search called on relation

Showing 2 changed files with 15 additions and 0 deletions   Show diff stats
lib/searchkick/model.rb
@@ -41,6 +41,8 @@ module Searchkick @@ -41,6 +41,8 @@ module Searchkick
41 41
42 class << self 42 class << self
43 def searchkick_search(term = "*", **options, &block) 43 def searchkick_search(term = "*", **options, &block)
  44 + # TODO same for Mongoid
  45 + Searchkick.warn("calling search on a relation is deprecated") if respond_to?(:current_scope) && !current_scope.nil?
44 Searchkick.search(term, model: self, **options, &block) 46 Searchkick.search(term, model: self, **options, &block)
45 end 47 end
46 alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name 48 alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name
test/match_test.rb
@@ -301,4 +301,17 @@ class MatchTest &lt; Minitest::Test @@ -301,4 +301,17 @@ class MatchTest &lt; Minitest::Test
301 store_names ["Ice Cream Cake"] 301 store_names ["Ice Cream Cake"]
302 assert_search "๐Ÿจ๐Ÿฐ", ["Ice Cream Cake"], emoji: true 302 assert_search "๐Ÿจ๐Ÿฐ", ["Ice Cream Cake"], emoji: true
303 end 303 end
  304 +
  305 + # TODO find better place
  306 +
  307 + def test_search_relation
  308 + skip unless defined?(ActiveRecord)
  309 +
  310 + assert_nil Product.current_scope
  311 + _, stderr = capture_io { Product.search("*") }
  312 + assert_equal "", stderr
  313 + assert Product.where(name: nil).current_scope
  314 + _, stderr = capture_io { Product.where(name: nil).search("*") }
  315 + assert_match "WARNING", stderr
  316 + end
304 end 317 end