bill_signature: fix reading unsaved attachments

Since Rails 6, an unsaved attachment_changes can contain either
a Tempfile, or an hash with an :io key.

squash! bill_signature: fix reading unsaved attachments
This commit is contained in:
Pierre de La Morinerie 2020-06-30 18:22:00 +02:00
parent e2b8545222
commit 4aeb8c392f

View file

@ -84,7 +84,7 @@ class BillSignature < ApplicationRecord
def read_signature
if attachment_changes['signature']
io = attachment_changes['signature'].attachable[:io]
io = io_for_changes(attachment_changes['signature'])
io.read if io.present?
elsif signature.attached?
signature.download
@ -93,10 +93,22 @@ class BillSignature < ApplicationRecord
def read_serialized
if attachment_changes['serialized']
io = attachment_changes['serialized'].attachable[:io]
io = io_for_changes(attachment_changes['serialized'])
io.read if io.present?
elsif serialized.attached?
serialized.download
end
end
private
def io_for_changes(attachment_changes)
attachable = attachment_changes.attachable
case attachable
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
attachable.open
when Hash
attachable.fetch(:io)
end
end
end