Attachment Model Patch doesn't work
Added by Dirk König over 15 years ago
Hello!
I'm writing a plugin, which patches the attachment model. My idea is to have the uploaded attachment's in separate subdirectories for each project. For example if I want to upload "test.txt" in project "Test" (with project id 2), i want to find the attachment in:
/files/2/0000000000_text.txt
So my idea is to patch the attachment.rb with a plugin:
require_dependency 'attachment'
module MyAttachmentPatch
    def self.included(base)
        base.send(:include, InstanceMethods)
    base.class_eval do
      unloadable # Send unloadable so it will not be unloaded in development
      alias_method_chain :diskfile, :project_path
    end
    end
    module InstanceMethods
        def diskfile_with_project_path
            path = "#{self.storage_path}/#{self.project.identifier}/#{self.disk_filename}" 
            # path = "#{@@storage_path}/#{self.project.identifier}/#{self.disk_filename}" 
            Dir.mkdirs(path) unless File.directory?(path)
            path
        end
    end
end
Attachment.send(:include, MyAttachmentPatch)
	My init.rb looks like:
begin
    if Dir.pwd.split('/').last == 'public'
       # Apache Workaroung (Apache has the public folder as working dir)
       require '../config/initializers/session_store.rb'
    else
       # Webrick Workaround
       require 'config/initializers/session_store.rb'
    end
rescue LoadError
end
require 'redmine'
require 'application'
...
require 'code_lib_attachment_patch'
ActionView::Base.send :include, CodeLibHelper::PublicMethods
Redmine::Plugin.register :redmine_code_lib do
  name 'Redmine test plugin'
  author 'test'
  description 'test'
  version '0.0.1'
    delete_menu_item :project_menu, :roadmap
  permission :version_extensions, {:version_extensions => [:index, :edit, :show]}, :public => true
  menu :project_menu, :version_extensions, { :controller => 'version_extensions', :action => 'index' }, :caption => :label_version_field_of_application, :after => :activity
end
	If i test it in development, the patch doesn't work (diskfile_with_project_path is never executed). If i test it in production i get an "Application Error", even if I put the original code 1:1 in the patch. Does someone has an idea, why it doesn't work ?
Thank you in advance,
Dirk König
Replies (1)
    
    RE: Attachment Model Patch doesn't work
    -
    Added by vik pro over 13 years ago
  
  if make like that it work fine
def diskfile
#"#{@@storage_path}/#{self.disk_filename}" 
path = "#{self.storage_path}/#{self.project.identifier}/#{self.disk_filename}" 
path_dir = "#{self.storage_path}/#{self.project.identifier}" 
Dir.mkdir(path_dir) unless File.directory?(path_dir)
path
end
I put it directly in attachment.rb it work. make folder with project_ID for each projects. And files not storage anymore in one locate.