This fixes a few issues we've had for years. First, when attaching an image and then sending a form with validation errors, the image preview would not be rendered when the form was displayed once again. Now it's rendered as expected. Second, when attaching an image, removing it, and attaching a new one, browsers were displaying the image preview of the first one. That's because Paperclip generated the same URL from both files (as they both had the same hash data and prefix). Browsers usually cache images and render the cached image when getting the same URL. Since now we're storing each image in a different Blob, the images have different URLs and so the preview of the second one is correctly displayed. Finally, when users downloaded a document, they were getting files with a very long hexadecimal hash as filename. Now they get the original filename.
28 lines
798 B
Ruby
28 lines
798 B
Ruby
module HasAttachment
|
|
extend ActiveSupport::Concern
|
|
|
|
class_methods do
|
|
def has_attachment(attribute, paperclip_options = {})
|
|
has_one_attached :"storage_#{attribute}"
|
|
|
|
define_method :"storage_#{attribute}=" do |file|
|
|
if file.is_a?(IO)
|
|
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
|
|
end
|
|
|
|
has_attached_file attribute, paperclip_options
|
|
alias_method :"paperclip_#{attribute}=", :"#{attribute}="
|
|
|
|
define_method :"#{attribute}=" do |file|
|
|
send(:"storage_#{attribute}=", file)
|
|
send(:"paperclip_#{attribute}=", file)
|
|
end
|
|
end
|
|
end
|
|
end
|