From 4aeb8c392f7b345f572f62966e5c913b17666275 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 30 Jun 2020 18:22:00 +0200 Subject: [PATCH] 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 --- app/models/bill_signature.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/bill_signature.rb b/app/models/bill_signature.rb index c4a9574d0..43e748e49 100644 --- a/app/models/bill_signature.rb +++ b/app/models/bill_signature.rb @@ -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