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
10 changes: 9 additions & 1 deletion lib/maglev/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ module ActiveStorage
included do
has_one_attached :file

after_commit :save_metadata_now, on: :create, prepend: true
# NOTE: the metadata can only be extracted once Active Storage has uploaded the file,
# which also happens in an after_commit callback (registered by has_one_attached above).
# So this callback has to run after that one:
# - with the legacy (reverse) order, it has to be prepended.
# - with `run_after_transaction_callbacks_in_order_defined` (load_defaults 7.1+),
# it has to be appended. Since Rails 8.1.4, `prepend: true` really runs it first
# (before the upload), raising ActiveStorage::FileNotFoundError.
after_commit :save_metadata_now, on: :create,
prepend: !::ActiveRecord.run_after_transaction_callbacks_in_order_defined

delegate :url, :download, to: :file
end
Expand Down