1 # A folder is a place where files can be stored.
2 # Folders can also have sub-folders.
3 # Via groups it is determined which actions the logged-in User can perform.
4 class Folder < ActiveRecord::Base
5 acts_as_ferret :store_class_name => true, :fields => { :name => { :store => :no } }
6 acts_as_tree :order => :name
9 has_many :myfiles, :dependent => :destroy
10 has_many :group_permissions, :dependent => :destroy
11 has_many :clipboards, :dependent => :destroy
14 validates_uniqueness_of :name, :scope => 'parent_id'
15 validates_presence_of :name
17 cattr_accessor :clipboard
21 # create the physical folder on disk
22 Dir.mkdir(self.path_on_disk) unless File.exists?(self.path_on_disk)
25 # have to call destroy through this, otherwise the moving doesn't work properly
27 trash_name = "#{TRASH_PATH}/#{name}.#{Time.now.to_f.to_s}"
28 File.mv(path_on_disk, trash_name)
29 p "Moving from #{path_on_disk} to #{trash_name}"
33 # gets called after delete, so folder has already been moved
35 trash_name = "#{TRASH_PATH}/#{@name}.#{Time.now.to_f.to_s}"
36 log_usage("deleted","moved from01 #{self.path_on_disk} moved to #{trash_name}")
42 unless folder.parent_id == 0
43 until folder.parent_id == 1
44 folder = folder.parent
45 path = folder.name + "/" + path
50 return (UPLOAD_PATH + "/" + path)
55 parent_path = self.parent.path_on_disk
56 if self.update_attributes(:name => Myfile.base_part_of(name), :date_modified => Time.now) && File.rename( parent_path + "/" + old_name, self.path_on_disk)
57 log_usage("renamed","from #{old_name} to #{self.name}")
64 return self.note unless self.note.nil? || self.note.empty?
65 current, result = self, ''
66 until !result.empty? || current == nil
67 if !current.note.nil? && !current.note.empty? && current.note_inheritable
70 current = current.parent
73 return (result.empty? ? nil : result)
76 def note_upload_inherited
77 return self.note_upload unless self.note_upload.nil? || self.note_upload.empty?
78 current, result = self, ''
79 until !result.empty? || current == nil
80 if !current.note_upload.nil? && !current.note_upload.empty? && current.note_upload_inheritable
81 result = current.note_upload
83 current = current.parent
86 return (result.empty? ? nil : result)
90 # for the given user in the given order.
91 def list_subfolders(logged_in_user, order)
93 if logged_in_user.can_read(self.id)
94 self.children.each do |sub_folder|
95 folders << sub_folder if logged_in_user.can_read(sub_folder.id)
104 # for the given user in the given order.
105 def list_files(logged_in_user, order)
107 if logged_in_user.can_read(self.id)
108 files = self.myfiles.find(:all, :order => order)
115 # Returns whether or not the root folder exists
116 def self.root_folder_exists?
117 folder = Folder.find_by_is_root(true)
118 return (not folder.blank?)
121 # Create the Root folder
122 def self.create_root_folder
123 if User.admin_exists? #and Folder.root_folder_exists?
125 folder.name = 'Root folder'
126 folder.date_modified = Time.now
127 folder.is_root = true
128 folder.lft, folder.rgt = 1, 2 # must be initialized, otherwise quota check on upload
129 # won't work until another folder has been added
131 # This folder is created by the admin
132 if user = User.find_by_is_the_administrator(true)
136 folder.save # this hopefully returns true
140 def log_usage(action, comment = nil)
142 :download_date_time => Time.now,
143 :user => User.logged_in_user,
145 :filename => self.name,
151 def all_with_children
153 self.children.each do |child_folder|
154 folders += child_folder.all_with_children if child_folder.children
160 self.all_with_children - [self]
166 if Myfile.find_by_filename_and_folder_id(self.name, self.parent_id)
167 errors.add_to_base "You cannot create a folder with the same name as a file."