diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index e4f8f952e..18a4c0226 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -115,9 +115,7 @@ module CommentableActions def recover_documents_from_cache(resource) return false unless resource.try(:documents) resource.documents = resource.documents.each do |document| - if document.cached_attachment.present? - document.attachment = URI.parse(document.cached_attachment) - end + document.set_attachment_from_cache if document.cached_attachment.present? end end diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 200d3d142..af9e284cd 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -45,7 +45,8 @@ class DocumentsController < ApplicationController end def destroy_upload - @document = Document.new(attachment: URI.parse(params[:path])) + @document = Document.new(cached_attachment: params[:path]) + @document.set_attachment_from_cache @document.documentable = @documentable if @document.attachment.destroy @@ -62,7 +63,7 @@ class DocumentsController < ApplicationController if @document.valid? @document.attachment_file_name = "#{Time.now.to_i} - #{@document.attachment_file_name}" @document.attachment.save - @document.cached_attachment = URI(request.url) + @document.attachment.url + @document.set_cached_attachment_from_attachment(URI(request.url)) else @document.attachment.destroy end @@ -91,7 +92,7 @@ class DocumentsController < ApplicationController def recover_attachments_from_cache if @document.attachment.blank? && @document.cached_attachment.present? - @document.attachment = URI.parse(@document.cached_attachment) + @document.set_attachment_from_cache end end diff --git a/app/models/document.rb b/app/models/document.rb index 97789806f..d8314d4de 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -20,6 +20,22 @@ class Document < ActiveRecord::Base after_save :remove_cached_document, if: -> { valid? && persisted? && cached_attachment.present? } + def set_cached_attachment_from_attachment(prefix) + self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem + attachment.path + else + prefix + attachment.url + end + end + + def set_attachment_from_cache + self.attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem + File.open(cached_attachment) + else + URI.parse(cached_attachment) + end + end + private def validate_attachment_size