Files

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

Public Instance Methods

all_roots_valid?() click to toggle source

Wrapper for each_root_valid? that can deal with scope.

# File lib/awesome_nested_set/awesome_nested_set.rb, line 155
def all_roots_valid?
  if acts_as_nested_set_options[:scope]
    roots.group_by {|record| scope_column_names.collect {|col| record.send(col.to_sym) } }.all? do |scope, grouped_roots|
      each_root_valid?(grouped_roots)
    end
  else
    each_root_valid?(roots)
  end
end
each_root_valid?(roots_to_validate) click to toggle source
# File lib/awesome_nested_set/awesome_nested_set.rb, line 165
def each_root_valid?(roots_to_validate)
  left = right = 0
  roots_to_validate.all? do |root|
    (root.left > left && root.right > right).tap do
      left = root.left
      right = root.right
    end
  end
end
each_with_level(objects) 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/awesome_nested_set.rb, line 217
def each_with_level(objects)
  path = [nil]
  objects.each do |o|
    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 wrong tailing paths elements
        path.pop while path.last != o.parent_id
      else
        path << o.parent_id
      end
    end
    yield(o, path.length - 1)
  end
end
leaves() click to toggle source
# File lib/awesome_nested_set/awesome_nested_set.rb, line 116
def leaves
  where("#{quoted_right_column_name} - #{quoted_left_column_name} = 1").order(quoted_left_column_name)
end
left_and_rights_valid?() click to toggle source
# File lib/awesome_nested_set/awesome_nested_set.rb, line 124
def left_and_rights_valid?
  ## AS clause not supported in Oracle in FROM clause for aliasing table name
  joins("LEFT OUTER JOIN #{quoted_table_name}" +
      (connection.adapter_name.match(/Oracle/).nil? ?  " AS " : " ") +
      "parent ON " +
      "#{quoted_table_name}.#{quoted_parent_column_name} = parent.#{primary_key}").
  where(
      "#{quoted_table_name}.#{quoted_left_column_name} IS NULL OR " +
      "#{quoted_table_name}.#{quoted_right_column_name} IS NULL OR " +
      "#{quoted_table_name}.#{quoted_left_column_name} >= " +
        "#{quoted_table_name}.#{quoted_right_column_name} OR " +
      "(#{quoted_table_name}.#{quoted_parent_column_name} IS NOT NULL AND " +
        "(#{quoted_table_name}.#{quoted_left_column_name} <= parent.#{quoted_left_column_name} OR " +
        "#{quoted_table_name}.#{quoted_right_column_name} >= parent.#{quoted_right_column_name}))"
  ).count == 0
end
no_duplicates_for_columns?() click to toggle source
# File lib/awesome_nested_set/awesome_nested_set.rb, line 141
def no_duplicates_for_columns?
  scope_string = Array(acts_as_nested_set_options[:scope]).map do |c|
    connection.quote_column_name(c)
  end.push(nil).join(", ")
  [quoted_left_column_name, quoted_right_column_name].all? do |column|
    # No duplicates
    select("#{scope_string}#{column}, COUNT(#{column})").
        group("#{scope_string}#{column}").
        having("COUNT(#{column}) > 1").
        first.nil?
  end
end
rebuild!(validate_nodes = true) click to toggle source

Rebuilds the left & rights if unset or invalid. Also very useful for converting from acts_as_tree.

# File lib/awesome_nested_set/awesome_nested_set.rb, line 177
def rebuild!(validate_nodes = true)
  # Don't rebuild a valid tree.
  return true if valid?

  scope = lambda{|node|}
  if acts_as_nested_set_options[:scope]
    scope = lambda{|node|
      scope_column_names.inject(""){|str, column_name|
        str << "AND #{connection.quote_column_name(column_name)} = #{connection.quote(node.send(column_name.to_sym))} "
      }
    }
  end
  indices = {}

  set_left_and_rights = lambda do |node|
    # set left
    node[left_column_name] = indices[scope.call(node)] += 1
    # find
    where(["#{quoted_parent_column_name} = ? #{scope.call(node)}", node]).order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").each{|n| set_left_and_rights.call(n) }
    # set right
    node[right_column_name] = indices[scope.call(node)] += 1
    node.save!(:validate => validate_nodes)
  end

  # Find root node(s)
  root_nodes = where("#{quoted_parent_column_name} IS NULL").order("#{quoted_left_column_name}, #{quoted_right_column_name}, id").each do |root_node|
    # setup index for this scope
    indices[scope.call(root_node)] ||= 0
    set_left_and_rights.call(root_node)
  end
end
root() click to toggle source

Returns the first root

# File lib/awesome_nested_set/awesome_nested_set.rb, line 108
def root
  roots.first
end
roots() click to toggle source
# File lib/awesome_nested_set/awesome_nested_set.rb, line 112
def roots
  where(parent_column_name => nil).order(quoted_left_column_name)
end
sorted_each_with_level(objects, order) click to toggle source

Same as each_with_level - Accepts a string as a second argument to sort the list Example:

Category.each_with_level(Category.root.self_and_descendants, :sort_by_this_column) do |o, level|
# File lib/awesome_nested_set/awesome_nested_set.rb, line 236
def sorted_each_with_level(objects, order)
  path = [nil]
  children = []
    objects.each do |o|
    children << o if o.leaf?
    if o.parent_id != path.last
      if !children.empty? && !o.leaf?
        children.sort_by! &order
        children.each { |c| yield(c, path.length-1) }
        children = []
      end
      # we are on a new level, did we decent or ascent?
      if path.include?(o.parent_id)
        # remove wrong wrong tailing paths elements
        path.pop while path.last != o.parent_id
      else
        path << o.parent_id
      end
    end
    yield(o,path.length-1) if !o.leaf?
  end
  if !children.empty?
    children.sort_by! &order
    children.each { |c| yield(c, path.length-1) }
  end
end
valid?() click to toggle source
# File lib/awesome_nested_set/awesome_nested_set.rb, line 120
def valid?
  left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.