Commit e1e4ea6dd1918b6901dfbe6b7d80ae5864d561f4

Authored by Andrew
1 parent bf00a7d8

Removed legacy ways to do callbacks

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