Class: RepositoryManager::RepoItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/repository_manager/repo_item.rb

Direct Known Subclasses

RepoFile, RepoFolder

Instance Method Summary collapse

Instance Method Details

#can_be_shared_without_nesting?Boolean

Returns true if it exist a sharing in the ancestors of descendant_ids of the repo_item (without itself)

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/repository_manager/repo_item.rb', line 93

def can_be_shared_without_nesting?
  # An array with the ids of all ancestors and descendants
  ancestor_and_descendant_ids = []
  ancestor_and_descendant_ids << self.descendant_ids if self.is_folder? && !self.descendant_ids.empty?
  ancestor_and_descendant_ids << self.ancestor_ids if !self.ancestor_ids.empty?

  # If it exist a sharing, it returns true
  if RepositoryManager::Sharing.where(repo_item_id: ancestor_and_descendant_ids).count > 0
    false
  else
    true
  end
end

#is_file?Boolean

Returns true if it is a file

Returns:

  • (Boolean)


113
114
115
# File 'app/models/repository_manager/repo_item.rb', line 113

def is_file?
  self.type == 'RepositoryManager::RepoFile'
end

#is_folder?Boolean

Returns true if it is a folder

Returns:

  • (Boolean)


108
109
110
# File 'app/models/repository_manager/repo_item.rb', line 108

def is_folder?
  self.type == 'RepositoryManager::RepoFolder'
end

#move(options = {}) ⇒ Object



64
65
66
67
68
69
70
# File 'app/models/repository_manager/repo_item.rb', line 64

def move(options = {})
  begin
    move!(options)
  rescue RepositoryManager::RepositoryManagerException, RepositoryManager::ItemExistException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    false
  end
end

#move!(options = {}) ⇒ Object

Move itself into the target or root options

:source_folder = the folder in witch you copy this item
:owner = the owner of the item

If :source_folder = nil, move to the root (of same owner)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/repository_manager/repo_item.rb', line 35

def move!(options = {})
  # If we are in source_folder, we check if it's ok
  if options[:source_folder]
    unless options[:source_folder].is_folder?
      raise RepositoryManager::RepositoryManagerException.new("move failed. target '#{options[:source_folder].name}' can't be a file")
    end
    if options[:source_folder].name_exist_in_children?(self.name)
      self.errors.add(:move, I18n.t('repository_manager.errors.repo_item.item_exist'))
      raise RepositoryManager::ItemExistException.new("move failed. The repo_item '#{name}' already exist ine the folder '#{options[:source_folder].name}'")
    end
  # We are in root, we check if name exist in root
  # We stay in the same owner
  elsif self.owner.repo_item_name_exist_in_root?(self.name)
    self.errors.add(:move, I18n.t('repository_manager.errors.repo_item.item_exist'))
    raise RepositoryManager::ItemExistException.new("move failed. The repo_item '#{name}' already exist ine the root")
  end
  # here, all is ok
  # We change the owner if another one is specify
  if options[:owner]
    self.owner = options[:owner]
  elsif options[:source_folder]
    self.owner = options[:source_folder].owner
  end
  # we update the tree with the new parent
  self.update_attribute :parent, options[:source_folder]
  self.save!
  self
end

#name_exist_in_siblings?(name) ⇒ Boolean

Returns true or false if the name exist in siblings

Returns:

  • (Boolean)


118
119
120
121
122
123
124
# File 'app/models/repository_manager/repo_item.rb', line 118

def name_exist_in_siblings?(name)
  # We take all siblings without itself
  sibling_ids_without_itself = self.sibling_ids.delete(self.id)
  # We check if another item has the same name
  #RepositoryManager::RepoItem.where(name: name).where(id: sibling_ids_without_itself).first ? true : false
  RepositoryManager::RepoItem.where('name = ?', name).where(id: sibling_ids_without_itself).first ? true : false
end

#rename(new_name) ⇒ Object

Rename the item



84
85
86
87
88
89
90
# File 'app/models/repository_manager/repo_item.rb', line 84

def rename(new_name)
  begin
    rename!(new_name)
  rescue RepositoryManager::ItemExistException, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
    false
  end
end

#rename!(new_name) ⇒ Object

Rename the item



73
74
75
76
77
78
79
80
81
# File 'app/models/repository_manager/repo_item.rb', line 73

def rename!(new_name)
  if name_exist_in_siblings?(new_name)
    self.errors.add(:copy, I18n.t('repository_manager.errors.repo_item.item_exist'))
    raise RepositoryManager::ItemExistException.new("rename failed. The repo_item '#{new_name}' already exist.'")
  else
    self.name = new_name
    save!
  end
end