Commit 5fc35e07bf20110da1e521853c88234c6a30f04c

Authored by Andrew Kane
1 parent 140231e8

Raise dangerous operations

lib/searchkick.rb
... ... @@ -22,6 +22,7 @@ module Searchkick
22 22 class MissingIndexError < StandardError; end
23 23 class UnsupportedVersionError < StandardError; end
24 24 class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
  25 + class DangerousOperation < StandardError; end
25 26  
26 27 class << self
27 28 attr_accessor :search_method_name
... ...
lib/searchkick/model.rb
... ... @@ -44,6 +44,9 @@ module Searchkick
44 44 end
45 45  
46 46 def reindex(options = {})
  47 + if respond_to?(:current_scope) && current_scope && current_scope.to_sql != default_scoped.to_sql
  48 + raise Searchkick::DangerousOperation, "Only call reindex on models, not relations"
  49 + end
47 50 searchkick_index.reindex_scope(searchkick_klass, options)
48 51 end
49 52  
... ...
test/index_test.rb
... ... @@ -103,6 +103,10 @@ class TestIndex &lt; Minitest::Test
103 103 assert_raises(Searchkick::InvalidQueryError) { Product.search(query: {}) }
104 104 end
105 105  
  106 + def test_reindex
  107 + assert_raises(Searchkick::DangerousOperation) { Product.where(id: [1, 2, 3]).reindex }
  108 + end
  109 +
106 110 if defined?(ActiveRecord)
107 111  
108 112 def test_transaction
... ...