Commit 8992dcd0f277ca383f72172b2feb3ef02d1eba9f

Authored by Eric Hankins
1 parent 112183c2

Make `Searchkick.disable_callbacks` threadsafe

Switch from a class variable to a fiber-local variable.
Showing 2 changed files with 6 additions and 5 deletions   Show diff stats
lib/searchkick.rb
@@ -24,13 +24,11 @@ module Searchkick @@ -24,13 +24,11 @@ module Searchkick
24 class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end 24 class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
25 25
26 class << self 26 class << self
27 - attr_accessor :callbacks  
28 attr_accessor :search_method_name 27 attr_accessor :search_method_name
29 attr_accessor :wordnet_path 28 attr_accessor :wordnet_path
30 attr_accessor :timeout 29 attr_accessor :timeout
31 attr_accessor :models 30 attr_accessor :models
32 end 31 end
33 - self.callbacks = true  
34 self.search_method_name = :search 32 self.search_method_name = :search
35 self.wordnet_path = "/var/lib/wn_s.pl" 33 self.wordnet_path = "/var/lib/wn_s.pl"
36 self.timeout = 10 34 self.timeout = 10
@@ -53,15 +51,15 @@ module Searchkick @@ -53,15 +51,15 @@ module Searchkick
53 end 51 end
54 52
55 def self.enable_callbacks 53 def self.enable_callbacks
56 - self.callbacks = true 54 + Thread.current[:searchkick_callbacks_enabled] = true
57 end 55 end
58 56
59 def self.disable_callbacks 57 def self.disable_callbacks
60 - self.callbacks = false 58 + Thread.current[:searchkick_callbacks_enabled] = false
61 end 59 end
62 60
63 def self.callbacks? 61 def self.callbacks?
64 - callbacks 62 + Thread.current[:searchkick_callbacks_enabled].nil? || Thread.current[:searchkick_callbacks_enabled]
65 end 63 end
66 64
67 def self.env 65 def self.env
test/model_test.rb
@@ -18,6 +18,9 @@ class TestModel &lt; Minitest::Test @@ -18,6 +18,9 @@ class TestModel &lt; Minitest::Test
18 end 18 end
19 19
20 def test_disable_callbacks_global 20 def test_disable_callbacks_global
  21 + # make sure callbacks default to on
  22 + assert Searchkick.callbacks?
  23 +
21 store_names ["product a"] 24 store_names ["product a"]
22 25
23 Searchkick.disable_callbacks 26 Searchkick.disable_callbacks