From ffa3f794837a0ad7fdfc89ef3bd125e5259e60ec Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 2 Mar 2018 17:37:35 -0800 Subject: [PATCH] Better callback handling [skip ci] --- lib/searchkick.rb | 8 ++++++-- lib/searchkick/index.rb | 2 -- lib/searchkick/model.rb | 14 +++++++------- lib/searchkick/record_indexer.rb | 2 -- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 4fd68d2..b872a86 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -125,8 +125,12 @@ module Searchkick self.callbacks_value = false end - def self.callbacks? - callbacks_value != false + def self.callbacks?(default: true) + if callbacks_value.nil? + default + else + callbacks_value != false + end end def self.callbacks(value) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index bbf25e9..4f831aa 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -179,8 +179,6 @@ module Searchkick # reindex def reindex(scope, method_name, scoped:, full: false, **options) - return unless Searchkick.callbacks? - refresh = options.fetch(:refresh, !scoped) if method_name diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 865ec69..445440b 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -65,13 +65,13 @@ module Searchkick end end - if callbacks - if respond_to?(:after_commit) - after_commit :reindex - elsif respond_to?(:after_save) - after_save :reindex - after_destroy :reindex - end + # always add callbacks, even when callbacks is false + # so Model.callbacks block can be used + if respond_to?(:after_commit) + after_commit :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } + elsif respond_to?(:after_save) + after_save :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } + after_destroy :reindex, if: -> { Searchkick.callbacks?(default: callbacks) } end def reindex(method_name = nil, **options) diff --git a/lib/searchkick/record_indexer.rb b/lib/searchkick/record_indexer.rb index 96631e9..ebf6fdb 100644 --- a/lib/searchkick/record_indexer.rb +++ b/lib/searchkick/record_indexer.rb @@ -8,8 +8,6 @@ module Searchkick end def reindex(method_name = nil, refresh: false, mode: nil) - return unless Searchkick.callbacks? - unless [true, nil, :async, :queue].include?(mode) raise ArgumentError, "Invalid value for mode" end -- libgit2 0.21.0