Commit 805f7efb02eab3f4831bbb7580b9d8441c02f6f1

Authored by Andrew Kane
2 parents 94f7ff37 d43b42b0

Added ability to temporarily disable callbacks - closes #35

1 ## 0.4.2 [unreleased] 1 ## 0.4.2 [unreleased]
2 2
3 - Added `should_index?` method to control which records are indexed 3 - Added `should_index?` method to control which records are indexed
  4 +- Added ability to temporarily disable callbacks
4 5
5 ## 0.4.1 6 ## 0.4.1
6 7
@@ -537,7 +537,7 @@ class Product < ActiveRecord::Base @@ -537,7 +537,7 @@ class Product < ActiveRecord::Base
537 end 537 end
538 ``` 538 ```
539 539
540 -Turn off callbacks 540 +Turn off callbacks permanently
541 541
542 ```ruby 542 ```ruby
543 class Product < ActiveRecord::Base 543 class Product < ActiveRecord::Base
@@ -545,6 +545,15 @@ class Product &lt; ActiveRecord::Base @@ -545,6 +545,15 @@ class Product &lt; ActiveRecord::Base
545 end 545 end
546 ``` 546 ```
547 547
  548 +or temporarily [master branch]
  549 +
  550 +```ruby
  551 +Product.disable_search_callbacks # use Searchkick.disable_callbacks for all models
  552 +ExpensiveProductsTask.execute
  553 +Product.enable_search_callbacks
  554 +Product.reindex
  555 +```
  556 +
548 Eager load associations 557 Eager load associations
549 558
550 ```ruby 559 ```ruby
lib/searchkick.rb
@@ -8,6 +8,22 @@ require &quot;searchkick/model&quot; @@ -8,6 +8,22 @@ require &quot;searchkick/model&quot;
8 require "searchkick/tasks" 8 require "searchkick/tasks"
9 require "searchkick/logger" if defined?(Rails) 9 require "searchkick/logger" if defined?(Rails)
10 10
  11 +module Searchkick
  12 + @callbacks = true
  13 +
  14 + def self.enable_callbacks
  15 + @callbacks = true
  16 + end
  17 +
  18 + def self.disable_callbacks
  19 + @callbacks = false
  20 + end
  21 +
  22 + def self.callbacks?
  23 + @callbacks
  24 + end
  25 +end
  26 +
11 # TODO find better ActiveModel hook 27 # TODO find better ActiveModel hook
12 ActiveModel::Callbacks.send(:include, Searchkick::Model) 28 ActiveModel::Callbacks.send(:include, Searchkick::Model)
13 ActiveRecord::Base.send(:extend, Searchkick::Model) if defined?(ActiveRecord) 29 ActiveRecord::Base.send(:extend, Searchkick::Model) if defined?(ActiveRecord)
lib/searchkick/model.rb
@@ -8,6 +8,7 @@ module Searchkick @@ -8,6 +8,7 @@ module Searchkick
8 class_variable_set :@@searchkick_options, options.dup 8 class_variable_set :@@searchkick_options, options.dup
9 class_variable_set :@@searchkick_env, ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development" 9 class_variable_set :@@searchkick_env, ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
10 class_variable_set :@@searchkick_klass, self 10 class_variable_set :@@searchkick_klass, self
  11 + class_variable_set :@@searchkick_callbacks, options[:callbacks] != false
11 12
12 # set index name 13 # set index name
13 # TODO support proc 14 # TODO support proc
@@ -18,25 +19,36 @@ module Searchkick @@ -18,25 +19,36 @@ module Searchkick
18 extend Searchkick::Reindex 19 extend Searchkick::Reindex
19 include Searchkick::Similar 20 include Searchkick::Similar
20 21
  22 + after_save :reindex
  23 + after_destroy :reindex
  24 +
  25 + def self.enable_search_callbacks
  26 + class_variable_set :@@searchkick_callbacks, true
  27 + end
  28 +
  29 + def self.disable_search_callbacks
  30 + class_variable_set :@@searchkick_callbacks, false
  31 + end
  32 +
  33 + def self.search_callbacks?
  34 + class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks?
  35 + end
  36 +
21 def should_index? 37 def should_index?
22 true 38 true
23 end 39 end
24 40
25 def reindex 41 def reindex
26 - index = self.class.searchkick_index  
27 - if destroyed? or !should_index?  
28 - index.remove self  
29 - else  
30 - index.store self 42 + if self.class.search_callbacks?
  43 + index = self.class.searchkick_index
  44 + if destroyed? or !should_index?
  45 + index.remove self
  46 + else
  47 + index.store self
  48 + end
31 end 49 end
32 end 50 end
33 51
34 - unless options[:callbacks] == false  
35 - # TODO ability to temporarily disable  
36 - after_save :reindex  
37 - after_destroy :reindex  
38 - end  
39 -  
40 def search_data 52 def search_data
41 respond_to?(:to_hash) ? to_hash : serializable_hash 53 respond_to?(:to_hash) ? to_hash : serializable_hash
42 end 54 end
test/model_test.rb 0 → 100644
@@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
  1 +require_relative "test_helper"
  2 +
  3 +class TestModel < Minitest::Unit::TestCase
  4 +
  5 + def test_disable_callbacks_model
  6 + store_names ["product a"]
  7 +
  8 + Product.disable_search_callbacks
  9 + assert !Product.search_callbacks?
  10 +
  11 + store_names ["product b"]
  12 + assert_search "product", ["product a"]
  13 +
  14 + Product.enable_search_callbacks
  15 + Product.reindex
  16 +
  17 + assert_search "product", ["product a", "product b"]
  18 + end
  19 +
  20 + def test_disable_callbacks_global
  21 + store_names ["product a"]
  22 +
  23 + Searchkick.disable_callbacks
  24 + assert !Searchkick.callbacks?
  25 +
  26 + store_names ["product b"]
  27 + assert_search "product", ["product a"]
  28 +
  29 + Searchkick.enable_callbacks
  30 + Product.reindex
  31 +
  32 + assert_search "product", ["product a", "product b"]
  33 + end
  34 +
  35 +end