diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 90ad8f5..d1f6446 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -41,8 +41,11 @@ module Searchkick class << self def searchkick_search(term = "*", **options, &block) - # TODO same for Mongoid - Searchkick.warn("calling search on a relation is deprecated") if respond_to?(:current_scope) && !current_scope.nil? + # TODO throw error in next major version + # TODO add Mongoid condition + relation = respond_to?(:current_scope) ? !current_scope.nil? : false + Searchkick.warn("calling search on a relation is deprecated") if relation + Searchkick.search(term, model: self, **options, &block) end alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name diff --git a/test/match_test.rb b/test/match_test.rb index 8fffd1b..da7a860 100644 --- a/test/match_test.rb +++ b/test/match_test.rb @@ -305,13 +305,22 @@ class MatchTest < Minitest::Test # TODO find better place def test_search_relation - skip unless defined?(ActiveRecord) + skip if defined?(Mongoid) # for now - assert_nil Product.current_scope _, stderr = capture_io { Product.search("*") } assert_equal "", stderr - assert Product.where(name: nil).current_scope - _, stderr = capture_io { Product.where(name: nil).search("*") } + _, stderr = capture_io { Product.all.search("*") } + assert_match "WARNING", stderr + end + + def test_search_relation_default_scope + skip if defined?(Mongoid) # for now + + Band.reindex + + _, stderr = capture_io { Band.search("*") } + assert_equal "", stderr + _, stderr = capture_io { Band.all.search("*") } assert_match "WARNING", stderr end end diff --git a/test/models/band.rb b/test/models/band.rb new file mode 100644 index 0000000..7476512 --- /dev/null +++ b/test/models/band.rb @@ -0,0 +1,3 @@ +class Band + searchkick +end diff --git a/test/support/activerecord.rb b/test/support/activerecord.rb index 37c0f2b..ec0833c 100644 --- a/test/support/activerecord.rb +++ b/test/support/activerecord.rb @@ -91,6 +91,10 @@ ActiveRecord::Migration.create_table :songs do |t| t.string :name end +ActiveRecord::Migration.create_table :bands do |t| + t.string :name +end + class Product < ActiveRecord::Base belongs_to :store end @@ -119,3 +123,7 @@ end class Song < ActiveRecord::Base end + +class Band < ActiveRecord::Base + default_scope { where(name: "Test") } +end diff --git a/test/support/mongoid.rb b/test/support/mongoid.rb index c589d73..7ab1355 100644 --- a/test/support/mongoid.rb +++ b/test/support/mongoid.rb @@ -66,3 +66,11 @@ class Song field :name end + +class Band + include Mongoid::Document + + field :name + + default_scope -> { where(name: "Test") } +end -- libgit2 0.21.0