| Module | ActsAsFileSystem::InstanceMethods |
| In: |
lib/acts_as_filesystem.rb
|
| recalculate_path | [RW] | used to know when to trigger batch renaming |
# File lib/acts_as_filesystem.rb, line 175
175: def all_children
176: res = map_traversal
177: res.shift
178: res
179: end
calculates the full path to this category using parent‘s path.
# File lib/acts_as_filesystem.rb, line 121
121: def calculate_path
122: if self.top_level?
123: self.slug
124: else
125: self.parent.calculate_path + "/" + self.slug
126: end
127: end
calculates the full name of a category by accessing the name of all its ancestors.
If you have this category hierarchy:
Category "A"
Category "B"
Category "C"
Then Category "C" will have "A/B/C" as its full name.
# File lib/acts_as_filesystem.rb, line 61
61: def full_name(sep = '/')
62: self.hierarchy.map {|item| item.name || '?' }.join(sep)
63: end
gets the name without leading parents. Usefull when dividing categories in top-level groups and full names must not include the top-level category which is already a emphasized label
# File lib/acts_as_filesystem.rb, line 68
68: def full_name_without_leading(count, sep = '/')
69: parts = self.full_name(sep).split(sep)
70: count.times { parts.shift }
71: parts.join(sep)
72: end
returns the full hierarchy from the top-level item to this one. For example, if item1 has a children item2 and item2 has a children item3, then item3‘s hierarchy would be [item1, item2, item3].
If reload is passed as true, then the hierarchy is reload (usefull when the ActiveRecord object was modified in some way, or just after changing parent)
# File lib/acts_as_filesystem.rb, line 144
144: def hierarchy(reload = false)
145: if reload
146: @hierarchy = nil
147: end
148:
149: unless @hierarchy
150: @hierarchy = []
151: item = self
152: while item
153: @hierarchy.unshift(item)
154: item = item.parent
155: end
156: end
157:
158: @hierarchy
159: end
calculates the level of the category in the category hierarchy. Top-level categories have level 0; the children of the top-level categories have level 1; the children of categories with level 1 have level 2, and so on.
A level 0
/ # B C level 1
/ \ / # E F G H level 2
...
# File lib/acts_as_filesystem.rb, line 84
84: def level
85: self.parent ? (self.parent.level + 1) : 0
86: end
# File lib/acts_as_filesystem.rb, line 161
161: def map_traversal(&block)
162: result = []
163: current_level = [self]
164:
165: while !current_level.empty?
166: result += current_level
167: ids = current_level.select {|item| item.children_count > 0}.map(&:id)
168: break if ids.empty?
169: current_level = self.class.find(:all, :conditions => { :parent_id => ids})
170: end
171: block ||= (lambda { |x| x })
172: result.map(&block)
173: end
sets the name of the category. Also sets slug accordingly.
# File lib/acts_as_filesystem.rb, line 101
101: def name=(value)
102: if self.name != value
103: self.recalculate_path = true
104: end
105:
106: self[:name] = value
107: unless self.name.blank?
108: self.slug = self.name.to_slug
109: end
110: end
sets the slug of the category. Also sets the path with the new slug value.
# File lib/acts_as_filesystem.rb, line 113
113: def slug=(value)
114: self[:slug] = value
115: unless self.slug.blank?
116: self.path = self.calculate_path
117: end
118: end
# File lib/acts_as_filesystem.rb, line 129
129: def top_ancestor
130: self.top_level? ? self : self.parent.top_ancestor
131: end