Commit ffa3f794837a0ad7fdfc89ef3bd125e5259e60ec
1 parent
3df58036
Exists in
master
and in
19 other branches
Better callback handling [skip ci]
Showing
4 changed files
with
13 additions
and
13 deletions
Show diff stats
lib/searchkick.rb
... | ... | @@ -125,8 +125,12 @@ module Searchkick |
125 | 125 | self.callbacks_value = false |
126 | 126 | end |
127 | 127 | |
128 | - def self.callbacks? | |
129 | - callbacks_value != false | |
128 | + def self.callbacks?(default: true) | |
129 | + if callbacks_value.nil? | |
130 | + default | |
131 | + else | |
132 | + callbacks_value != false | |
133 | + end | |
130 | 134 | end |
131 | 135 | |
132 | 136 | def self.callbacks(value) | ... | ... |
lib/searchkick/index.rb
lib/searchkick/model.rb
... | ... | @@ -65,13 +65,13 @@ module Searchkick |
65 | 65 | end |
66 | 66 | end |
67 | 67 | |
68 | - if callbacks | |
69 | - if respond_to?(:after_commit) | |
70 | - after_commit :reindex | |
71 | - elsif respond_to?(:after_save) | |
72 | - after_save :reindex | |
73 | - after_destroy :reindex | |
74 | - end | |
68 | + # always add callbacks, even when callbacks is false | |
69 | + # so Model.callbacks block can be used | |
70 | + if respond_to?(:after_commit) | |
71 | + after_commit :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } | |
72 | + elsif respond_to?(:after_save) | |
73 | + after_save :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } | |
74 | + after_destroy :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } | |
75 | 75 | end |
76 | 76 | |
77 | 77 | def reindex(method_name = nil, **options) | ... | ... |
lib/searchkick/record_indexer.rb