diff options
Diffstat (limited to 'app/models/concerns/file_store_mounter.rb')
-rw-r--r-- | app/models/concerns/file_store_mounter.rb | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/app/models/concerns/file_store_mounter.rb b/app/models/concerns/file_store_mounter.rb index f1ac734635d..4d267dc69d0 100644 --- a/app/models/concerns/file_store_mounter.rb +++ b/app/models/concerns/file_store_mounter.rb @@ -1,31 +1,35 @@ # frozen_string_literal: true module FileStoreMounter + ALLOWED_FILE_FIELDS = %i[file signed_file].freeze + extend ActiveSupport::Concern class_methods do - # 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) - mount_uploader(:file, uploader) + # When `skip_store_file: true` is used, the model MUST explicitly call `store_#{file_field}_now!` + def mount_file_store_uploader(uploader, skip_store_file: false, file_field: :file) + raise ArgumentError, "file_field not allowed: #{file_field}" unless ALLOWED_FILE_FIELDS.include?(file_field) + + mount_uploader(file_field, uploader) + + define_method("update_#{file_field}_store") do + # The file.object_store is set during `uploader.store!` and `uploader.migrate!` + update_column("#{file_field}_store", public_send(file_field).object_store) # rubocop:disable GitlabSecurity/PublicSend + end + + define_method("store_#{file_field}_now!") do + public_send("store_#{file_field}!") # rubocop:disable GitlabSecurity/PublicSend + public_send("update_#{file_field}_store") # rubocop:disable GitlabSecurity/PublicSend + end if skip_store_file - skip_callback :save, :after, :store_file! + skip_callback :save, :after, "store_#{file_field}!".to_sym return end # This hook is a no-op when the file is uploaded after_commit - after_save :update_file_store, if: :saved_change_to_file? + after_save "update_#{file_field}_store".to_sym, if: "saved_change_to_#{file_field}?".to_sym end end - - def update_file_store - # The file.object_store is set during `uploader.store!` and `uploader.migrate!` - update_column(:file_store, file.object_store) - end - - def store_file_now! - store_file! - update_file_store - end end |