Module: CollectiveIdea::Acts::NestedSet::InstanceMethods

Included in:
SingletonMethods
Defined in:
vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb

Overview

Any instance method that returns a collection makes use of Rails 2.1’s named_scope (which is bundled for Rails 2.0), so it can be treated as a finder.

  category.self_and_descendants.count
  category.ancestors.find(:all, :conditions => "name like '%foo%'")

Instance Method Summary

Instance Method Details

- (Object) <=>(x)

order by left column



269
270
271
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 269

def <=>(x)
  left <=> x.left
end

- (Object) ==(comparison_object)

Redefine to act like active record



274
275
276
277
278
279
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 274

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.instance_of?(self.class) &&
      comparison_object.id == id &&
      !comparison_object.new_record?)
end

- (Object) ancestors

Returns an array of all parents



299
300
301
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 299

def ancestors
  without_self self_and_ancestors
end

- (Boolean) child?

Returns true is this is a child node

Returns:

  • (Boolean)


264
265
266
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 264

def child?
  !parent_id.nil?
end

- (Object) children

Returns a set of only this entry’s immediate children



337
338
339
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 337

def children
  nested_set_scope.scoped :conditions => {parent_column_name => self}
end

- (Object) descendants

Returns a set of all of its children and nested children



332
333
334
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 332

def descendants
  without_self self_and_descendants
end

- (Boolean) is_ancestor_of?(other)

Returns:

  • (Boolean)


349
350
351
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 349

def is_ancestor_of?(other)
  self.left < other.left && other.left < self.right && same_scope?(other)
end

- (Boolean) is_descendant_of?(other)

Returns:

  • (Boolean)


341
342
343
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 341

def is_descendant_of?(other)
  other.left < self.left && self.left < other.right && same_scope?(other)
end

- (Boolean) is_or_is_ancestor_of?(other)

Returns:

  • (Boolean)


353
354
355
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 353

def is_or_is_ancestor_of?(other)
  self.left <= other.left && other.left < self.right && same_scope?(other)
end

- (Boolean) is_or_is_descendant_of?(other)

Returns:

  • (Boolean)


345
346
347
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 345

def is_or_is_descendant_of?(other)
  other.left <= self.left && self.left < other.right && same_scope?(other)
end

- (Boolean) leaf?

Returns:

  • (Boolean)


259
260
261
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 259

def leaf?
  new_record? || (right - left == 1)
end

- (Object) leaves

Returns a set of all of its nested children which do not have children



314
315
316
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 314

def leaves
  descendants.scoped :conditions => "#{self.class.table_name}.#{quoted_right_column_name} - #{self.class.table_name}.#{quoted_left_column_name} = 1"
end

- (Object) left

Value of the left column



245
246
247
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 245

def left
  self[left_column_name]
end

- (Object) left_sibling

Find the first sibling to the left



365
366
367
368
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 365

def left_sibling
  siblings.find(:first, :conditions => ["#{self.class.table_name}.#{quoted_left_column_name} < ?", left],
    :order => "#{self.class.table_name}.#{quoted_left_column_name} DESC")
end

- (Object) level

Returns the level of this object in the tree root level is 0



320
321
322
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 320

def level
  parent_id.nil? ? 0 : ancestors.count
end

- (Object) move_left

Shorthand method for finding the left sibling and moving to the left of it.



376
377
378
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 376

def move_left
  move_to_left_of left_sibling
end

- (Boolean) move_possible?(target)

Returns:

  • (Boolean)


405
406
407
408
409
410
411
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 405

def move_possible?(target)
  self != target && # Can't target self
  same_scope?(target) && # can't be in different scopes
  # !(left..right).include?(target.left..target.right) # this needs tested more
  # detect impossible move
  !((left <= target.left && right >= target.left) or (left <= target.right && right >= target.right))
end

- (Object) move_right

Shorthand method for finding the right sibling and moving to the right of it.



381
382
383
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 381

def move_right
  move_to_right_of right_sibling
end

- (Object) move_to_child_of(node)

Move the node to the child of another node (you can pass id only)



396
397
398
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 396

def move_to_child_of(node)
  move_to node, :child
end

- (Object) move_to_left_of(node)

Move the node to the left of another node (you can pass id only)



386
387
388
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 386

def move_to_left_of(node)
  move_to node, :left
end

- (Object) move_to_right_of(node)

Move the node to the left of another node (you can pass id only)



391
392
393
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 391

def move_to_right_of(node)
  move_to node, :right
end

- (Object) move_to_root

Move the node to root nodes



401
402
403
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 401

def move_to_root
  move_to nil, :root
end

- (Object) parent

Returns the immediate parent



287
288
289
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 287

def parent
  nested_set_scope.find_by_id(parent_id) if parent_id
end

- (Object) parent_id

Value of the parent column



240
241
242
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 240

def parent_id
  self[parent_column_name]
end

- (Object) right

Value of the right column



250
251
252
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 250

def right
  self[right_column_name]
end

- (Object) right_sibling

Find the first sibling to the right



371
372
373
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 371

def right_sibling
  siblings.find(:first, :conditions => ["#{self.class.table_name}.#{quoted_left_column_name} > ?", left])
end

- (Object) root

Returns root



282
283
284
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 282

def root
  self_and_ancestors.find(:first)
end

- (Boolean) root?

Returns true if this is a root node.

Returns:

  • (Boolean)


255
256
257
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 255

def root?
  parent_id.nil?
end

- (Boolean) same_scope?(other)

Check if other model is in the same scope

Returns:

  • (Boolean)


358
359
360
361
362
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 358

def same_scope?(other)
  Array(acts_as_nested_set_options[:scope]).all? do |attr|
    self.send(attr) == other.send(attr)
  end
end

- (Object) self_and_ancestors

Returns the array of all parents and self



292
293
294
295
296
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 292

def self_and_ancestors
  nested_set_scope.scoped :conditions => [
    "#{self.class.table_name}.#{quoted_left_column_name} <= ? AND #{self.class.table_name}.#{quoted_right_column_name} >= ?", left, right
  ]
end

- (Object) self_and_descendants

Returns a set of itself and all of its nested children



325
326
327
328
329
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 325

def self_and_descendants
  nested_set_scope.scoped :conditions => [
    "#{self.class.table_name}.#{quoted_left_column_name} >= ? AND #{self.class.table_name}.#{quoted_right_column_name} <= ?", left, right
  ]
end

- (Object) self_and_siblings

Returns the array of all children of the parent, including self



304
305
306
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 304

def self_and_siblings
  nested_set_scope.scoped :conditions => {parent_column_name => parent_id}
end

- (Object) siblings

Returns the array of all children of the parent, except self



309
310
311
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 309

def siblings
  without_self self_and_siblings
end

- (Object) to_text



413
414
415
416
417
# File 'vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb', line 413

def to_text
  self_and_descendants.map do |node|
    "#{'*'*(node.level+1)} #{node.id} #{node.to_s} (#{node.parent_id}, #{node.left}, #{node.right})"
  end.join("\n")
end