2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module FileStoreMounter
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
2022-07-23 23:45:48 +05:30
|
|
|
# When `skip_store_file: true` is used, the model MUST explicitly call `store_file_now!`
|
|
|
|
def mount_file_store_uploader(uploader, skip_store_file: false)
|
2020-10-24 23:57:45 +05:30
|
|
|
mount_uploader(:file, uploader)
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
if skip_store_file
|
|
|
|
skip_callback :save, :after, :store_file!
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
# This hook is a no-op when the file is uploaded after_commit
|
2020-10-24 23:57:45 +05:30
|
|
|
after_save :update_file_store, if: :saved_change_to_file?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_file_store
|
2021-12-11 22:18:48 +05:30
|
|
|
# The file.object_store is set during `uploader.store!` and `uploader.migrate!`
|
|
|
|
update_column(:file_store, file.object_store)
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
def store_file_now!
|
|
|
|
store_file!
|
|
|
|
update_file_store
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|