Commit 6a70e1c752612334d0ec4ac188e9cf83b8dc3686

Authored by Andrew
1 parent 59ac07b4

Fixed deletes with routing and callbacks - #1182

CHANGELOG.md
  1 +## 3.1.2 [unreleased]
  2 +
  3 +- Fixed deletes with routing and `async` callbacks
  4 +
1 5 ## 3.1.1
2 6  
3 7 - Added per-field misspellings
... ...
lib/searchkick/record_indexer.rb
... ... @@ -26,10 +26,13 @@ module Searchkick
26 26 raise Searchkick::Error, "Active Job not found"
27 27 end
28 28  
  29 + routing = record.search_routing if record.destroyed? && record.respond_to?(:search_routing)
  30 +
29 31 Searchkick::ReindexV2Job.perform_later(
30 32 record.class.name,
31 33 record.id.to_s,
32   - method_name ? method_name.to_s : nil
  34 + method_name ? method_name.to_s : nil,
  35 + routing: routing
33 36 )
34 37 else # bulk, inline/true/nil
35 38 reindex_record(method_name)
... ...
lib/searchkick/reindex_v2_job.rb
... ... @@ -9,7 +9,7 @@ module Searchkick
9 9  
10 10 queue_as { Searchkick.queue_name }
11 11  
12   - def perform(klass, id, method_name = nil)
  12 + def perform(klass, id, method_name = nil, routing: nil)
13 13 model = klass.constantize
14 14 record =
15 15 begin
... ... @@ -28,6 +28,11 @@ module Searchkick
28 28 unless record
29 29 record = model.new
30 30 record.id = id
  31 + if routing
  32 + record.define_singleton_method(:search_routing) do
  33 + routing
  34 + end
  35 + end
31 36 end
32 37  
33 38 RecordIndexer.new(record).reindex(method_name, mode: :inline)
... ...
test/routing_test.rb
... ... @@ -20,4 +20,13 @@ class RoutingTest < Minitest::Test
20 20 store_names ["Dollar Tree"], Store
21 21 assert_search "*", ["Dollar Tree"], {routing: "Boom"}, Store
22 22 end
  23 +
  24 + def test_routing_async
  25 + skip unless defined?(ActiveJob)
  26 +
  27 + with_options(Song, routing: true, callbacks: :async) do
  28 + store_names ["Dollar Tree"], Song
  29 + Song.destroy_all
  30 + end
  31 + end
23 32 end
... ...
test/test_helper.rb
... ... @@ -529,6 +529,10 @@ end
529 529  
530 530 class Song
531 531 searchkick
  532 +
  533 + def search_routing
  534 + name
  535 + end
532 536 end
533 537  
534 538 Product.searchkick_index.delete if Product.searchkick_index.exists?
... ...