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,8 +125,12 @@ module Searchkick | ||
125 | self.callbacks_value = false | 125 | self.callbacks_value = false |
126 | end | 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 | end | 134 | end |
131 | 135 | ||
132 | def self.callbacks(value) | 136 | def self.callbacks(value) |
lib/searchkick/index.rb
@@ -179,8 +179,6 @@ module Searchkick | @@ -179,8 +179,6 @@ module Searchkick | ||
179 | # reindex | 179 | # reindex |
180 | 180 | ||
181 | def reindex(scope, method_name, scoped:, full: false, **options) | 181 | def reindex(scope, method_name, scoped:, full: false, **options) |
182 | - return unless Searchkick.callbacks? | ||
183 | - | ||
184 | refresh = options.fetch(:refresh, !scoped) | 182 | refresh = options.fetch(:refresh, !scoped) |
185 | 183 | ||
186 | if method_name | 184 | if method_name |
lib/searchkick/model.rb
@@ -65,13 +65,13 @@ module Searchkick | @@ -65,13 +65,13 @@ module Searchkick | ||
65 | end | 65 | end |
66 | end | 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 | end | 75 | end |
76 | 76 | ||
77 | def reindex(method_name = nil, **options) | 77 | def reindex(method_name = nil, **options) |
lib/searchkick/record_indexer.rb
@@ -8,8 +8,6 @@ module Searchkick | @@ -8,8 +8,6 @@ module Searchkick | ||
8 | end | 8 | end |
9 | 9 | ||
10 | def reindex(method_name = nil, refresh: false, mode: nil) | 10 | def reindex(method_name = nil, refresh: false, mode: nil) |
11 | - return unless Searchkick.callbacks? | ||
12 | - | ||
13 | unless [true, nil, :async, :queue].include?(mode) | 11 | unless [true, nil, :async, :queue].include?(mode) |
14 | raise ArgumentError, "Invalid value for mode" | 12 | raise ArgumentError, "Invalid value for mode" |
15 | end | 13 | end |