Class: RepositoryManager::RepoFolder
- Defined in:
- app/models/repository_manager/repo_folder.rb
Instance Method Summary collapse
- #add(repo_item) ⇒ Object
-
#add!(repo_item, options = {}) ⇒ Object
Add a repo_item in the folder.
- #copy(options = {}) ⇒ Object
-
#copy!(options = {}) ⇒ Object
Copy itself into the source_folder options :source_folder = the folder in witch you copy this item :owner = the owner of the item :sender = the sender of the item (if you don’t specify sender.. The sender is still the same) :overwrite = overwrite an item with the same name (default : see config ‘auto_overwrite_item’).
-
#delete_zip(options = {}) ⇒ Object
Delete the zip file.
- #download(options = {}) ⇒ Object
-
#download!(options = {}) ⇒ Object
Return the path to the folder.zip options can have : :object => Object : is the object that request the download If object = nil, it download all the folder if object is set, it download only the folder that the object ‘can_read`.
- #get_children_by_name(name) ⇒ Object
-
#get_or_create_by_path_array(path_array, options = {}) ⇒ Object
Get or create the folder with this name options :owner = you can path an owner (you have to if you have no :source_folder) :sender = the sender of the item.
-
#name_exist_in_children?(name) ⇒ Boolean
Returns true or false if the name exist in this folder.
Methods inherited from RepoItem
#can_be_shared_without_nesting?, #is_file?, #is_folder?, #move, #move!, #name_exist_in_siblings?, #rename, #rename!
Instance Method Details
#add(repo_item) ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/repository_manager/repo_folder.rb', line 40 def add(repo_item) begin add!(repo_item) rescue RepositoryManager::ItemExistException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved false end end |
#add!(repo_item, options = {}) ⇒ Object
Add a repo_item in the folder. options
:destroy_if_fail = false // the repo_item if it can't move it.
:do_not_save = false // Save the repo_item after changing his param
:overwrite = overwrite an item with the same name (default : see config 'auto_overwrite_item')
second param destroy the repo_item if it can’t move it.
35 36 37 38 |
# File 'app/models/repository_manager/repo_folder.rb', line 35 def add!(repo_item, = {}) !![:overwrite] == [:overwrite] ? overwrite = [:overwrite] : overwrite = RepositoryManager.auto_overwrite_item repo_item.move!(source_folder: self, do_not_save: [:do_not_save], destroy_if_fail: [:destroy_if_fail], overwrite: overwrite) end |
#copy(options = {}) ⇒ Object
93 94 95 96 97 98 99 |
# File 'app/models/repository_manager/repo_folder.rb', line 93 def copy( = {}) begin copy!() rescue RepositoryManager::ItemExistException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved false end end |
#copy!(options = {}) ⇒ Object
Copy itself into the source_folder options
:source_folder = the folder in witch you copy this item
:owner = the owner of the item
:sender = the sender of the item (if you don't specify sender.. The sender is still the same)
:overwrite = overwrite an item with the same name (default : see config 'auto_overwrite_item')
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/repository_manager/repo_folder.rb', line 54 def copy!( = {}) !![:overwrite] == [:overwrite] ? overwrite = [:overwrite] : overwrite = RepositoryManager.auto_overwrite_item new_item = RepositoryManager::RepoFolder.new new_item.name = self.name [:owner] ? new_item.owner = [:owner] : new_item.owner = self.owner [:sender] ? new_item.sender = [:sender] : new_item.sender = self.sender if [:source_folder] [:source_folder].add!(new_item, do_not_save: true) elsif [:owner] repo_item_with_same_name = [:owner].get_item_in_root_by_name(new_item.name) if repo_item_with_same_name and !overwrite self.errors.add(:copy, I18n.t('repository_manager.errors.repo_item.item_exist')) raise RepositoryManager::ItemExistException.new("copy failed. The repo_folder '#{new_item.name}' already exist in root.") elsif repo_item_with_same_name and overwrite repo_item_with_same_name.destroy! end else repo_item_with_same_name = self.owner.get_item_in_root_by_name(new_item.name) if repo_item_with_same_name and !overwrite self.errors.add(:copy, I18n.t('repository_manager.errors.repo_item.item_exist')) raise RepositoryManager::ItemExistException.new("copy failed. The repo_file '#{new_item.name}' already exist in root.") elsif repo_item_with_same_name and overwrite repo_item_with_same_name.destroy! end end new_item.save # Recursive method which copy all children. children.each do |c| c.copy!(source_folder: new_item, owner: [:owner], sender: [:sender], overwrite: overwrite) end new_item end |
#delete_zip(options = {}) ⇒ Object
Delete the zip file
154 155 156 157 158 159 160 161 |
# File 'app/models/repository_manager/repo_folder.rb', line 154 def delete_zip( = {}) [:object]? object = [:object]: object = nil RepositoryManager.default_zip_path == true ? path = get_default_download_path(object): path = RepositoryManager.default_zip_path path = [:path] if [:path] # Delete the path FileUtils.rm_rf(path) end |
#download(options = {}) ⇒ Object
144 145 146 147 148 149 150 |
# File 'app/models/repository_manager/repo_folder.rb', line 144 def download( = {}) begin download!() rescue RepositoryManager::RepositoryManagerException false end end |
#download!(options = {}) ⇒ Object
Return the path to the folder.zip options can have :
:object => Object : is the object that request the download
If object = nil, it download all the folder
if object is set, it download only the folder that the object `can_read`.
:path => 'path/to/zip/' is the path where the zip is generated
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/repository_manager/repo_folder.rb', line 108 def download!( = {}) # Get all the children children = self.children # If something is in the array to add, we zip it #if children.length > 0 # Default values [:object]? object = [:object]: object = nil RepositoryManager.default_zip_path == true ? path = get_default_download_path(object): path = RepositoryManager.default_zip_path path = [:path] if [:path] full_path = "#{path}#{name}.zip" # Create the directory if not exist dir = File.dirname(full_path) unless File.directory?(dir) FileUtils.mkdir_p(dir) end # Delete the zip if it already exist File.delete(full_path) if File.exist?(full_path) Zip::File.open(full_path, Zip::File::CREATE) { |zf| add_repo_item_to_zip(children, zf, object) } File.chmod(0444, full_path) return full_path #else # # Nothing to download here # raise RepositoryManager::RepositoryManagerException.new("download failed. Folder #{name} is empty") #end end |
#get_children_by_name(name) ⇒ Object
169 170 171 |
# File 'app/models/repository_manager/repo_folder.rb', line 169 def get_children_by_name(name) RepositoryManager::RepoItem.where('name = ?', name).where(id: child_ids).first end |
#get_or_create_by_path_array(path_array, options = {}) ⇒ Object
Get or create the folder with this name options
:owner = you can path an owner (you have to if you have no :source_folder)
:sender = the sender of the item
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/repository_manager/repo_folder.rb', line 11 def get_or_create_by_path_array(path_array, = {}) children = self unless path_array.empty? name = path_array[0] children = self.get_children_by_name(name) unless children children = RepositoryManager::RepoFolder.new(name: name) children.owner = [:owner] children.sender = [:sender] children.save! end # remove the first element path_array.shift children = children.get_or_create_by_path_array(path_array, ) end children end |
#name_exist_in_children?(name) ⇒ Boolean
Returns true or false if the name exist in this folder
164 165 166 167 |
# File 'app/models/repository_manager/repo_folder.rb', line 164 def name_exist_in_children?(name) #RepositoryManager::RepoItem.where(name: name).where(id: child_ids).first ? true : false get_children_by_name(name) ? true : false end |