Use different path to store ajax uploaded cached attachments
This commit is contained in:
@@ -115,7 +115,7 @@ module CommentableActions
|
||||
def recover_documents_from_cache(resource)
|
||||
return false unless resource.try(:documents)
|
||||
resource.documents = resource.documents.each do |document|
|
||||
document.set_attachment_from_cache if document.cached_attachment.present?
|
||||
document.set_attachment_from_cached_attachment if document.cached_attachment.present?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class DocumentsController < ApplicationController
|
||||
|
||||
def create
|
||||
recover_attachments_from_cache
|
||||
|
||||
if @document.save
|
||||
flash[:notice] = t "documents.actions.create.notice"
|
||||
redirect_to params[:from]
|
||||
@@ -26,6 +27,7 @@ class DocumentsController < ApplicationController
|
||||
|
||||
def destroy
|
||||
respond_to do |format|
|
||||
|
||||
format.html do
|
||||
if @document.destroy
|
||||
flash[:notice] = t "documents.actions.destroy.notice"
|
||||
@@ -34,6 +36,7 @@ class DocumentsController < ApplicationController
|
||||
end
|
||||
redirect_to params[:from]
|
||||
end
|
||||
|
||||
format.js do
|
||||
if @document.destroy
|
||||
flash.now[:notice] = t "documents.actions.destroy.notice"
|
||||
@@ -41,12 +44,13 @@ class DocumentsController < ApplicationController
|
||||
flash.now[:alert] = t "documents.actions.destroy.alert"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_upload
|
||||
@document = Document.new(cached_attachment: params[:path])
|
||||
@document.set_attachment_from_cache
|
||||
@document.set_attachment_from_cached_attachment
|
||||
@document.documentable = @documentable
|
||||
|
||||
if @document.attachment.destroy
|
||||
@@ -60,6 +64,7 @@ class DocumentsController < ApplicationController
|
||||
def upload
|
||||
@document = Document.new(document_params.merge(user: current_user))
|
||||
@document.documentable = @documentable
|
||||
|
||||
if @document.valid?
|
||||
@document.attachment_file_name = "#{Time.now.to_i} - #{@document.attachment_file_name}"
|
||||
@document.attachment.save
|
||||
@@ -92,7 +97,7 @@ class DocumentsController < ApplicationController
|
||||
|
||||
def recover_attachments_from_cache
|
||||
if @document.attachment.blank? && @document.cached_attachment.present?
|
||||
@document.set_attachment_from_cache
|
||||
@document.set_attachment_from_cached_attachment
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Document < ActiveRecord::Base
|
||||
include DocumentsHelper
|
||||
include DocumentablesHelper
|
||||
has_attached_file :attachment
|
||||
has_attached_file :attachment, path: ":rails_root/public/system/:class/:attachment/:prefix/:style/:filename"
|
||||
attr_accessor :cached_attachment
|
||||
|
||||
belongs_to :user
|
||||
@@ -28,7 +28,7 @@ class Document < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def set_attachment_from_cache
|
||||
def set_attachment_from_cached_attachment
|
||||
self.attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
|
||||
File.open(cached_attachment)
|
||||
else
|
||||
@@ -36,6 +36,18 @@ class Document < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
Paperclip.interpolates :prefix do |attachment, style|
|
||||
attachment.instance.prefix(attachment, style)
|
||||
end
|
||||
|
||||
def prefix(attachment, style)
|
||||
if !attachment.instance.persisted?
|
||||
"cached_attachments/user/#{attachment.instance.user_id}"
|
||||
else
|
||||
Paperclip::Interpolations.id_partition(attachment, style)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_attachment_size
|
||||
|
||||
Reference in New Issue
Block a user