From 6a70e1c752612334d0ec4ac188e9cf83b8dc3686 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 10 Aug 2018 10:21:59 -0700 Subject: [PATCH] Fixed deletes with routing and callbacks - #1182 --- CHANGELOG.md | 4 ++++ lib/searchkick/record_indexer.rb | 5 ++++- lib/searchkick/reindex_v2_job.rb | 7 ++++++- test/routing_test.rb | 9 +++++++++ test/test_helper.rb | 4 ++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd94a90..8f8f87c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.1.2 [unreleased] + +- Fixed deletes with routing and `async` callbacks + ## 3.1.1 - Added per-field misspellings diff --git a/lib/searchkick/record_indexer.rb b/lib/searchkick/record_indexer.rb index 0849370..68c3d5d 100644 --- a/lib/searchkick/record_indexer.rb +++ b/lib/searchkick/record_indexer.rb @@ -26,10 +26,13 @@ module Searchkick raise Searchkick::Error, "Active Job not found" end + routing = record.search_routing if record.destroyed? && record.respond_to?(:search_routing) + Searchkick::ReindexV2Job.perform_later( record.class.name, record.id.to_s, - method_name ? method_name.to_s : nil + method_name ? method_name.to_s : nil, + routing: routing ) else # bulk, inline/true/nil reindex_record(method_name) diff --git a/lib/searchkick/reindex_v2_job.rb b/lib/searchkick/reindex_v2_job.rb index 405d8da..03c8bb8 100644 --- a/lib/searchkick/reindex_v2_job.rb +++ b/lib/searchkick/reindex_v2_job.rb @@ -9,7 +9,7 @@ module Searchkick queue_as { Searchkick.queue_name } - def perform(klass, id, method_name = nil) + def perform(klass, id, method_name = nil, routing: nil) model = klass.constantize record = begin @@ -28,6 +28,11 @@ module Searchkick unless record record = model.new record.id = id + if routing + record.define_singleton_method(:search_routing) do + routing + end + end end RecordIndexer.new(record).reindex(method_name, mode: :inline) diff --git a/test/routing_test.rb b/test/routing_test.rb index 10cdfa5..b996af1 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -20,4 +20,13 @@ class RoutingTest < Minitest::Test store_names ["Dollar Tree"], Store assert_search "*", ["Dollar Tree"], {routing: "Boom"}, Store end + + def test_routing_async + skip unless defined?(ActiveJob) + + with_options(Song, routing: true, callbacks: :async) do + store_names ["Dollar Tree"], Song + Song.destroy_all + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index d0bc67e..505942f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -529,6 +529,10 @@ end class Song searchkick + + def search_routing + name + end end Product.searchkick_index.delete if Product.searchkick_index.exists? -- libgit2 0.21.0