Commit e1e4ea6dd1918b6901dfbe6b7d80ae5864d561f4

Authored by Andrew
1 parent bf00a7d8

Removed legacy ways to do callbacks

CHANGELOG.md
... ... @@ -9,6 +9,8 @@
9 9 - An `ArgumentError` is raised instead of a warning when options are incompatible with the `body` option
10 10 - Removed `log` option from `boost_by`. Use `modifier: "ln2p"` instead.
11 11 - Removed `unscoped_reindex_job` option (always `true` now)
  12 +- Removed `Model.enable_search_callbacks`, `Model.disable_search_callbacks`, and `Model.search_callbacks?`
  13 +- Removed `async` option from `record.reindex` - use `mode: :async` instead
12 14  
13 15 ## 2.5.1 [unreleased]
14 16  
... ...
lib/searchkick.rb
... ... @@ -115,7 +115,7 @@ module Searchkick
115 115 end
116 116  
117 117 def self.callbacks?
118   - Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled]
  118 + callbacks_value != false
119 119 end
120 120  
121 121 def self.callbacks(value)
... ...
lib/searchkick/model.rb
... ... @@ -17,11 +17,8 @@ module Searchkick
17 17 class_eval do
18 18 cattr_reader :searchkick_options, :searchkick_klass
19 19  
20   - callbacks = options.key?(:callbacks) ? options[:callbacks] : true
21   -
22 20 class_variable_set :@@searchkick_options, options.dup
23 21 class_variable_set :@@searchkick_klass, self
24   - class_variable_set :@@searchkick_callbacks, callbacks
25 22 class_variable_set :@@searchkick_index, options[:index_name] ||
26 23 (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") }) ||
27 24 [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
39 36 end
40 37 alias_method :search_index, :searchkick_index unless method_defined?(:search_index)
41 38  
42   - def enable_search_callbacks
43   - class_variable_set :@@searchkick_callbacks, true
44   - end
45   -
46   - def disable_search_callbacks
47   - class_variable_set :@@searchkick_callbacks, false
48   - end
49   -
50   - def search_callbacks?
51   - class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks?
52   - end
53   -
54 39 def searchkick_reindex(method_name = nil, full: false, **options)
55 40 scoped = (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) ||
56 41 (respond_to?(:queryable) && queryable != unscoped.with_default_scope)
... ... @@ -79,23 +64,24 @@ module Searchkick
79 64 end
80 65 end
81 66  
82   - if respond_to?(:after_commit)
83   - after_commit :reindex, if: -> { self.class.search_callbacks? }
84   - elsif respond_to?(:after_save)
85   - after_save :reindex, if: -> { self.class.search_callbacks? }
86   - after_destroy :reindex, if: -> { self.class.search_callbacks? }
  67 + callbacks = options.key?(:callbacks) ? options[:callbacks] : true
  68 + if callbacks
  69 + if respond_to?(:after_commit)
  70 + after_commit :reindex, if: -> { Searchkick.callbacks? }
  71 + elsif respond_to?(:after_save)
  72 + after_save :reindex, if: -> { Searchkick.callbacks? }
  73 + after_destroy :reindex, if: -> { Searchkick.callbacks? }
  74 + end
87 75 end
88 76  
89   - def reindex(method_name = nil, refresh: false, async: false, mode: nil)
  77 + def reindex(method_name = nil, refresh: false, mode: nil)
90 78 klass_options = self.class.searchkick_index.options
91 79  
92 80 if mode.nil?
93 81 mode =
94   - if async
95   - :async
96   - elsif Searchkick.callbacks_value
  82 + if Searchkick.callbacks_value
97 83 Searchkick.callbacks_value
98   - elsif klass_options.key?(:callbacks)
  84 + else
99 85 klass_options[:callbacks]
100 86 end
101 87 end
... ...
test/model_test.rb
... ... @@ -4,13 +4,11 @@ class ModelTest < Minitest::Test
4 4 def test_disable_callbacks_model
5 5 store_names ["product a"]
6 6  
7   - Product.disable_search_callbacks
8   - assert !Product.search_callbacks?
9   -
10   - store_names ["product b"]
  7 + Searchkick.callbacks(false) do
  8 + store_names ["product b"]
  9 + end
11 10 assert_search "product", ["product a"]
12 11  
13   - Product.enable_search_callbacks
14 12 Product.reindex
15 13  
16 14 assert_search "product", ["product a", "product b"]
... ...