Make documentable capable to work with local or remote storage system.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user