debian-mirror-gitlab/app/models/concerns/file_store_mounter.rb
2023-01-13 15:02:22 +05:30

35 lines
1.3 KiB
Ruby

# 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_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_field}!".to_sym
return
end
# This hook is a no-op when the file is uploaded after_commit
after_save "update_#{file_field}_store".to_sym, if: "saved_change_to_#{file_field}?".to_sym
end
end
end