def acts_as_tree(options = {})
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
configuration.update(options) if options.is_a?(Hash)
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => true
module_eval "def self.roots\nself.find(:all, :conditions => \"\#{configuration[:foreign_key]} IS NULL\", :order => \#{configuration[:order].nil? ? \"nil\" : %Q{\"\#{configuration[:order]}\"}})\nend\ndef self.root\nself.find(:first, :conditions => \"\#{configuration[:foreign_key]} IS NULL\", :order => \#{configuration[:order].nil? ? \"nil\" : %Q{\"\#{configuration[:order]}\"}})\nend\n"
define_method(:ancestors) do
node, nodes = self, []
nodes << node = node.parent until not node.has_parent?
nodes
end
define_method(:root) do
node = self
node = node.parent until not node.has_parent?
node
end
define_method(:siblings) do
self_and_siblings - [self]
end
define_method(:self_and_siblings) do
has_parent? ? parent.children : self.class.roots
end
end