Files
nairobi/app/models/concerns/has_attachment.rb
Senén Rodero Rodríguez 5a4921a1af Use URI.open to assign external cached attachments
We were having issues with cached attachments and external services.

A `Tempfile` is returned by `URI.open` when using S3, so we're dealing
with this case as well.
2021-09-24 13:39:15 +02:00

25 lines
751 B
Ruby

module HasAttachment
extend ActiveSupport::Concern
class_methods do
def has_attachment(attribute, paperclip_options = {})
has_one_attached :"storage_#{attribute}"
has_attached_file attribute, paperclip_options
alias_method :"paperclip_#{attribute}=", :"#{attribute}="
define_method :"#{attribute}=" do |file|
if file.is_a?(IO) || file.is_a?(Tempfile) && !file.is_a?(Ckeditor::Http::QqFile)
send(:"storage_#{attribute}").attach(io: file, filename: File.basename(file.path))
elsif file.nil?
send(:"storage_#{attribute}").detach
else
send(:"storage_#{attribute}").attach(file)
end
send(:"paperclip_#{attribute}=", file)
end
end
end
end