Description
Is your feature request related to a problem? Please describe.
Currently the suggested way to perform the reindexing conditionally is
class Product < ApplicationRecord
searchkick callbacks: false
# add the callbacks manually
after_commit :reindex, if: -> (model) { model.previous_changes.key?("name") } # use your own condition
end
The problem is that this approach is likely about the copy/paste action from Searchkick internals into the host application. The client is forced to investigate the sources of Searchkick, understand in details how and why it uses after_commit
callback, paste the same into own application, apply the customizations, and as it always happen with code duplication - keep tracking the upstream sources in order to catch & reflect the breaking changes.
For instance, the suggested conditional reindexing approach from above relies on :inline
callbacks mode under the hood. If a client normally uses :async
and just wants to skip reindexing sometimes - it have to do something like:
class Product < ApplicationRecord
searchkick callbacks: false
# add the callbacks manually
after_commit(if: -> (model) { model.previous_changes.key?("name") } { reindex(mode: :async) }
end
And still some important parts or Searchkick functionality might be broken now (or will be broken later after Searchkick upgrade) - for instance the global callbacks mode. This way the full-weight suggestion turns into:
class Product < ApplicationRecord
searchkick callbacks: false
# add the callbacks manually
after_commit(if: -> (model) { Searchkick.callbacks? && model.previous_changes.key?("name") } { reindex(mode: :async) }
end
And still it does not fully respect the global callbacks mode such as Searchkick.callbacks(:bulk) do; end
..
Describe the solution you'd like
Let's introduce the possibility to define callbacks mode as lambda for the model as follows:
class Product < ApplicationRecord
searchkick callbacks: ->{ searchkick_callbacks_mode }
def searchkick_callbacks_mode
return false unless previous_changes.key?("name")
return :inline if urgent?
:async
end
end
It will be evaluated within generic after_commit
Searchkick callback.
I'm ready to provide PR once the feature will be accepted in general by code owners )