Commit c347c1e4016ac1126894ce7acaca559cd16e4737

Authored by Andrew Kane
1 parent ca27d8e4

Better test search on relations

lib/searchkick/model.rb
@@ -41,8 +41,11 @@ module Searchkick @@ -41,8 +41,11 @@ 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 + # TODO throw error in next major version
  45 + # TODO add Mongoid condition
  46 + relation = respond_to?(:current_scope) ? !current_scope.nil? : false
  47 + Searchkick.warn("calling search on a relation is deprecated") if relation
  48 +
46 Searchkick.search(term, model: self, **options, &block) 49 Searchkick.search(term, model: self, **options, &block)
47 end 50 end
48 alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name 51 alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name
test/match_test.rb
@@ -305,13 +305,22 @@ class MatchTest &lt; Minitest::Test @@ -305,13 +305,22 @@ class MatchTest &lt; Minitest::Test
305 # TODO find better place 305 # TODO find better place
306 306
307 def test_search_relation 307 def test_search_relation
308 - skip unless defined?(ActiveRecord) 308 + skip if defined?(Mongoid) # for now
309 309
310 - assert_nil Product.current_scope  
311 _, stderr = capture_io { Product.search("*") } 310 _, stderr = capture_io { Product.search("*") }
312 assert_equal "", stderr 311 assert_equal "", stderr
313 - assert Product.where(name: nil).current_scope  
314 - _, stderr = capture_io { Product.where(name: nil).search("*") } 312 + _, stderr = capture_io { Product.all.search("*") }
  313 + assert_match "WARNING", stderr
  314 + end
  315 +
  316 + def test_search_relation_default_scope
  317 + skip if defined?(Mongoid) # for now
  318 +
  319 + Band.reindex
  320 +
  321 + _, stderr = capture_io { Band.search("*") }
  322 + assert_equal "", stderr
  323 + _, stderr = capture_io { Band.all.search("*") }
315 assert_match "WARNING", stderr 324 assert_match "WARNING", stderr
316 end 325 end
317 end 326 end
test/models/band.rb 0 → 100644
@@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
  1 +class Band
  2 + searchkick
  3 +end
test/support/activerecord.rb
@@ -91,6 +91,10 @@ ActiveRecord::Migration.create_table :songs do |t| @@ -91,6 +91,10 @@ ActiveRecord::Migration.create_table :songs do |t|
91 t.string :name 91 t.string :name
92 end 92 end
93 93
  94 +ActiveRecord::Migration.create_table :bands do |t|
  95 + t.string :name
  96 +end
  97 +
94 class Product < ActiveRecord::Base 98 class Product < ActiveRecord::Base
95 belongs_to :store 99 belongs_to :store
96 end 100 end
@@ -119,3 +123,7 @@ end @@ -119,3 +123,7 @@ end
119 123
120 class Song < ActiveRecord::Base 124 class Song < ActiveRecord::Base
121 end 125 end
  126 +
  127 +class Band < ActiveRecord::Base
  128 + default_scope { where(name: "Test") }
  129 +end
test/support/mongoid.rb
@@ -66,3 +66,11 @@ class Song @@ -66,3 +66,11 @@ class Song
66 66
67 field :name 67 field :name
68 end 68 end
  69 +
  70 +class Band
  71 + include Mongoid::Document
  72 +
  73 + field :name
  74 +
  75 + default_scope -> { where(name: "Test") }
  76 +end