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)
|
def recover_documents_from_cache(resource)
|
||||||
return false unless resource.try(:documents)
|
return false unless resource.try(:documents)
|
||||||
resource.documents = resource.documents.each do |document|
|
resource.documents = resource.documents.each do |document|
|
||||||
if document.cached_attachment.present?
|
document.set_attachment_from_cache if document.cached_attachment.present?
|
||||||
document.attachment = URI.parse(document.cached_attachment)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ class DocumentsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy_upload
|
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
|
@document.documentable = @documentable
|
||||||
|
|
||||||
if @document.attachment.destroy
|
if @document.attachment.destroy
|
||||||
@@ -62,7 +63,7 @@ class DocumentsController < ApplicationController
|
|||||||
if @document.valid?
|
if @document.valid?
|
||||||
@document.attachment_file_name = "#{Time.now.to_i} - #{@document.attachment_file_name}"
|
@document.attachment_file_name = "#{Time.now.to_i} - #{@document.attachment_file_name}"
|
||||||
@document.attachment.save
|
@document.attachment.save
|
||||||
@document.cached_attachment = URI(request.url) + @document.attachment.url
|
@document.set_cached_attachment_from_attachment(URI(request.url))
|
||||||
else
|
else
|
||||||
@document.attachment.destroy
|
@document.attachment.destroy
|
||||||
end
|
end
|
||||||
@@ -91,7 +92,7 @@ class DocumentsController < ApplicationController
|
|||||||
|
|
||||||
def recover_attachments_from_cache
|
def recover_attachments_from_cache
|
||||||
if @document.attachment.blank? && @document.cached_attachment.present?
|
if @document.attachment.blank? && @document.cached_attachment.present?
|
||||||
@document.attachment = URI.parse(@document.cached_attachment)
|
@document.set_attachment_from_cache
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,22 @@ class Document < ActiveRecord::Base
|
|||||||
|
|
||||||
after_save :remove_cached_document, if: -> { valid? && persisted? && cached_attachment.present? }
|
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
|
private
|
||||||
|
|
||||||
def validate_attachment_size
|
def validate_attachment_size
|
||||||
|
|||||||
Reference in New Issue
Block a user