From e1e4ea6dd1918b6901dfbe6b7d80ae5864d561f4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 1 Mar 2018 16:28:24 -0800 Subject: [PATCH] Removed legacy ways to do callbacks --- CHANGELOG.md | 2 ++ lib/searchkick.rb | 2 +- lib/searchkick/model.rb | 36 +++++++++++------------------------- test/model_test.rb | 8 +++----- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a5759f..b7a216a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - An `ArgumentError` is raised instead of a warning when options are incompatible with the `body` option - Removed `log` option from `boost_by`. Use `modifier: "ln2p"` instead. - Removed `unscoped_reindex_job` option (always `true` now) +- Removed `Model.enable_search_callbacks`, `Model.disable_search_callbacks`, and `Model.search_callbacks?` +- Removed `async` option from `record.reindex` - use `mode: :async` instead ## 2.5.1 [unreleased] diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 97b2dbe..f1c2dc7 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -115,7 +115,7 @@ module Searchkick end def self.callbacks? - Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled] + callbacks_value != false end def self.callbacks(value) diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 0060799..a86e84b 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -17,11 +17,8 @@ module Searchkick class_eval do cattr_reader :searchkick_options, :searchkick_klass - callbacks = options.key?(:callbacks) ? options[:callbacks] : true - class_variable_set :@@searchkick_options, options.dup class_variable_set :@@searchkick_klass, self - class_variable_set :@@searchkick_callbacks, callbacks class_variable_set :@@searchkick_index, options[:index_name] || (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") }) || [options.key?(:index_prefix) ? options[:index_prefix] : Searchkick.index_prefix, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") @@ -39,18 +36,6 @@ module Searchkick end alias_method :search_index, :searchkick_index unless method_defined?(:search_index) - def enable_search_callbacks - class_variable_set :@@searchkick_callbacks, true - end - - def disable_search_callbacks - class_variable_set :@@searchkick_callbacks, false - end - - def search_callbacks? - class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks? - end - def searchkick_reindex(method_name = nil, full: false, **options) scoped = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) || (respond_to?(:queryable) && queryable != unscoped.with_default_scope) @@ -79,23 +64,24 @@ module Searchkick end end - if respond_to?(:after_commit) - after_commit :reindex, if: -> { self.class.search_callbacks? } - elsif respond_to?(:after_save) - after_save :reindex, if: -> { self.class.search_callbacks? } - after_destroy :reindex, if: -> { self.class.search_callbacks? } + callbacks = options.key?(:callbacks) ? options[:callbacks] : true + if callbacks + if respond_to?(:after_commit) + after_commit :reindex, if: -> { Searchkick.callbacks? } + elsif respond_to?(:after_save) + after_save :reindex, if: -> { Searchkick.callbacks? } + after_destroy :reindex, if: -> { Searchkick.callbacks? } + end end - def reindex(method_name = nil, refresh: false, async: false, mode: nil) + def reindex(method_name = nil, refresh: false, mode: nil) klass_options = self.class.searchkick_index.options if mode.nil? mode = - if async - :async - elsif Searchkick.callbacks_value + if Searchkick.callbacks_value Searchkick.callbacks_value - elsif klass_options.key?(:callbacks) + else klass_options[:callbacks] end end diff --git a/test/model_test.rb b/test/model_test.rb index f25f117..168011e 100644 --- a/test/model_test.rb +++ b/test/model_test.rb @@ -4,13 +4,11 @@ class ModelTest < Minitest::Test def test_disable_callbacks_model store_names ["product a"] - Product.disable_search_callbacks - assert !Product.search_callbacks? - - store_names ["product b"] + Searchkick.callbacks(false) do + store_names ["product b"] + end assert_search "product", ["product a"] - Product.enable_search_callbacks Product.reindex assert_search "product", ["product a", "product b"] -- libgit2 0.21.0