| Class | Folder |
| In: |
app/models/folder.rb
|
| Parent: | ActiveRecord::Base |
Folders can store another folders or Document’s.
| childrens | -> | ar_childrens |
| mapping | -> | ar_mapping |
| documents | -> | ar_documents |
All hierarchy’s root folder
# File app/models/folder.rb, line 56
56: def self.find_root
57: find_by_sql( sql_store.folder_find_root() ).first
58: end
Returns array with sub-folders. If include_subfolders is false, only first-level childrens are returned
# File app/models/folder.rb, line 72
72: def childrens(include_subfolders = true)
73: return Folder.find_all([ 'hidden <> 1 AND folder_id = ?', self.id]) unless include_subfolders
74: @_childrens ||= Folder.find_by_sql( sql_store.folder_find_all_childrens ).select { |f| f.l > self.l and f.r < self.r }
75: end
Returns documents of this folder without iterating into sub-folders
# File app/models/folder.rb, line 114
114: def documents(limit = nil)
115: plainscanner().scan(limit)
116: end
Iterates through documents of this folder, without iterating into subfolders.
# File app/models/folder.rb, line 93
93: def each
94: documents.each do |doc|
95: yield doc
96: end
97: end
Iterates through sub-folders
# File app/models/folder.rb, line 211
211: def each_folder # yields: folder
212: self.childrens(false).each do |element|
213: yield element
214: end
215: end
Check if this folder has no documents
# File app/models/folder.rb, line 119
119: def empty?
120: docscanner().count == 0
121: end
Fetches all documents from the folder corresponding to "where to look for documents" field of the mapping
<% for document in @folder.fetch_all %>
<%= document.title %> in <%= document.folder_name %>
<% end %>
# File app/models/folder.rb, line 132
132: def fetch_all
133: docscanner().scan_published(nil)
134: end
Returns first num of documents from the Folder#fetch_all() result
# File app/models/folder.rb, line 145
145: def fetch_first(num)
146: docscanner().scan_published( [0, num ] )
147: end
Returns last num of documents from the Folder#fetch_all() result
# File app/models/folder.rb, line 150
150: def fetch_last(num)
151: start = docscanner().count_published - num
152: if num > start
153: start = 0
154: end
155: docscanner().scan_published( [ start, num ] ).reverse
156: end
Splits Folder#fetch_all() results using FolderPaginator class. Returns result for a given current_page
<% for document in @folder.fetch_page(@params[:page]) %>
<%= document.title %> in <%= document.folder_name %>
<% end %>
# File app/models/folder.rb, line 140
140: def fetch_page(current_page = nil)
141: docscanner().scan_published(paginator(current_page).sql_limit)
142: end
Check if this folder was created only to store some document’s parts
# File app/models/folder.rb, line 168
168: def for_parts?
169: hidden?
170: end
Check if exact this folder has a mapping on it
# File app/models/folder.rb, line 163
163: def has_direct_mapping?
164: !ar_mapping.nil?
165: end
# File app/models/folder.rb, line 270
270: def inspect
271: "<Folder: #{id}; name: #{name}; root?: #{root?}; itemCount: #{itemCount}; [#{l},#{r}]>"
272: end
Iterates through hierarchy under a given folder.
Example:
<ul>
<% @folder.iterate do |section, level, shift| %>
<%= shift > 0 ? '<ul>' : '' %>
<%= shift < 0 ? '</ul>' :'' %>
<li><%= link_to section.name, :action => 'show', :id => section.id %></li>
<% end %>
</ul>
# File app/models/folder.rb, line 229
229: def iterate # yields: folder, level, shift
230: shift = 0
231: prev_level = nil
232: self.childrens.each do |folder|
233: level = folder.indentation.to_i
234: prev_level ||= level
235: if prev_level < level then shift = 1 end
236: if prev_level > level then shift = -1 end
237: if prev_level == level then shift = 0 end
238: yield folder, level, shift
239: prev_level = level
240: end
241: end
Returns mapping that covers this folder
# File app/models/folder.rb, line 80
80: def mapping
81: sql = sql_store.folder_find_mapping(self.id)
82: Mapping.find_by_sql( sql ).first
83: end