From c15369e30ef9d458d23f803ff81afcd0dae550aa Mon Sep 17 00:00:00 2001 From: Kirill Shnurov Date: Mon, 19 Sep 2022 16:19:02 +0300 Subject: [PATCH] allow to skip reindex --- README.md | 7 ++++--- lib/searchkick/model.rb | 10 +++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ddd8d44d..480531aa 100644 --- a/README.md +++ b/README.md @@ -2058,10 +2058,11 @@ Reindex conditionally ```ruby class Product < ApplicationRecord - searchkick callbacks: false + searchkick - # add the callbacks manually - after_commit :reindex, if: -> (model) { model.previous_changes.key?("name") } # use your own condition + def skip_searchkick_reindex? + persisted? && saved_changes.keys == ['updated_at'] + end end ``` diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 41aa4875..1145b819 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -46,6 +46,10 @@ def search_data def should_index? true end unless base.method_defined?(:should_index?) + + def skip_searchkick_reindex? + false + end end class_eval do @@ -99,10 +103,10 @@ def searchkick_index_name # always add callbacks, even when callbacks is false # so Model.callbacks block can be used if respond_to?(:after_commit) - after_commit :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } + after_commit :reindex, if: proc { Searchkick.callbacks?(default: callbacks) && !skip_searchkick_reindex? } elsif respond_to?(:after_save) - after_save :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } - after_destroy :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } + after_save :reindex, if: proc { Searchkick.callbacks?(default: callbacks) && !skip_searchkick_reindex? } + after_destroy :reindex, if: proc { Searchkick.callbacks?(default: callbacks) && !skip_searchkick_reindex? } end end end