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

Defined in:
lib/nested_set/base.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 collapse

Instance Method Details

#<=>(x) ⇒ Object

order by left column



393
394
395
# File 'lib/nested_set/base.rb', line 393

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

#==(comparison_object) ⇒ Object

Redefine to act like active record



398
399
400
401
402
403
# File 'lib/nested_set/base.rb', line 398

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

#ancestorsObject

Returns an array of all parents



421
422
423
# File 'lib/nested_set/base.rb', line 421

def ancestors
  without_self self_and_ancestors
end

#child?Boolean

Returns true is this is a child node

Returns:

  • (Boolean)


388
389
390
# File 'lib/nested_set/base.rb', line 388

def child?
  !parent_id.nil?
end

#descendantsObject

Returns a set of all of its children and nested children



452
453
454
# File 'lib/nested_set/base.rb', line 452

def descendants
  without_self self_and_descendants
end

#is_ancestor_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


464
465
466
# File 'lib/nested_set/base.rb', line 464

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

#is_descendant_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


456
457
458
# File 'lib/nested_set/base.rb', line 456

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

#is_or_is_ancestor_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


468
469
470
# File 'lib/nested_set/base.rb', line 468

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

#is_or_is_descendant_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


460
461
462
# File 'lib/nested_set/base.rb', line 460

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

#leaf?Boolean

Returns:

  • (Boolean)


383
384
385
# File 'lib/nested_set/base.rb', line 383

def leaf?
  !new_record? && right - left == 1
end

#leavesObject

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



436
437
438
# File 'lib/nested_set/base.rb', line 436

def leaves
  descendants.where("#{q_right} - #{q_left} = 1")
end

#leftObject

Value of the left column



369
370
371
# File 'lib/nested_set/base.rb', line 369

def left
  self[left_column_name]
end

#left_siblingObject

Find the first sibling to the left



480
481
482
# File 'lib/nested_set/base.rb', line 480

def left_sibling
  siblings.where("#{q_left} < ?", left).last
end

#levelObject

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



442
443
444
# File 'lib/nested_set/base.rb', line 442

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

#lock_check(cond = nil) ⇒ Object

Lock rows whose lfts and rgts are to be updated



490
491
492
# File 'lib/nested_set/base.rb', line 490

def lock_check(cond=nil)
  nested_set_scope.select(primary_key_column_name).where(cond).lock
end

#move_leftObject

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



495
496
497
# File 'lib/nested_set/base.rb', line 495

def move_left
  move_to_left_of left_sibling
end

#move_possible?(target) ⇒ Boolean

Returns:

  • (Boolean)


524
525
526
527
528
529
530
# File 'lib/nested_set/base.rb', line 524

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

#move_rightObject

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



500
501
502
# File 'lib/nested_set/base.rb', line 500

def move_right
  move_to_right_of right_sibling
end

#move_to_child_of(node) ⇒ Object

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



515
516
517
# File 'lib/nested_set/base.rb', line 515

def move_to_child_of(node)
  move_to node, :child
end

#move_to_left_of(node) ⇒ Object

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



505
506
507
# File 'lib/nested_set/base.rb', line 505

def move_to_left_of(node)
  move_to node, :left
end

#move_to_right_of(node) ⇒ Object

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



510
511
512
# File 'lib/nested_set/base.rb', line 510

def move_to_right_of(node)
  move_to node, :right
end

#move_to_rootObject

Move the node to root nodes



520
521
522
# File 'lib/nested_set/base.rb', line 520

def move_to_root
  move_to nil, :root
end

#parent_idObject

Value of the parent column



364
365
366
# File 'lib/nested_set/base.rb', line 364

def parent_id
  self[parent_column_name]
end

#rightObject

Value of the right column



374
375
376
# File 'lib/nested_set/base.rb', line 374

def right
  self[right_column_name]
end

#right_siblingObject

Find the first sibling to the right



485
486
487
# File 'lib/nested_set/base.rb', line 485

def right_sibling
  siblings.where("#{q_left} > ?", left).first
end

#rootObject

Returns root



406
407
408
# File 'lib/nested_set/base.rb', line 406

def root
  self_and_ancestors.first
end

#root?Boolean

Returns true if this is a root node.

Returns:

  • (Boolean)


379
380
381
# File 'lib/nested_set/base.rb', line 379

def root?
  parent_id.nil?
end

#same_scope?(other) ⇒ Boolean

Check if other model is in the same scope

Returns:

  • (Boolean)


473
474
475
476
477
# File 'lib/nested_set/base.rb', line 473

def same_scope?(other)
  scope_column_names.all? do |attr|
    self.send(attr) == other.send(attr)
  end
end

#self_and_ancestorsObject

Returns the array of all parents and self



416
417
418
# File 'lib/nested_set/base.rb', line 416

def self_and_ancestors
  nested_set_scope.where("#{q_left} <= ? AND #{q_right} >= ?", left, right)
end

#self_and_childrenObject

Returns the array of all children and self



411
412
413
# File 'lib/nested_set/base.rb', line 411

def self_and_children
  nested_set_scope.where("#{q_parent} = :id or #{q_primary_key} = :id", :id => self)
end

#self_and_descendantsObject

Returns a set of itself and all of its nested children



447
448
449
# File 'lib/nested_set/base.rb', line 447

def self_and_descendants
  nested_set_scope.where("#{q_left} >= ? AND #{q_right} <= ?", left, right)
end

#self_and_siblingsObject

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



426
427
428
# File 'lib/nested_set/base.rb', line 426

def self_and_siblings
  nested_set_scope.where(parent_column_name => parent_id)
end

#siblingsObject

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



431
432
433
# File 'lib/nested_set/base.rb', line 431

def siblings
  without_self self_and_siblings
end

#to_textObject



532
533
534
535
536
# File 'lib/nested_set/base.rb', line 532

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