Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/elastic_record/model/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module ElasticRecord
module Model
module Callbacks
def self.included(base)
return unless base.respond_to?(:after_save) && base.respond_to?(:after_destroy)
return unless base.respond_to?(:after_commit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elastic_record only supports Ruby 2.7+:

s.required_ruby_version = '>= 2.7'

Based on https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html, that pretty much limits it to Rail >= 6.

So if the point of this line is to ensure that we're using a version of Rails that supports after_commit (which has been around since Rails 3), we can remove it.

Otherwise, this is the same as if defined?(ActiveRecord::Base) && base < ActiveRecord::Base, which I think is a more explicit way of expressing what we're trying to achieve here.


base.class_eval do
after_create :index_to_elasticsearch
after_update :update_index_document, if: -> { respond_to?(:saved_changes?) ? saved_changes? : changed? }
after_destroy :delete_index_document
after_commit :index_to_elasticsearch, on: :create
after_commit :update_index_document, on: :update, if: -> { respond_to?(:saved_changes?) ? saved_changes? : changed? }
after_commit :delete_index_document, on: :destroy
end
end

Expand Down