Commit ef692e0a902bdf0cf2aadc971f64c176527f9e65

Authored by Jacek Tomaszewski
1 parent 6c51a2b1

Add ability to temporarily disable callbacks

README.md
... ... @@ -527,13 +527,19 @@ class Product < ActiveRecord::Base
527 527 end
528 528 ```
529 529  
530   -Turn off callbacks
531   -
  530 +Turn off callbacks permanently:
532 531 ```ruby
533 532 class Product < ActiveRecord::Base
534 533 searchkick callbacks: false
535 534 end
536 535 ```
  536 +or temporarily:
  537 +```ruby
  538 +Product.searchkick_disable! # use `Searchkick.disable!` for all models
  539 +ExpensiveProductsTask.execute
  540 +Product.searchkick_enable!
  541 +Product.reindex
  542 +```
537 543  
538 544 Eager load associations
539 545  
... ...
lib/searchkick.rb
1 1 require "tire"
2 2 require "searchkick/version"
  3 +require "searchkick/options"
3 4 require "searchkick/reindex"
4 5 require "searchkick/results"
5 6 require "searchkick/search"
... ...
lib/searchkick/model.rb
... ... @@ -3,11 +3,13 @@ module Searchkick
3 3  
4 4 def searchkick(options = {})
5 5 class_eval do
6   - cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass, :searchkick_index
  6 + cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass,
  7 + :searchkick_index
7 8  
8 9 class_variable_set :@@searchkick_options, options.dup
9 10 class_variable_set :@@searchkick_env, ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
10 11 class_variable_set :@@searchkick_klass, self
  12 + class_variable_set :@@searchkick_enabled, false
11 13  
12 14 # set index name
13 15 # TODO support proc
... ... @@ -18,19 +20,39 @@ module Searchkick
18 20 extend Searchkick::Reindex
19 21 include Searchkick::Similar
20 22  
21   - def reindex
22   - index = self.class.searchkick_index
23   - if destroyed?
24   - index.remove self
25   - else
26   - index.store self
  23 + def self.searchkick_enable!
  24 + unless class_variable_get(:@@searchkick_enabled)
  25 + class_variable_set :@@searchkick_enabled, true
  26 + after_save :reindex
  27 + after_destroy :reindex
  28 + end
  29 + end
  30 +
  31 + def self.searchkick_disable!
  32 + if class_variable_get(:@@searchkick_enabled)
  33 + class_variable_set :@@searchkick_enabled, false
  34 + skip_callback :save, :after, :reindex
  35 + skip_callback :destroy, :after, :reindex
27 36 end
28 37 end
29 38  
  39 + def self.searchkick_enabled?
  40 + !!class_variable_get(:@@searchkick_enabled) && Searchkick.enabled?
  41 + end
  42 +
30 43 unless options[:callbacks] == false
31   - # TODO ability to temporarily disable
32   - after_save :reindex
33   - after_destroy :reindex
  44 + self.searchkick_enable!
  45 + end
  46 +
  47 + def reindex
  48 + if self.class.searchkick_enabled?
  49 + index = self.class.searchkick_index
  50 + if destroyed?
  51 + index.remove self
  52 + else
  53 + index.store self
  54 + end
  55 + end
34 56 end
35 57  
36 58 def search_data
... ...
lib/searchkick/options.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +module Searchkick
  2 + @enabled = true
  3 +
  4 + def self.enable!
  5 + @enabled = true
  6 + end
  7 +
  8 + def self.disable!
  9 + @enabled = false
  10 + end
  11 +
  12 + def self.enabled?
  13 + !!@enabled
  14 + end
  15 +end
... ...
test/model_test.rb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +require_relative "test_helper"
  2 +
  3 +class TestModel < Minitest::Unit::TestCase
  4 +
  5 + def test_searchkick_disable
  6 + store_names ["product a"]
  7 +
  8 + Product.searchkick_disable!
  9 + assert !Product.searchkick_enabled?, 'searchkick has been disabled'
  10 +
  11 + store_names ["product b"]
  12 + assert_search "product", ["product a"]
  13 +
  14 + Product.searchkick_enable!
  15 + Product.reindex
  16 +
  17 + assert_search "product", ["product a", "product b"]
  18 + end
  19 +
  20 + def test_global_searchkick_disable
  21 + store_names ["product a"]
  22 +
  23 + Searchkick.disable!
  24 + assert !Searchkick.enabled?, 'searchkick has been disabled'
  25 +
  26 + store_names ["product b"]
  27 + assert_search "product", ["product a"]
  28 +
  29 + Searchkick.enable!
  30 + Product.reindex
  31 +
  32 + assert_search "product", ["product a", "product b"]
  33 + end
  34 +
  35 +end
... ...