| Module | WizardActsAsOrderedTree::Acts::OrderedTree::InstanceMethods |
| In: |
lib/acts_as_ordered_tree.rb
|
returns an ordered array of all nodes without a parent
i.e. parent_id = 0 return is cached use self.class.roots(true) to force a reload
# File lib/acts_as_ordered_tree.rb, line 97
97: def self.roots(reload = false)
98: # stub for rdoc - overwritten in AddActsAsMethod::acts_as_ordered_tree
99: end
returns an array of the object‘s immediate children
auto-loads itself on first access instead of returning "<child_nodes not loaded yet>" return is cached use children(true) to force a reload
# File lib/acts_as_ordered_tree.rb, line 135
135: def children(reload=false)
136: reload = true if !@children
137: reload ? child_nodes(true) : @children
138: end
returns an array of the object‘s descendants
return is cached use descendants(true) to force a reload
# File lib/acts_as_ordered_tree.rb, line 144
144: def descendants(reload = false)
145: @descendants = nil if reload
146: reload = true if !@descendants
147: reload ? find_descendants(self) : @descendants
148: end
moves the item above sibling in the list
defaults to the top of the list
# File lib/acts_as_ordered_tree.rb, line 256
256: def move_above(sibling = nil)
257: if sibling
258: return if (!self_and_siblings(true).include?(sibling) || (sibling == self))
259: if sibling.position_in_list > position_in_list
260: move_to(sibling.position_in_list - 1)
261: else
262: move_to(sibling.position_in_list)
263: end
264: else
265: move_to_top
266: end
267: end
swap with the node above self
# File lib/acts_as_ordered_tree.rb, line 276
276: def move_higher
277: return if position_in_list == 1
278: move_to(position_in_list - 1)
279: end
swap with the node below self
# File lib/acts_as_ordered_tree.rb, line 282
282: def move_lower
283: return if self == self_and_siblings(true).last
284: move_to(position_in_list + 1)
285: end
move to the bottom of the list
# File lib/acts_as_ordered_tree.rb, line 288
288: def move_to_bottom
289: return if self == self_and_siblings(true).last
290: move_to(self_and_siblings.last.position_in_list)
291: end
move to the top of the list
# File lib/acts_as_ordered_tree.rb, line 270
270: def move_to_top
271: return if position_in_list == 1
272: move_to(1)
273: end
returns object‘s parent in the tree
auto-loads itself on first access instead of returning "<parent_node not loaded yet>" return is cached, unless nil use parent(true) to force a reload
# File lib/acts_as_ordered_tree.rb, line 124
124: def parent(reload=false)
125: reload = true if !@parent
126: reload ? parent_node(true) : @parent
127: end
if no parent, children will be orphaned
# File lib/acts_as_ordered_tree.rb, line 226
226: def parent_adopts_children
227: if parent(true)
228: self.class.transaction do
229: children(true).each{|child| parent.children << child}
230: end
231: else
232: orphan_children
233: end
234: end
returns object‘s position in the list
the list will either be parent.children, or self.class.roots i.e. self.position
# File lib/acts_as_ordered_tree.rb, line 173
173: def position_in_list
174: self[order_column]
175: end
returns the top node in the object‘s tree
return is cached, unless nil use root(true) to force a reload
# File lib/acts_as_ordered_tree.rb, line 105
105: def root(reload = false)
106: reload = true if !@root
107: reload ? find_root : @root
108: end
returns an array of the object‘s siblings, including itself
return is cached use self_and_siblings(true) to force a reload
# File lib/acts_as_ordered_tree.rb, line 156
156: def self_and_siblings(reload = false)
157: parent(reload) ? parent.children(reload) : self.class.roots(reload)
158: end
shifts a node to another parent, optionally specifying it‘s position
(descendants will follow along)
shift_to()
defaults to the bottom of the "roots" list
shift_to(nil, new_sibling)
will move the item to "roots",
and position the item above new_sibling
shift_to(new_parent)
will move the item to the new parent,
and position at the bottom of the parent's list
shift_to(new_parent, new_sibling)
will move the item to the new parent,
and position the item above new_sibling
# File lib/acts_as_ordered_tree.rb, line 197
197: def shift_to(new_parent = nil, new_sibling = nil)
198: if new_parent
199: ok = new_parent.children(true) << self
200: else
201: ok = orphan
202: end
203: if ok && new_sibling
204: ok = move_above(new_sibling) if self_and_siblings(true).include?(new_sibling)
205: end
206: return ok
207: end