Commit 039c5d273eef91eeff0cfad49ad2279feef385e8
1 parent
0255fb80
Exists in
master
and in
8 other branches
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,6 +253,18 @@ module Searchkick | ||
253 | } | 253 | } |
254 | end | 254 | end |
255 | end | 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 | end | 268 | end |
257 | 269 | ||
258 | # TODO find better ActiveModel hook | 270 | # TODO find better ActiveModel hook |
lib/searchkick/model.rb
@@ -42,8 +42,7 @@ module Searchkick | @@ -42,8 +42,7 @@ module Searchkick | ||
42 | class << self | 42 | class << self |
43 | def searchkick_search(term = "*", **options, &block) | 43 | def searchkick_search(term = "*", **options, &block) |
44 | # TODO throw error in next major version | 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 | Searchkick.search(term, model: self, **options, &block) | 47 | Searchkick.search(term, model: self, **options, &block) |
49 | end | 48 | end |
@@ -58,7 +57,7 @@ module Searchkick | @@ -58,7 +57,7 @@ module Searchkick | ||
58 | alias_method :search_index, :searchkick_index unless method_defined?(:search_index) | 57 | alias_method :search_index, :searchkick_index unless method_defined?(:search_index) |
59 | 58 | ||
60 | def searchkick_reindex(method_name = nil, **options) | 59 | def searchkick_reindex(method_name = nil, **options) |
61 | - # TODO properly check relation like in searchkick_search method | 60 | + # TODO use Searchkick.relation? |
62 | relation = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || | 61 | relation = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || |
63 | (respond_to?(:queryable) && queryable != unscoped.with_default_scope) | 62 | (respond_to?(:queryable) && queryable != unscoped.with_default_scope) |
64 | 63 |