Class: MediaFolder
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- MediaFolder
- Includes:
- HasOwner
- Defined in:
- app/models/media_folder.rb
Constant Summary collapse
- NAME_LIMIT =
100
- MAX_DEPTH =
3
Class Method Summary collapse
-
.creation_parameters ⇒ Array<Symbol>
Allowed parameters for creating in controllers.
-
.entity_parameters ⇒ Array<Symbol>
Allowed parameters for editing entity in controllers.
Instance Method Summary collapse
-
#branch_ids ⇒ Array<Integer>
Parent branch ids including current id.
- #cache_children! ⇒ Object
- #cache_parents! ⇒ Object
- #can_be_deleted? ⇒ Boolean
- #editable_by?(user) ⇒ Boolean
-
#file_count ⇒ Integer
Total media file count for all children and current entity.
- #full_title ⇒ Object
- #parent_ids ⇒ Array<Integer>
- #parents ⇒ Object
-
#subbranch_ids ⇒ Array<Integer>
Child branch (with subbranches) ids starting with current id.
Methods included from HasOwner
Class Method Details
.creation_parameters ⇒ Array<Symbol>
Allowed parameters for creating in controllers
39 40 41 |
# File 'app/models/media_folder.rb', line 39 def self.creation_parameters entity_parameters + %i(parent_id) end |
.entity_parameters ⇒ Array<Symbol>
Allowed parameters for editing entity in controllers
32 33 34 |
# File 'app/models/media_folder.rb', line 32 def self.entity_parameters %i(name snapshot) end |
Instance Method Details
#branch_ids ⇒ Array<Integer>
Parent branch ids including current id
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
78 79 80 |
# File 'app/models/media_folder.rb', line 78 def can_be_deleted? file_count < 1 end |
#editable_by?(user) ⇒ 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_count ⇒ Integer
Total media file count for all children and current entity
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_title ⇒ Object
43 44 45 |
# File 'app/models/media_folder.rb', line 43 def full_title (parents.map { |parent| parent.name } + [name]).join ' / ' end |
#parent_ids ⇒ Array<Integer>
48 49 50 |
# File 'app/models/media_folder.rb', line 48 def parent_ids parents_cache.split(',').compact.map(&:to_i) end |
#parents ⇒ Object
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_ids ⇒ Array<Integer>
Child branch (with subbranches) ids starting with current id
62 63 64 |
# File 'app/models/media_folder.rb', line 62 def subbranch_ids [id] + children_cache end |