Class: MediaFolder

Inherits:
ApplicationRecord show all
Includes:
HasOwner
Defined in:
app/models/media_folder.rb

Constant Summary collapse

NAME_LIMIT =
100
MAX_DEPTH =
3

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasOwner

#owned_by?, #owner_name

Class Method Details

.creation_parametersArray<Symbol>

Allowed parameters for creating in controllers

Returns:

  • (Array<Symbol>)


39
40
41
# File 'app/models/media_folder.rb', line 39

def self.creation_parameters
  entity_parameters + %i(parent_id)
end

.entity_parametersArray<Symbol>

Allowed parameters for editing entity in controllers

Returns:

  • (Array<Symbol>)


32
33
34
# File 'app/models/media_folder.rb', line 32

def self.entity_parameters
  %i(name snapshot)
end

Instance Method Details

#branch_idsArray<Integer>

Parent branch ids including current id

Returns:

  • (Array<Integer>)


55
56
57
# File 'app/models/media_folder.rb', line 55

def branch_ids
  parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id]
end

#cache_children!Object



93
94
95
96
97
98
# File 'app/models/media_folder.rb', line 93

def cache_children!
  child_folders.order('id asc').each do |child|
    self.children_cache += [child.id] + child.children_cache
  end
  save!
end

#cache_parents!Object



87
88
89
90
91
# File 'app/models/media_folder.rb', line 87

def cache_parents!
  return if parent.nil?
  self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '')
  save!
end

#can_be_deleted?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/media_folder.rb', line 78

def can_be_deleted?
  file_count < 1
end

#editable_by?(user) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


83
84
85
# File 'app/models/media_folder.rb', line 83

def editable_by?(user)
  owned_by?(user) || UserPrivilege.user_has_privilege?(user, :chief_editor)
end

#file_countInteger

Total media file count for all children and current entity

Returns:

  • (Integer)


74
75
76
# File 'app/models/media_folder.rb', line 74

def file_count
  MediaFolder.where(id: subbranch_ids).sum(:media_files_count)
end

#full_titleObject



43
44
45
# File 'app/models/media_folder.rb', line 43

def full_title
  (parents.map { |parent| parent.name } + [name]).join ' / '
end

#parent_idsArray<Integer>

Returns:

  • (Array<Integer>)


48
49
50
# File 'app/models/media_folder.rb', line 48

def parent_ids
  parents_cache.split(',').compact.map(&:to_i)
end

#parentsObject



66
67
68
69
# File 'app/models/media_folder.rb', line 66

def parents
  return [] if parents_cache.blank?
  MediaFolder.where(id: parent_ids).order('id asc')
end

#subbranch_idsArray<Integer>

Child branch (with subbranches) ids starting with current id

Returns:

  • (Array<Integer>)


62
63
64
# File 'app/models/media_folder.rb', line 62

def subbranch_ids
  [id] + children_cache
end