module Mongoid::Acts::NestedSet::Document::ClassMethods

Public Instance Methods

after_move(*args, &block) click to toggle source
# File lib/mongoid_nested_set/document.rb, line 117
def after_move(*args, &block)
  set_callback :move, :after, *args, &block
end
before_move(*args, &block) click to toggle source
# File lib/mongoid_nested_set/document.rb, line 112
def before_move(*args, &block)
  set_callback :move, :before, *args, &block
end
descendants_of(parents) click to toggle source

Provides a chainable relation to select all descendants of a set of records, excluding the record set itself. Similar to parent.descendants, except this allows you to find all descendants of a set of nodes, rather than being restricted to find the descendants of only a single node.

Example:

parents = Category.roots.all
parents_descendants = Category.where(:deleted => false).descendants_of(parents)
# File lib/mongoid_nested_set/document.rb, line 103
def descendants_of(parents)
  # TODO: Add root or scope?
  conditions = parents.map do |parent|
    {left_field_name => {"$gt" => parent.left}, right_field_name => {"$lt" => parent.right}}
  end
  where("$or" => conditions)
end
each_with_ancestors(objects) { |o, ancestors| ... } click to toggle source

Iterates over tree elements with ancestors. Only accepts default ordering, ordering by an other field than lft does not work. This is much more efficient than calling ancestors for each object because it doesn’t require any additional database queries.

Example:

Category.each_with_ancestors(Category.root.self_and_descendants) do |o, ancestors|
# File lib/mongoid_nested_set/document.rb, line 70
def each_with_ancestors(objects)
  ancestors = nil
  last_parent = nil
  objects.each do |o|
    if ancestors == nil
      ancestors = o.root? ? [] : o.ancestors.entries
    end
    if ancestors.empty? || o.parent_id != ancestors.last.id
      # we are on a new level, did we descend or ascend?
      if ancestors.any? {|a| a.id == o.parent_id}
        # ascend
        ancestors.pop while (!ancestors.empty? && ancestors.last.id != o.parent_id)
      elsif !o.root?
        # descend
        ancestors << last_parent
      end
    end
    yield(o, ancestors)
    last_parent = o
  end
end
each_with_level(objects) { |o, length - 1 + offset| ... } click to toggle source

Iterates over tree elements and determines the current level in the tree. Only accepts default ordering, ordering by an other field than lft does not work. This method is much more efficient then calling level because it doesn’t require any additional database queries. This method does not used the cached depth field.

Example:

Category.each_with_level(Category.root.self_and_descendants) do |o, level|
# File lib/mongoid_nested_set/document.rb, line 41
def each_with_level(objects)
  offset = nil
  path = [nil]
  objects.each do |o|
    if offset == nil
      offset = o.parent_id.nil? ? 0 : o.parent.level
    end
    if o.parent_id != path.last
      # we are on a new level, did we descend or ascend?
      if path.include?(o.parent_id)
        # remove wrong tailing path elements
        path.pop while path.last != o.parent_id
      else
        path << o.parent_id
      end
    end
    yield(o, path.length - 1 + offset)
  end
end
root() click to toggle source

Returns the first root

# File lib/mongoid_nested_set/document.rb, line 18
def root
  roots.first
end
scope_condition_by_options(options) click to toggle source
# File lib/mongoid_nested_set/document.rb, line 23
def scope_condition_by_options(options)
  h = {}
  scope_string = Array(acts_as_nested_set_options[:scope]).reject{|s| !options.has_key?(s) }.each do |c|
    h[c] = options[c]
  end
  h
end