From b990384b0aa55b3a982d83a9d2b0ec2d1b954ec1 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 3 Jan 2018 17:02:14 +0100 Subject: [PATCH] Fix multiple attachment upload by setting the id, style and updated at date on hash --- app/models/document.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/document.rb b/app/models/document.rb index 3771a4e69..108f29fad 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -2,10 +2,10 @@ class Document < ActiveRecord::Base include DocumentsHelper include DocumentablesHelper has_attached_file :attachment, url: "/system/:class/:prefix/:style/:hash.:extension", - hash_data: ":class/:style", + hash_data: ":class/:style/:custom_hash_data", use_timestamp: false, hash_secret: Rails.application.secrets.secret_key_base - attr_accessor :cached_attachment + attr_accessor :cached_attachment, :remove, :original_filename belongs_to :user belongs_to :documentable, polymorphic: true @@ -44,6 +44,10 @@ class Document < ActiveRecord::Base attachment.instance.prefix(attachment, style) end + Paperclip.interpolates :custom_hash_data do |attachment, _style| + attachment.instance.custom_hash_data(attachment) + end + def prefix(attachment, _style) if !attachment.instance.persisted? "cached_attachments/user/#{attachment.instance.user_id}" @@ -52,6 +56,17 @@ class Document < ActiveRecord::Base end end + def custom_hash_data(attachment) + original_filename = if !attachment.instance.persisted? && attachment.instance.remove + attachment.instance.original_filename + elsif !attachment.instance.persisted? + attachment.instance.attachment_file_name + else + attachment.instance.title + end + "#{attachment.instance.user_id}/#{original_filename}" + end + def humanized_content_type attachment_content_type.split("/").last.upcase end @@ -89,7 +104,9 @@ class Document < ActiveRecord::Base def remove_cached_attachment document = Document.new(documentable: documentable, cached_attachment: cached_attachment, - user: user) + user: user, + remove: true, + original_filename: title) document.set_attachment_from_cached_attachment document.attachment.destroy end