Skip to content

Commit

Permalink
Run all callbacks for create/update in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
sl4vr committed Dec 10, 2021
1 parent 22b4d6c commit 192e71b
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions lib/chewy/index/observe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ class Index
module Observe
extend ActiveSupport::Concern

class Callback
def initialize(executable, **filters)
@executable = executable
@if_filter = filters[:if]
@unless_filter = filters[:unless]
end

def call(context)
return if @if_filter && !eval_filter(@if_filter, context)
return if @unless_filter && eval_filter(@unless_filter, context)

context.instance_eval(&@executable)
end

private

def eval_filter(filter, context)
case filter
when Symbol then context.instance_exec(&filter.to_proc)
when Proc then context.instance_exec(&filter)
else filter
end
end
end

module Helpers
def update_proc(index_name, *args, &block)
options = args.extract_options!
Expand Down Expand Up @@ -48,14 +73,30 @@ def extract_callback_options!(args)
extend Helpers

module ActiveRecordMethods
def self.extend_object(base)
super

base.class_attribute :chewy_callbacks, default: []

base.define_method :run_chewy_callbacks do
chewy_callbacks.each { |callback| callback.call(self) }
end
end

def update_index(type_name, *args, &block)
callback_options = Observe.extract_callback_options!(args)
update_proc = Observe.update_proc(type_name, *args, &block)

self.chewy_callbacks =
chewy_callbacks.dup << Chewy::Index::Observe::Callback.new(update_proc, callback_options)

# Set Chewy callbacks along with destroy callbacks here
# because here we have actual Chewy.use_after_commit_callbacks
if Chewy.use_after_commit_callbacks
after_commit(**callback_options, &update_proc)
after_commit(:run_chewy_callbacks, on: %i[create update])
after_commit(on: :destroy, **callback_options, &update_proc)
else
after_save(**callback_options, &update_proc)
after_save(:run_chewy_callbacks)
after_destroy(**callback_options, &update_proc)
end
end
Expand Down

0 comments on commit 192e71b

Please sign in to comment.