Commit 7f724c219bde988682caa4e90ed5f0141bfab2dc

Authored by Andrew Kane
1 parent 321b8e2c

Use after_commit hook to protect against data inconsistencies - closes #111

README.md
... ... @@ -657,25 +657,20 @@ class Product < ActiveRecord::Base
657 657 end
658 658 ```
659 659  
660   -Reindex conditionally
  660 +Asynchronous reindexing
  661 +
  662 +**Note:** Use `after_commit` for ActiveRecord and `after_save` and `after_destroy` for Mongoid
661 663  
662 664 ```ruby
663 665 class Product < ActiveRecord::Base
664 666 searchkick callbacks: false
665 667  
666 668 # add the callbacks manually
667   - after_save :reindex, if: proc{|model| model.name_changed? } # use your own condition
668   - after_destroy :reindex
669   -end
670   -```
671 669  
672   -Asynchronous reindexing
673   -
674   -```ruby
675   -class Product < ActiveRecord::Base
676   - searchkick callbacks: false
  670 + # ActiveRecord - one callback
  671 + after_commit :reindex_async
677 672  
678   - # add the callbacks manually
  673 + # Mongoid - two callbacks
679 674 after_save :reindex_async
680 675 after_destroy :reindex_async
681 676  
... ... @@ -686,6 +681,20 @@ class Product &lt; ActiveRecord::Base
686 681 end
687 682 ```
688 683  
  684 +Reindex conditionally
  685 +
  686 +**Note:** Use with caution - (transaction rollbacks can cause data inconstencies)[https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/README.md#custom-callbacks] with this feature
  687 +
  688 +```ruby
  689 +class Product < ActiveRecord::Base
  690 + searchkick callbacks: false
  691 +
  692 + # add the callbacks manually
  693 + after_save :reindex, if: proc{|model| model.name_changed? } # use your own condition
  694 + after_destroy :reindex
  695 +end
  696 +```
  697 +
689 698 Reindex all models (Rails only)
690 699  
691 700 ```sh
... ...
lib/searchkick/model.rb
... ... @@ -19,8 +19,12 @@ module Searchkick
19 19 extend Searchkick::Reindex
20 20 include Searchkick::Similar
21 21  
22   - after_save :reindex, if: proc { self.class.search_callbacks? }
23   - after_destroy :reindex, if: proc { self.class.search_callbacks? }
  22 + if respond_to?(:after_commit)
  23 + after_commit :reindex, if: proc{ self.class.search_callbacks? }
  24 + else
  25 + after_save :reindex, if: proc{ self.class.search_callbacks? }
  26 + after_destroy :reindex, if: proc{ self.class.search_callbacks? }
  27 + end
24 28  
25 29 def self.enable_search_callbacks
26 30 class_variable_set :@@searchkick_callbacks, true
... ...
test/index_test.rb
... ... @@ -32,4 +32,17 @@ class TestIndex &lt; Minitest::Unit::TestCase
32 32 assert_equal ["Dollar Tree"], Store.search(query: {match: {name: "Dollar Tree"}}).map(&:name)
33 33 end
34 34  
  35 + if defined?(ActiveRecord)
  36 +
  37 + def test_transaction
  38 + Product.transaction do
  39 + store_names ["Product A"]
  40 + raise ActiveRecord::Rollback
  41 + end
  42 +
  43 + assert_search "product", []
  44 + end
  45 +
  46 + end
  47 +
35 48 end
... ...