From 5581445fff7afaf838b8af7c7e39c645331c9f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 31 Aug 2017 12:37:41 +0200 Subject: [PATCH] Use different path to store ajax uploaded cached attachments --- app/controllers/concerns/commentable_actions.rb | 2 +- app/controllers/documents_controller.rb | 11 ++++++++--- app/models/document.rb | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 18a4c0226..1c47b0c06 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -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 diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index af9e284cd..ba12d723a 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -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 @@ -54,12 +58,13 @@ class DocumentsController < ApplicationController else flash.now[:alert] = t "documents.actions.destroy.alert" end - render:destroy + render :destroy end 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 diff --git a/app/models/document.rb b/app/models/document.rb index d8314d4de..8e21b8888 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -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