Reputation: 91
I have pictures attached to a model. Those pictures are analyzed and the EXIF data is saved as metadata on the ActiveStorage::Blob
.
class Foo < ApplicationRecord
has_one_attached :picture
end
There is an attribute on this model that I use for sorting the instances called order_date
. This attribute has to be updated with the EXIF time after the blob got analyzed.
Using paperclip, a before_commit
callback method was sufficient. With ActiveStorage, I also tried before_save
and after_touch
but both are not working.
How can I run code right after the ActiveStorage::AnalyzeJob
has run successfully?
(I want to avoid monkey-patching ActiveStorage::AnalyzeJob
, because it is also performed for other attachments.)
Thanks very much for your help!
Upvotes: 4
Views: 2560
Reputation: 41
I couldn't find anything official. I ended up overriding the analyze job since it is very simple anyways. It looks like below.
#/app/jobs/active_storage/analyze_job.rb
class ActiveStorage::AnalyzeJob < ActiveStorage::BaseJob
def perform(blob)
blob.analyze
blob.attachments.includes(:record).each do |attachment|
if attachment.record_type == 'Content'
record = attachment.record
record.set_file_info
record.save!
end
end
end
end
Upvotes: 4