Update indexed instance after blob analyzed
# app/jobs/active_storage/analyze_job_decorator.rb
module ActiveStorage::AnalyzeJobDecorator
def self.prepended(base)
base.around_perform do |job, block|
block.call
blob = job.arguments.first
blob.reload.attachments.each do |attachment|
next unless attachment.record.respond_to?(:reindex)
Searchkick::RecordIndexer.new(attachment.record).reindex
end
end
end
::ActiveStorage::AnalyzeJob.prepend(self)
end
# app/models/active_storage/blob_decorator.rb
# ...
end
def self.prepended(base)
base.after_commit :reindex_attachment_records, only: :update
end
def reindex_attachment_records
return unless saved_change_to_metadata?
attachments.each do |attachment|
next unless attachment.record.respond_to?(:reindex)
Searchkick::RecordIndexer.new(attachment.record).reindex
end
end
::ActiveStorage::Blob.extend(ClassMethods)
::ActiveStorage::Blob.prepend(self)
end
# config/application.rb
config.to_prepare do
Dir.glob(Rails.root.join('app/**/*_decorator*.rb')) do |path|
require_dependency(path)
end
end