Commit c347c1e4016ac1126894ce7acaca559cd16e4737
1 parent
ca27d8e4
Exists in
master
and in
8 other branches
Better test search on relations
Showing
5 changed files
with
37 additions
and
6 deletions
Show diff stats
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 < Minitest::Test | @@ -305,13 +305,22 @@ class MatchTest < 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/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 |