Class: RepositoryManager::RepoFolder

Inherits:
RepoItem
  • Object
show all
Defined in:
app/models/repository_manager/repo_folder.rb

Instance Method Summary collapse

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, options = {})
  !!options[:overwrite] == options[:overwrite] ? overwrite = options[:overwrite] : overwrite = RepositoryManager.auto_overwrite_item
  repo_item.move!(source_folder: self, do_not_save: options[:do_not_save], destroy_if_fail: options[: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(options = {})
  begin
    copy!(options)
  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!(options = {})
  !!options[:overwrite] == options[:overwrite] ? overwrite = options[:overwrite] : overwrite = RepositoryManager.auto_overwrite_item

  new_item = RepositoryManager::RepoFolder.new
  new_item.name = self.name

  options[:owner] ? new_item.owner = options[:owner] : new_item.owner = self.owner
  options[:sender] ? new_item.sender = options[:sender] : new_item.sender = self.sender

  if options[:source_folder]
    options[:source_folder].add!(new_item, do_not_save: true)
  elsif options[:owner]
    repo_item_with_same_name = options[: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: options[:owner], sender: options[: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(options = {})
  options[:object]? object = options[:object]: object = nil
  RepositoryManager.default_zip_path == true ? path = get_default_download_path(object): path = RepositoryManager.default_zip_path
  path = options[:path] if options[: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(options = {})
  begin
    download!(options)
  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!(options = {})
  # Get all the children
  children = self.children

  # If something is in the array to add, we zip it
  #if children.length > 0

    # Default values
    options[:object]? object = options[:object]: object = nil

    RepositoryManager.default_zip_path == true ? path = get_default_download_path(object): path = RepositoryManager.default_zip_path
    path = options[:path] if options[: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, options = {})
  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 = options[:owner]
      children.sender = options[:sender]
      children.save!
    end
    # remove the first element
    path_array.shift
    children = children.get_or_create_by_path_array(path_array, options)
  end
  children
end

#name_exist_in_children?(name) ⇒ Boolean

Returns true or false if the name exist in this folder

Returns:

  • (Boolean)


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