Module WizardActsAsOrderedTree::Acts::OrderedTree::InstanceMethods
In: lib/acts_as_ordered_tree.rb

Methods

Public Class methods

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

[Source]

    # 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

Public Instance methods

returns an array of ancestors, starting from parent until root.

  return is cached
  use ancestors(true) to force a reload

[Source]

     # File lib/acts_as_ordered_tree.rb, line 113
113:         def ancestors(reload = false)
114:           reload = true if !@ancestors
115:           reload ? find_ancestors : @ancestors
116:         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

[Source]

     # 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

[Source]

     # 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

sends immediate children to the ‘roots’ list, then destroy‘s self

[Source]

     # File lib/acts_as_ordered_tree.rb, line 296
296:         def destroy_and_orphan_children
297:           self.class.transaction do
298:             orphan_children
299:             self.destroy
300:           end
301:         end

hands immediate children of to it‘s parent, then destroy‘s self

[Source]

     # File lib/acts_as_ordered_tree.rb, line 304
304:         def destroy_and_parent_adopts_children
305:           self.class.transaction do
306:             parent_adopts_children
307:             self.destroy
308:           end
309:         end

moves the item above sibling in the list

  defaults to the top of the list

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

orphans the node (sends it to the roots list)

  (descendants follow)

[Source]

     # File lib/acts_as_ordered_tree.rb, line 211
211:         def orphan
212:           self[foreign_key_column] = 0
213:           self.update
214:         end

orphans the node‘s children

  sends all immediate children to the 'roots' list

[Source]

     # File lib/acts_as_ordered_tree.rb, line 218
218:         def orphan_children
219:           self.class.transaction do
220:             children(true).each{|child| child.orphan}
221:           end
222:         end

sends self and immediate children to the roots list

[Source]

     # File lib/acts_as_ordered_tree.rb, line 237
237:         def orphan_self_and_children
238:           self.class.transaction do
239:             orphan_children
240:             orphan
241:           end
242:         end

hands children off to parent (if possible), then orphans itself

[Source]

     # File lib/acts_as_ordered_tree.rb, line 245
245:         def orphan_self_and_parent_adopts_children
246:           self.class.transaction do
247:             parent_adopts_children
248:             orphan
249:           end
250:         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

[Source]

     # 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

hands children off to parent

  if no parent, children will be orphaned

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

returns an array of the object‘s siblings, excluding itself

  return is cached
  use siblings(true) to force a reload

[Source]

     # File lib/acts_as_ordered_tree.rb, line 164
164:         def siblings(reload = false)
165:           self_and_siblings(reload) - [self]
166:         end

[Validate]