module CollectiveIdea::Acts::NestedSet::Model::ClassMethods

Public Instance Methods

add_to_inverse_association(association, record) click to toggle source
# File lib/awesome_nested_set/model.rb, line 42
def add_to_inverse_association(association, record)
  inverse_reflection = association.send(:inverse_reflection_for, record)
  inverse = record.association(inverse_reflection.name)
  inverse.target << association.owner
  inverse.loaded!
end
associate_parents(objects) click to toggle source
# File lib/awesome_nested_set/model.rb, line 27
def associate_parents(objects)
  return objects unless objects.all? {|o| o.respond_to?(:association)}

  id_indexed = objects.index_by(&primary_column_name.to_sym)
  objects.each do |object|
    association = object.association(:parent)
    parent = id_indexed[object.parent_id]

    if !association.loaded? && parent
      association.target = parent
      add_to_inverse_association(association, parent)
    end
  end
end
children_of(parent_id) click to toggle source
# File lib/awesome_nested_set/model.rb, line 49
def children_of(parent_id)
  where arel_table[parent_column_name].eq(parent_id)
end
each_with_level(objects, &block) click to toggle source

Iterates over tree elements and determines the current level in the tree. Only accepts default ordering, odering by an other column than lft does not work. This method is much more efficent than calling level because it doesn't require any additional database queries.

Example:

Category.each_with_level(Category.root.self_and_descendants) do |o, level|
# File lib/awesome_nested_set/model.rb, line 61
def each_with_level(objects, &block)
  Iterator.new(objects).each_with_level(&block)
end
leaves() click to toggle source
# File lib/awesome_nested_set/model.rb, line 65
def leaves
  nested_set_scope.where "#{quoted_right_column_full_name} - #{quoted_left_column_full_name} = 1"
end
left_of(node) click to toggle source
# File lib/awesome_nested_set/model.rb, line 69
def left_of(node)
  where arel_table[left_column_name].lt(node)
end
left_of_right_side(node) click to toggle source
# File lib/awesome_nested_set/model.rb, line 73
def left_of_right_side(node)
  where arel_table[right_column_name].lteq(node)
end
nested_set_scope(options = {}) click to toggle source
# File lib/awesome_nested_set/model.rb, line 81
def nested_set_scope(options = {})
  options = {:order => quoted_order_column_full_name}.merge(options)

  where(options[:conditions]).order(options.delete(:order))
end
primary_key_scope(id) click to toggle source
# File lib/awesome_nested_set/model.rb, line 87
def primary_key_scope(id)
  where arel_table[primary_column_name].eq(id)
end
right_of(node) click to toggle source
# File lib/awesome_nested_set/model.rb, line 77
def right_of(node)
  where arel_table[left_column_name].gteq(node)
end
root() click to toggle source
# File lib/awesome_nested_set/model.rb, line 91
def root
  roots.first
end
roots() click to toggle source
# File lib/awesome_nested_set/model.rb, line 95
def roots
  nested_set_scope.children_of nil
end