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