Commit 039c5d273eef91eeff0cfad49ad2279feef385e8

Authored by Andrew Kane
1 parent 0255fb80

Better relation check

Showing 2 changed files with 14 additions and 3 deletions   Show diff stats
lib/searchkick.rb
... ... @@ -253,6 +253,18 @@ module Searchkick
253 253 }
254 254 end
255 255 end
  256 +
  257 + # private
  258 + # methods are forwarded to base class
  259 + # this check to see if scope exists on that class
  260 + # it's a bit tricky, but this seems to work
  261 + def self.relation?(klass)
  262 + if klass.respond_to?(:current_scope)
  263 + !klass.current_scope.nil?
  264 + elsif defined?(Mongoid::Threaded)
  265 + !Mongoid::Threaded.current_scope(klass).nil?
  266 + end
  267 + end
256 268 end
257 269  
258 270 # TODO find better ActiveModel hook
... ...
lib/searchkick/model.rb
... ... @@ -42,8 +42,7 @@ module Searchkick
42 42 class << self
43 43 def searchkick_search(term = "*", **options, &block)
44 44 # TODO throw error in next major version
45   - relation = respond_to?(:current_scope) ? !current_scope.nil? : !Mongoid::Threaded.current_scope(self).nil?
46   - Searchkick.warn("calling search on a relation is deprecated") if relation
  45 + Searchkick.warn("calling search on a relation is deprecated") if Searchkick.relation?(self)
47 46  
48 47 Searchkick.search(term, model: self, **options, &block)
49 48 end
... ... @@ -58,7 +57,7 @@ module Searchkick
58 57 alias_method :search_index, :searchkick_index unless method_defined?(:search_index)
59 58  
60 59 def searchkick_reindex(method_name = nil, **options)
61   - # TODO properly check relation like in searchkick_search method
  60 + # TODO use Searchkick.relation?
62 61 relation = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) ||
63 62 (respond_to?(:queryable) && queryable != unscoped.with_default_scope)
64 63  
... ...